鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: ooxx7788

[技术交流] Python:每日一题 44

[复制链接]
发表于 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[t]
    return ' '.join(s.split())
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-12 09:52:09 | 显示全部楼层
def Fun44():   
    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'}
    inputcode = input("please input morse code:")
    Afterstripcode = inputcode.strip()
    Finalcode = Afterstripcode.rstrip()
    Lmorsecode = Finalcode.split(" ")
    S = ""
    for i in range(len(Lmorsecode)):
        if i!=len(Lmorsecode)-1:
            if Lmorsecode[i]!="":
                S = S + MORSE_CODE[Lmorsecode[i]]
            else:
                S = S + " "
        else:
            S = S + MORSE_CODE[Lmorsecode[i]]
    print(S)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-17 21:26:01 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-17 21:27:58 | 显示全部楼层
class MorseCode:
    def __init__(self):
        self.code_dict = {'.-...': '&', '--..--': ',', '....-': '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 trans(self,s):
        for each_code in s.split(' '):
            print(self.code_dict[each_code],end = ' ')

a = MorseCode()
a.trans('.... . -.-- .--- ..- -.. .')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-28 12:53:58 From FishC Mobile | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-17 19:15:54 | 显示全部楼层
1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-27 10:45:48 | 显示全部楼层

#题目来源:https://fishc.com.cn/forum.php?mod=viewthread&tid=87150
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):
    result=[]
    #去掉头和尾部的空格
    morseCode = morseCode.strip()
    #调用split()方法将传入的morseCode 先按照三个空格进行切片
    lst_three = morseCode.split('   ')
    for i in lst_three:
        # 调用split()方法将传入的morseCode 先按照一个空格进行切片,得到key值
        lst_one = i.split(' ')
        #将lst_one 合并为一个单词
        temp_he = ''
        for j in lst_one:
            temp_he += MORSE_CODE[j]
        result.append(temp_he)

    return ' '.join(result)

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

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.')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-17 11:26:28 | 显示全部楼层
from unittest import result


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'}
    wordListt = morseCode.split("   ")
    print(wordListt)
    result = ''
    for each in wordListt:
        alphabetList = each.split(' ')
        for alphabet in alphabetList:
            if(str(MORSE_CODE.get(alphabet)) == "None"):
                continue

            else:
                result += str(MORSE_CODE.get(alphabet))

        result += ' '

    return result

if __name__ == "__main__":
    target = " ...---... -.-.--   - .... .   --.- ..- .. -.-. -.-   -... .-. --- .-- -.   ..-. --- -..-   .--- ..- -- .--. ...   --- ...- . .-.   - .... .   .-.. .- --.. -.--   -.. --- --. .-.-.-  "
    result = decodeMorse(target)
    print(result)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-28 18:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表