英文:
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 = { "A" => ".-",
"B" => "-...",
"C" => "-.-.",
"D" => "-..",
"E" => ".",
"F" => "..-.",
"G" => "--.",
"H" => "....",
"I" => "..",
"J" => ".---",
"K" => "-.-",
"L" => ".-..",
"M" => "--",
"N" => "-.",
"O" => "---",
"P" => ".--.",
"Q" => "--.-",
"R" => ".-.",
"S" => "...",
"T" => "-",
"U" => "..-",
"V" => "...-",
"W" => ".--",
"X" => "-..-",
"Y" => "-.--",
"Z" => "--..",
" " => "|",
"a" => ".-",
"b" => "-...",
"c" => "-.-.",
"d" => "-..",
"e" => ".",
"f" => "..-.",
"g" => "--.",
"h" => "....",
"i" => "..",
"j" => ".---",
"k" => "-.-",
"l" => ".-..",
"m" => "--",
"n" => "-.",
"o" => "---",
"p" => ".--.",
"q" => "--.-",
"r" => ".-.",
"s" => "...",
"t" => "-",
"u" => "..-",
"v" => "...-",
"w" => ".--",
"x" => "-..-",
"y" => "-.--",
"z" => "--..",
"," => " ",
"" => " ",
"'" => " " }
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 = ""
text.chars.each { |element| encode_word << 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(' ')])
请注意:
- 一旦您的莫尔斯表中包含的输入不再存在,此方法将开始失败。例如,
é
将导致错误。 - 由于莫尔斯码中大小写是相同的,您可以放弃一半的表,并确保使用正确的大小写查找。例如,放弃小写并像这样查找
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(" ").map do |word|
word.each_char.map do |char|
MASTER[char]
end.join(" ")
end.join("|")
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
é
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] }`从哈希中获取等效的哈希值,然后我们使用这个正则表达式`/(?<=\w) (?=\w)/`来替换空格。
def encode(text)
text.gsub(/\w+/) do |word|
word.chars.map { |letter| MASTER[letter] }.join("").gsub(/(?<=\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 /(?<=\w) (?=\w)/
to replace the empty space.
def encode(text)
text.gsub(/\w+/) do |word|
word.chars.map { |letter| MASTER[letter] }.join("").gsub(/(?<=\w) (?=\w)/, "")
end
end
Example input
text = "hello world"
p encode(text)
Output
"......-...-..--- .-----.-..-..-.."
答案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 = "Little Miss Muffet"
We can modify text
as follows.
text.gsub(/.(?=(.))?/) do |c|
MASTER[c] + ((c != ' ' && $1 && $1 != ' ') ? ' ' : '')
end
#=> ".-.. .. - - .-.. .|-- .. ... ...|-- ..- ..-. ..-. . -"
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 #=> nil
.
The expression
c != ' ' && $1 && $1 != ' '
reads, "The character matched ('c'
) is neither a space nor is the last character of the string ($1
) and the following character is not a space ($1 != ' '
)".
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论