What is the regexp for the space between two letters of a same word in Ruby

huangapple go评论76阅读模式
英文:

What is the regexp for the space between two letters of a same word in Ruby

问题

def encode(text)
  encode_word = ""
  text.chars.each { |element| encode_word << element.gsub(/(\w|\W)/, MASTER) }
  encode_word
end
英文:

The code executes well but I am missing a spec: the space between letters of the same word must be replaced by an empty space in morse. For example: hi in morse would be .... .. (4 dots, space, 2 dots)....the code is not recognizing the way I am representing this separation in my master hash --> "" => " "

Any thoughts on what am I missing?. Thanks

My code is as follows:

MASTER = { &quot;A&quot; =&gt; &quot;.-&quot;,
           &quot;B&quot; =&gt; &quot;-...&quot;,
           &quot;C&quot; =&gt; &quot;-.-.&quot;,
           &quot;D&quot; =&gt; &quot;-..&quot;,
           &quot;E&quot; =&gt; &quot;.&quot;,
           &quot;F&quot; =&gt; &quot;..-.&quot;,
           &quot;G&quot; =&gt; &quot;--.&quot;,
           &quot;H&quot; =&gt; &quot;....&quot;,
           &quot;I&quot; =&gt; &quot;..&quot;,
           &quot;J&quot; =&gt; &quot;.---&quot;,
           &quot;K&quot; =&gt; &quot;-.-&quot;,
           &quot;L&quot; =&gt; &quot;.-..&quot;,
           &quot;M&quot; =&gt; &quot;--&quot;,
           &quot;N&quot; =&gt; &quot;-.&quot;,
           &quot;O&quot; =&gt; &quot;---&quot;,
           &quot;P&quot; =&gt; &quot;.--.&quot;,
           &quot;Q&quot; =&gt; &quot;--.-&quot;,
           &quot;R&quot; =&gt; &quot;.-.&quot;,
           &quot;S&quot; =&gt; &quot;...&quot;,
           &quot;T&quot; =&gt; &quot;-&quot;,
           &quot;U&quot; =&gt; &quot;..-&quot;,
           &quot;V&quot; =&gt; &quot;...-&quot;,
           &quot;W&quot; =&gt; &quot;.--&quot;,
           &quot;X&quot; =&gt; &quot;-..-&quot;,
           &quot;Y&quot; =&gt; &quot;-.--&quot;,
           &quot;Z&quot; =&gt; &quot;--..&quot;,
           &quot; &quot; =&gt; &quot;|&quot;,
           &quot;a&quot; =&gt; &quot;.-&quot;,
           &quot;b&quot; =&gt; &quot;-...&quot;,
           &quot;c&quot; =&gt; &quot;-.-.&quot;,
           &quot;d&quot; =&gt; &quot;-..&quot;,
           &quot;e&quot; =&gt; &quot;.&quot;,
           &quot;f&quot; =&gt; &quot;..-.&quot;,
           &quot;g&quot; =&gt; &quot;--.&quot;,
           &quot;h&quot; =&gt; &quot;....&quot;,
           &quot;i&quot; =&gt; &quot;..&quot;,
           &quot;j&quot; =&gt; &quot;.---&quot;,
           &quot;k&quot; =&gt; &quot;-.-&quot;,
           &quot;l&quot; =&gt; &quot;.-..&quot;,
           &quot;m&quot; =&gt; &quot;--&quot;,
           &quot;n&quot; =&gt; &quot;-.&quot;,
           &quot;o&quot; =&gt; &quot;---&quot;,
           &quot;p&quot; =&gt; &quot;.--.&quot;,
           &quot;q&quot; =&gt; &quot;--.-&quot;,
           &quot;r&quot; =&gt; &quot;.-.&quot;,
           &quot;s&quot; =&gt; &quot;...&quot;,
           &quot;t&quot; =&gt; &quot;-&quot;,
           &quot;u&quot; =&gt; &quot;..-&quot;,
           &quot;v&quot; =&gt; &quot;...-&quot;,
           &quot;w&quot; =&gt; &quot;.--&quot;,
           &quot;x&quot; =&gt; &quot;-..-&quot;,
           &quot;y&quot; =&gt; &quot;-.--&quot;,
           &quot;z&quot; =&gt; &quot;--..&quot;,
           &quot;,&quot; =&gt; &quot; &quot;,
           &quot;&quot;  =&gt; &quot; &quot;,
           &quot;&#39;&quot; =&gt; &quot; &quot; }

def encode(text)
  # TODO: write a method which returns the morse sentence for the given text
  # NOTE: you may want to extract a `encode_word` method
  encode_word = &quot;&quot;
  text.chars.each { |element| encode_word &lt;&lt; element.gsub(/(\w|\W)/, MASTER) }
  encode_word
end

答案1

得分: 2

我明白了。以下是您要求的代码部分的中文翻译:

# 将文本按空格(或可能是任何空白字符)拆分为单词,使用您的莫尔斯码替代表将这些单词中的每个字符进行翻译,然后将所有内容重新连接(用空格连接字符,用管道连接单词)。类似于

def encode(text):
  return '|'.join([' '.join([MASTER[char] for char in word]) for word in text.split(' ')])

请注意:

  • 一旦您的莫尔斯表中包含的输入不再存在,此方法将开始失败。例如,&#233; 将导致错误。
  • 由于莫尔斯码中大小写是相同的,您可以放弃一半的表,并确保使用正确的大小写查找。例如,放弃小写并像这样查找 MASTER[char.upper()]
英文:

Split the text by space (or perhaps any whitespace) into words, translate each character in those words using your morse code substitution table and then join everything back together (join chars with a space, words with a pipe). Something like

def encode(text)
  text.split(&quot; &quot;).map do |word|
    word.each_char.map do |char|
      MASTER[char]
    end.join(&quot; &quot;)
  end.join(&quot;|&quot;)
end

Note

  • this will start to fail as soon as you have input that is not contained in your morse table. E.g. a an &#233; will cause an error.
  • Since upper/lowercase is the same in morse, you can drop half the table and make sure you lookup with the correct case. E.g. drop lower case and do lookup like this MASTER[char.upcase]

答案2

得分: 2

首先让我们使用`word.chars.map { |letter| MASTER[letter] }`从哈希中获取等效的哈希值,然后我们使用这个正则表达式`/(?&lt;=\w) (?=\w)/`来替换空格。

def encode(text)
  text.gsub(/\w+/) do |word|
    word.chars.map { |letter| MASTER[letter] }.join("").gsub(/(?&lt;=\w) (?=\w)/, "")
  end
end

示例输入

text = "hello world"
p encode(text)

输出

"...- .- .-.. .-.. --- .-- --- .-. .-.. -.." 
英文:

Code

First let us get the hash equivalent from the hash using word.chars.map { |letter| MASTER[letter] } and then we use this regular expression /(?&lt;=\w) (?=\w)/ to replace the empty space.

def encode(text)
  text.gsub(/\w+/) do |word|
    word.chars.map { |letter| MASTER[letter] }.join(&quot;&quot;).gsub(/(?&lt;=\w) (?=\w)/, &quot;&quot;)
  end
end

Example input

text = &quot;hello world&quot;
p encode(text)

Output

&quot;......-...-..--- .-----.-..-..-..&quot;

答案3

得分: 2

master 成为问题中给出的哈希值。

假设

text = "Little Miss Muffet"

我们可以如下修改 text

text.gsub(/.(?=(.))?/) do |c|
  MASTER[c] + ((c != ' ' && $1 && $1 != ' ') ? ' ' : '')
end
  #=> ".-.. .. - - .-.. .|-- .. ... ...|-- ..- ..-. ..-. . -"

参见 String#gsub


正则表达式包含以下元素。

.       # 匹配任何字符(除了行终止符)
(?=     # 开始正向预查
  (.)   # 匹配任何字符并保存到捕获组 1
)?      # 结束正向预查并将其设为可选的

如果第一个匹配的字符后面还有一个字符,正向预查将被执行,并且以下字符将被保存到捕获组 1 中,并由全局变量 $1 保留。

如果第一个匹配的字符是字符串中的最后一个字符,将不会执行正向预查。在这种情况下 $1 #=> nil


表达式

c != ' ' && $1 && $1 != ' '

解读为,“匹配的字符('c')既不是空格,也不是字符串的最后一个字符($1),并且后面的字符也不是空格($1 != ' ')”。


请注意,这种方法直接操作字符串,而不是将其拆分为单词,将每个单词转换为字母数组,对这些数组进行操作,然后将结果转回字符串。

英文:

Let master be the hash given in the question.

Suppose

text = &quot;Little Miss Muffet&quot;

We can modify text as follows.

text.gsub(/.(?=(.))?/) do |c|
  MASTER[c] + ((c != &#39; &#39; &amp;&amp; $1 &amp;&amp; $1 != &#39; &#39;) ? &#39; &#39; : &#39;&#39;)
end
  #=&gt; &quot;.-.. .. - - .-.. .|-- .. ... ...|-- ..- ..-. ..-. . -&quot;

See String#gsub.


The regular expression has the following elements.

.       # match any character (other than a line terminator)
(?=     # begin a positive lookahead
  (.)   # match any character and save it to capture group 1
)?      # end the positive lookahead and make it optional

If the first character matched is followed by another character the positive lookahead will be executed and the following character will be saved to capture group 1 and held by the global variable $1.

The forward lookahead will not be executed if the first character matched is the last character in the string. In this case $1 #=&gt; nil.


The expression

c != &#39; &#39; &amp;&amp; $1 &amp;&amp; $1 != &#39; &#39;

reads, "The character matched (&#39;c&#39;) is neither a space nor is the last character of the string ($1) and the following character is not a space ($1 != &#39; &#39;)".


Note that this approach manipulates the string directly, rather than breaking it into words, converting each word to array of letters, manipulate those arrays and then converting the result back to a string.

huangapple
  • 本文由 发表于 2023年4月11日 14:05:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定