gpt4 book ai didi

python - 莫尔斯电码程序不处理带空格的测试用例

转载 作者:行者123 更新时间:2023-12-01 06:47:03 27 4
gpt4 key购买 nike

我正在做《Code Wars》中的这个问题。我已经完成了基本的摩尔斯电码功能,但我没有找到解决其他一些测试用例的方法。

测试的代码对于 8/12 测试用例是正确的。我如何测试较长的句子,例如“The Brown Quick Fox Jumped over the起始狗”、“E E”的测试用例和“S O S”的测试用例?

这是我的代码:

def decodeMorse(morse_code):    
space_pos = morse_code.find(" ")
match_str = ''.join(MORSE_CODE.get(i) for i in morse_code.split())
match_list = list(match_str)
if space_pos >= 0:
edit_match = match_list.insert(int(space_pos/3.5), " ")
final_str = ''.join(match_list)
return final_str

这是Code Wars Kata我正在看:

In this kata you have to write a simple Morse code decoder. While the Morse code is now mostly superseded by voice and digital data communication channels, it still has its use in some applications around the world.

The Morse code encodes every character as a sequence of "dots" and "dashes". For example, the letter A is coded as ·−, letter Q is coded as −−·−, and digit 1 is coded as ·−−−−. The Morse code is case-insensitive, traditionally capital letters are used. When the message is written in Morse code, a single space is used to separate the character codes and 3 spaces are used to separate words. For example, the message HEY JUDE in Morse code is ···· · −·−− ·−−− ··− −·· ·.

NOTE: Extra spaces before or after the code have no meaning and should be ignored.

In addition to letters, digits and some punctuation, there are some special service codes, the most notorious of those is the international distress signal SOS (that was first issued by Titanic), that is coded as ···−−−···. These special codes are treated as single special characters, and usually are transmitted as separate words.

Your task is to implement a function that would take the morse code as input and return a decoded human-readable string.

For example:

decodeMorse('.... . -.--   .--- ..- -.. .')
#should return "HEY JUDE"

NOTE: For coding purposes you have to use ASCII characters . and -, not Unicode characters.

The Morse code table is preloaded for you as a dictionary, feel free to use it: MORSE_CODE['.--']

最佳答案

问题是,使用 morse_code.find("") 您只能找到第一个双空格的索引。无法保证您的输入仅包含两个单词。您还需要检测输入字符串中的任何其他双空格。

其次,将此位置除以 3.5 无法可靠地知道在最终翻译的字符串中插入空格的位置。尽管这可能是莫尔斯编码中字母宽度的良好平均值,但有些单词的宽度更接近每个字母 2 个。

您应该通过执行 .split("") 将输入拆分为单词。然后分别解决每个单词的问题,最后将这些单独的翻译连接在一起,中间有一个空格。

如果将问题分解为以下步骤,那就非常简单了:

  • 通过将莫尔斯语句子拆分为莫尔斯语单词(在双空格边界)并单独翻译每个莫尔斯语单词来翻译莫尔斯语句子
  • 通过将单词拆分为莫尔斯字符(在单个空格边界)并单独翻译每个莫尔斯字符来翻译莫尔斯单词
  • 将翻译的字符连接在一起
  • 将翻译的单词连接在一起,并用空格分隔。

这是一个可能的 Python 解决方案:

# helper function to solve the problem for just one Morse-word
def decodeMorseWord(morse_word):
return "".join([MORSE_CODE[morse_letter] for morse_letter in morse_word.split(' ')])

def decodeMorse(morse_code):
return " ".join([decodeMorseWord(morse_word) for morse_word in morse_code.split(' ')])

关于python - 莫尔斯电码程序不处理带空格的测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59179747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com