ooxx7788 发表于 2017-5-13 16:35:27

Python:每日一题 44

本帖最后由 ooxx7788 于 2017-5-14 19:35 编辑

今日的题目是制作一个简单的摩斯码解码器。
摩斯码由"."和“-”组成,我在下面也为各位做好了,摩斯码常用的字母,标点的映射表。

MORSE_CODE = {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5', '...---...': 'SOS', '-...': 'B', '-..-': 'X', '.-.': 'R', '.--': 'W', '..---': '2', '.-': 'A', '..': 'I', '..-.': 'F', '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6', '-...-': '=', '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N', '....': 'H', '.----.': "'", '...-': 'V', '--...': '7', '-.-.-.': ';', '-....-': '-', '..--.-': '_', '-.--.-': ')', '-.-.--': '!', '--.': 'G', '--.-': 'Q', '--..': 'Z', '-..-.': '/', '.-.-.': '+', '-.-.': 'C', '---...': ':', '-.--': 'Y', '-': 'T', '.--.-.': '@', '...-..-': '$', '.---': 'J', '-----': '0', '----.': '9', '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'}

摩斯码在实际编写时,字母与字母间由1个空格进行分割,单词和单词间,由3个空格进行分割。例如:
.... . -.--   .--- ..- -.. .
就是HEY JUDE对应的摩斯码

现在请给出函数:
def decodeMorse(morseCode):
    # ToDo: Accept dots, dashes and spaces, return human-readable message


注意:出现在摩斯码首尾的空格均为无效字符。

顺便说一句,这个题目是3题一套,难度递增,这是最简单的第一道。后两道,我会随后放出(内容有点多,翻译有点困难),如果可能你最好先保存这次的代码,将来或许用得上。(其实我也不知道后面用不用得上)

测试:
test.assert_equals(decodeMorse('.... . -.--   .--- ..- -.. .'), 'HEY JUDE')
test.assert_equals(decodeMorse(' . '), 'E')
test.assert_equals(decodeMorse('...---...'), 'SOS')
test.assert_equals(decodeMorse('      ...---... -.-.--   - .... .   --.- ..- .. -.-. -.-   -... .-. --- .-- -.   ..-. --- -..-   .--- ..- -- .--. ...   --- ...- . .-.   - .... .   .-.. .- --.. -.--   -.. --- --. .-.-.-'), 'SOS! THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.')


**** Hidden Message *****

冬雪雪冬 发表于 2017-5-13 17:02:28

第一个交作业。
def decodeMorse(morseCode):
    MORSE_CODE = {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5', '...---...': 'SOS', '-...': 'B', '-..-': 'X', '.-.': 'R', '.--': 'W', '..---': '2', '.-': 'A', '..': 'I', '..-.': 'F', '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6', '-...-': '=', '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N', '....': 'H', '.----.': "'", '...-': 'V', '--...': '7', '-.-.-.': ';', '-....-': '-', '..--.-': '_', '-.--.-': ')', '-.-.--': '!', '--.': 'G', '--.-': 'Q', '--..': 'Z', '-..-.': '/', '.-.-.': '+', '-.-.': 'C', '---...': ':', '-.--': 'Y', '-': 'T', '.--.-.': '@', '...-..-': ', '.---': 'J', '-----': '0', '----.': '9', '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'}
    morseCode = morseCode.strip()
    result = ''
    for word in morseCode.split('   '):
      for char in word.split(' '):
            result += MORSE_CODE
      result += ' '
    return result.rstrip()

ooxx7788 发表于 2017-5-14 19:42:48

冬雪雪冬 发表于 2017-5-13 17:02
第一个交作业。

这题太简单了,试试看45吧。但是我觉得45依然不足以拦住你。

yuzhoudeaomi 发表于 2017-5-14 20:20:59

谢谢谢谢

jerryxjr1220 发表于 2017-5-15 22:43:43

def decodeMorse(morse):
    MORSE_CODE = {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5', '...---...': 'SOS', '-...': 'B', '-..-': 'X', '.-.': 'R', '.--': 'W', '..---': '2', '.-': 'A', '..': 'I', '..-.': 'F', '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6', '-...-': '=', '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N', '....': 'H', '.----.': "'", '...-': 'V', '--...': '7', '-.-.-.': ';', '-....-': '-', '..--.-': '_', '-.--.-': ')', '-.-.--': '!', '--.': 'G', '--.-': 'Q', '--..': 'Z', '-..-.': '/', '.-.-.': '+', '-.-.': 'C', '---...': ':', '-.--': 'Y', '-': 'T', '.--.-.': '@', '...-..-': ', '.---': 'J', '-----': '0', '----.': '9', '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'}
    words = ''
    morselist = morse.strip().split('   ')
    for each in morselist:
      words += ''.join( for e in each.split()])
      words += ' '
    return words.strip()

print(decodeMorse('      ...---... -.-.--   - .... .   --.- ..- .. -.-. -.-   -... .-. --- .-- -.   ..-. --- -..-   .--- ..- -- .--. ...   --- ...- . .-.   - .... .   .-.. .- --.. -.--   -.. --- --. .-.-.-'))

余欲渔 发表于 2017-5-17 19:52:35

MORSE_CODE = {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5', '...---...': 'SOS', '-...': 'B', '-..-': 'X', '.-.': 'R', '.--': 'W', '..---': '2', '.-': 'A', '..': 'I', '..-.': 'F', '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6', '-...-': '=', '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N', '....': 'H', '.----.': "'", '...-': 'V', '--...': '7', '-.-.-.': ';', '-....-': '-', '..--.-': '_', '-.--.-': ')', '-.-.--': '!', '--.': 'G', '--.-': 'Q', '--..': 'Z', '-..-.': '/', '.-.-.': '+', '-.-.': 'C', '---...': ':', '-.--': 'Y', '-': 'T', '.--.-.': '@', '...-..-': ', '.---': 'J', '-----': '0', '----.': '9', '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'}
def decodeMorse(morseCode):
    c=[]
    ci=morseCode.strip().split('   ')
    for i in ci:
      x=i.strip().split(' ')
      for j in x:
            c.append(MORSE_CODE)
      c.append(' ')
    return ''.join(c)
print(decodeMorse('      ...---... -.-.--   - .... .   --.- ..- .. -.-. -.-   -... .-. --- .-- -.   ..-. --- -..-   .--- ..- -- .--. ...   --- ...- . .-.   - .... .   .-.. .- --.. -.--   -.. --- --. .-.-.-'))

ooxx7788 发表于 2017-5-17 19:59:12

余欲渔 发表于 2017-5-17 19:52


代码有个小缺陷,return的时候需要改成
return ''.join(c).strip()
否则最后会多一个空格。

余欲渔 发表于 2017-5-17 20:53:44

ooxx7788 发表于 2017-5-17 19:59
代码有个小缺陷,return的时候需要改成

否则最后会多一个空格。

嗯   确实for i 循环{:5_108:}

solomonxian 发表于 2017-7-4 20:14:31

想到用空格分割之后就很顺了
MORSE_CODE = {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5',\
            '...---...': 'SOS', '-...': 'B', '-..-': 'X', '.-.': 'R', \
            '.--': 'W', '..---': '2', '.-': 'A', '..': 'I', '..-.': 'F',\
            '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', \
            '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6', '-...-': '=', \
            '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N', \
            '....': 'H', '.----.': "'", '...-': 'V', '--...': '7',\
            '-.-.-.': ';', '-....-': '-', '..--.-': '_', '-.--.-': ')',\
            '-.-.--': '!', '--.': 'G', '--.-': 'Q', '--..': 'Z', '-..-.': '/',\
            '.-.-.': '+', '-.-.': 'C', '---...': ':', '-.--': 'Y', '-': 'T',\
            '.--.-.': '@', '...-..-': ', '.---': 'J', '-----': '0', \
            '----.': '9', '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'}

# 用空格分隔字符串,若是空字符串则用空格代替,最后拼接起来,删去多余空格
def decodeMorse(s):
    return "".join().strip().replace(""," ")

shigure_takimi 发表于 2017-12-5 16:10:19

本帖最后由 shigure_takimi 于 2017-12-5 16:11 编辑

def decodeMorse(morseCode):
    morseCode = morseCode.strip().split('   ')
    allWords = []
    for word in morseCode:
      word = word.strip().split(' ')
      word = ''.join( for i in word])
      allWords.append(word)
    return ' '.join(allWords)

# 测试通过。

def assert_equals(func, target, *args):
    if func == target:
      print('Success!')
    else:
      print('Fail!{0} not equals {1}'.format(func, target))
      print(*args)

MORSE_CODE = {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5', '...---...': 'SOS', '-...': 'B', '-..-': 'X', '.-.': 'R', '.--': 'W', '..---': '2', '.-': 'A', '..': 'I', '..-.': 'F', '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6', '-...-': '=', '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N', '....': 'H', '.----.': "'", '...-': 'V', '--...': '7', '-.-.-.': ';', '-....-': '-', '..--.-': '_', '-.--.-': ')', '-.-.--': '!', '--.': 'G', '--.-': 'Q', '--..': 'Z', '-..-.': '/', '.-.-.': '+', '-.-.': 'C', '---...': ':', '-.--': 'Y', '-': 'T', '.--.-.': '@', '...-..-': '
, '.---': 'J', '-----': '0', '----.': '9', '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'}


def decodeMorse(morseCode):
    morseCode = morseCode.strip().split('   ')
    allWords = []
    for word in morseCode:
      word = word.strip().split(' ')
      word = ''.join( for i in word])
      allWords.append(word)
    return ' '.join(allWords)


assert_equals(decodeMorse('.... . -.--   .--- ..- -.. .'), 'HEY JUDE')
assert_equals(decodeMorse(' . '), 'E')
assert_equals(decodeMorse('...---...'), 'SOS')
assert_equals(decodeMorse('      ...---... -.-.--   - .... .   --.- ..- .. -.-. -.-   -... .-. --- .-- -.   ..-. --- -..-   .--- ..- -- .--. ...   --- ...- . .-.   - .... .   .-.. .- --.. -.--   -.. --- --. .-.-.-'), 'SOS! THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.')   
   

shigure_takimi 发表于 2017-12-5 16:32:14

本帖最后由 shigure_takimi 于 2017-12-6 08:13 编辑

MORSE_CODE = {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5', \
            '...---...': 'SOS', '-...': 'B', '-..-': 'X', '.-.': 'R', \
            '.--': 'W', '..---': '2', '.-': 'A', '..': 'I', '..-.': 'F', \
            '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', \
            '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6', '-...-': '=', \
            '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N', \
            '....': 'H', '.----.': "'", '...-': 'V', '--...': '7', \
            '-.-.-.': ';', '-....-': '-', '..--.-': '_', '-.--.-': ')', \
            '-.-.--': '!', '--.': 'G', '--.-': 'Q', '--..': 'Z', '-..-.': '/', \
            '.-.-.': '+', '-.-.': 'C', '---...': ':', '-.--': 'Y', '-': 'T', \
            '.--.-.': '@', '...-..-': ', '.---': 'J', '-----': '0', \
            '----.': '9', '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'}

charToMorse = {}
for key in MORSE_CODE:
    charToMorse] = key
   


def getMorseCode(string):
    string = string.upper().strip().split(' ')
    eachMorse = []
    for word in string:
      if word == 'SOS':
            eachMorse.append(charToMorse)
      else:
            wordMorse = []
            for char in word:
                wordMorse.append(charToMorse)
            wordMorse = ' '.join(wordMorse)
            eachMorse.append(wordMorse)
    return '   '.join(eachMorse)

def decodeMorse(morseCode):
    morseCode = morseCode.strip().split('   ')
    allWords = []
    for word in morseCode:
      word = word.strip().split(' ')
      word = ''.join( for i in word])
      allWords.append(word)
    return ' '.join(allWords)

a = '''this is a story about a girl named lucky. she said I love you, but the boy didn't love her.'''
b = getMorseCode(a)
print(b)
print(decodeMorse(b))

print(getMorseCode('SOS'))
print(decodeMorse('...---...'))
print(decodeMorse('... --- ...'))


## 得到密码和解码都有了。
- .... .. ...   .. ...   .-   ... - --- .-. -.--   .- -... --- ..- -   .-   --. .. .-. .-..   -. .- -- . -..   .-.. ..- -.-. -.- .-.-.-   ... .... .   ... .- .. -..   ..   .-.. --- ...- .   -.-- --- ..- --..--   -... ..- -   - .... .   -... --- -.--   -.. .. -.. -. .----. -   .-.. --- ...- .   .... . .-. .-.-.-
THIS IS A STORY ABOUT A GIRL NAMED LUCK. SHE SAID I LOVE YOU, BUT THE BOY DIDN'T LOVE HER.

yjsx86 发表于 2018-1-22 00:02:10

def func(string):
    L = string.strip().split('   ')
    rs = ''
    for x in L:
      rs += ''.join( for y in x.split()])
      rs += ' '
    return rs.strip()

mike0724 发表于 2018-1-24 00:07:24

def decodeMorse(code):
    #去除首尾的无效空格
    words = code.strip().split("   ") #再把三个空格分隔的单词分开
    mdecode = ''
    for i in range(len(words)): #对每个单词,再将用一个空格分开的元素分开
      word = words.strip().split(" ")
      for j in range(len(word)): #针对每个元素去解码
            mdecode += (MORSE_CODE]) #将单词里的每个元素解码以后接在一起
      mdecode +=' ' #一个单词接完以后先接一个空格再接后面单词\
    mdecode = mdecode.strip()
    return mdecode

凌九霄 发表于 2018-4-12 09:55:36

def codetranslation(cstr):
    codestr = cstr.split(' ')
    chars = ''
    if len(codestr) > 0:
      for i in codestr:
            if i == '':
                chars += ' '
            else:
                chars += MORSE_CODE
    return chars

天下独我意 发表于 2018-6-15 21:14:02

我的答案:
def msmm(str_n):
      n = str_n
      f = {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5','...---...': 'SOS',
         '-...': 'B', '-..-': 'X', '.-.': 'R', '.--': 'W', '..---': '2', '.-': 'A', '..': 'I',
         '..-.': 'F', '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6',
         '-...-': '=', '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N', '....': 'H', '.----.': "'", '...-': 'V', '--...': '7',
         '-.-.-.': ';', '-....-': '-', '..--.-': '_', '-.--.-': ')', '-.-.--': '!', '--.': 'G', '--.-': 'Q', '--..': 'Z', '-..-.': '/',
         '.-.-.': '+', '-.-.': 'C', '---...': ':', '-.--': 'Y', '-': 'T', '.--.-.': '@', '...-..-': ', '.---': 'J', '-----': '0', '----.': '9',
         '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'}
      n = n.strip()
      k = n.split(' ')
      z = []
      g = ''
      for jkl in k:
            z.append(f)
      for jkl in z:
            if jkl.isdigit():
                  g += jkl
            elif len(jkl) == 1:
                  g += ' '+jkl
            else:
                  g += ' '+jkl+' '
      return g.strip()

萧丹夜 发表于 2018-6-29 18:05:00

成功!
def decodeMorse(morseCode):
    result = ''
    keys = []
    values = []
    morseCode = morseCode.strip()       ## 去掉首尾空格
    words = morseCode.split('   ')       ## 单词切割
    for each in MORSE_CODE.keys():      ## 字母
      keys.append(each)
    for each in MORSE_CODE.values():    ## 摩斯码
      values.append(each)
      
    for each in words:                   ## 单词
      each = each.split(' ')
      for every in each:            ## 字母
            result += values
      result += ' '
    result = result.strip()
    return result

小强工作室 发表于 2018-7-22 10:38:56

学习中

jrro452 发表于 2018-11-1 17:43:44

def decodeMorse(morseCode):
    morseCode = morseCode.strip(" ")
    list1 = morseCode.split("   ")
    list2 = []
    result = []
    fresult = ""
    for each in list1:
      list2.append(each.split(" "))
    for eacha in list2:
      for eachb in eacha:
            result.append(MORSE_CODE)
      result.append(" ")
    return "".join(result).rstrip()
测试没问题。

咕咕鸡鸽鸽 发表于 2019-1-8 17:39:41

kankan

yu123py 发表于 2019-5-5 22:33:55

到底还要翻过多少座山,才能得到更多的鱼币?
def decodeMorse(morseCode):
    MORSE_CODE = {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5', '...---...': 'SOS', '-...': 'B', '-..-': 'X', '.-.': 'R', '.--': 'W', '..---': '2', '.-': 'A', '..': 'I', '..-.': 'F', '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?', '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6', '-...-': '=', '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N', '....': 'H', '.----.': "'", '...-': 'V', '--...': '7', '-.-.-.': ';', '-....-': '-', '..--.-': '_', '-.--.-': ')', '-.-.--': '!', '--.': 'G', '--.-': 'Q', '--..': 'Z', '-..-.': '/', '.-.-.': '+', '-.-.': 'C', '---...': ':', '-.--': 'Y', '-': 'T', '.--.-.': '@', '...-..-': ', '.---': 'J', '-----': '0', '----.': '9', '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'}
    s = ''
    s1 = ''
    for t in morseCode.split(' '):
      if t == '':
            s += ' '
      else:
            s += MORSE_CODE
    return ' '.join(s.split())
页: [1] 2
查看完整版本: Python:每日一题 44