繁体   English   中英

您好,请有人解释一下我如何才能制定解决方案以将完整的“Hello World”加密为莫尔斯电码?

[英]Hello there, can pls someone explain me how exactly I can make a solution to get the full "Hello World" encrypted into the morse code?

def encode( text, code_table, usecaps=True, insep="", outsep=" "):
    for x in text:
        if x != outsep:
            insep += code_table[x] + outsep
        return(code_table[x])

print(encode("Hello World", {'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': '--..',

        '0': '-----', '1': '.----', '2': '..---',
        '3': '...--', '4': '....-', '5': '.....',
        '6': '-....', '7': '--...', '8': '---..',
        '9': '----.'
        }))

这是使用(生成器)理解和dict.get的一种方法; 我不确定insepoutsep应该是什么意思。

def encode(text, code_table):
    return ''.join(code_table.get(c.upper(), c) for c in text)

# output: ......-...-..--- .-----.-..-..-..

请注意,如果c.upper()不在字典code_table中,则 code_table.get code_table.get(..., c)按原样返回输入字符c 特别是,将保留空格字符' '

实现此目的的另一种方法

#!/usr/bin/env python

code_dict = {
    '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': '--..',
    '1': '.----', '2': '..---', '3': '...--',
    '4': '....-', '5': '.....', '6': '-....',
    '7': '--...', '8': '---..', '9': '----.',
    '0': '-----', ', ': '--..--', '.': '.-.-.-',
    '?': '..--..', '/': '-..-.', '-': '-....-',
    '(': '-.--.', ')': '-.--.-', " ": " "
}


def encode(text, code_dictionary):
    # Create empty results variable to be returned once finished processing
    results = ""
    # For each in character in text.upper(). This will make the text uppercase so we can match our dictionary
    for character in text.upper():
        # This will append the value (morris code) from the character (key) from the morris code dictionary defined above 
        results += code_dictionary[character] + " "
    return results

#print the results of the definition
print(encode("Hello World", code_dict))

您要完成的是在字典中查找每个字母。 更多关于字典的信息也可以在这里找到

请注意,我在code_dict变量中添加了一个空格,因此我们可以在您的编码定义中进行评估时将空格与空格匹配。

可能有几千种方法可以完成您正在尝试做的事情。 这将帮助您了解如何在字典中查找键,使用for 循环将它们附加到变量中。

您应该尝试解释您的代码不工作的方式。 但是,我怀疑问题在于您的return语句是缩进的,因此它位于for循环内。 这意味着 function 将在处理完第一个字符后返回。 您应该取消缩进return ,以便在for循环完成后执行。

这是一个简单的方法。 您可以遍历 hw 字符串并检查每个字母是否是 morse_dict 中的键。 我添加了 .upper() 字符串方法来解释包含大小写字母混合的字符串,而所有 dict 键都是大写的。

morse_dict = {'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': '--..',
    
            '0': '-----', '1': '.----', '2': '..---',
            '3': '...--', '4': '....-', '5': '.....',
            '6': '-....', '7': '--...', '8': '---..',
            '9': '----.'
            }
    
hw = 'Hello World'

for x in hw:
    if x.upper() in morse_dict:
        print(x, morse_dict[x.upper()])
    else:
        print(' ')

H ....
e .
l .-..
l .-..
o ---
 
W .--
o ---
r .-.
l .-..
d -..

好的,已经寻求帮助了,这是我在这里的第一个问题,我对 python 还很陌生,并且在简单的事情上有点挣扎,但我会注意未来。 我最大的问题是代码需要用确切的参数来解决。 insep 和 outsep 应该定义输入和 output 分隔符,并且必须使用默认值备份。 我无法在我的代码中真正构建您的答案,并且努力解决上面代码中大写字母的问题,并且不使用以下每个字母覆盖 output。

Hrm ...假设输入/输出分隔符可能是这样的。

#!/usr/bin/env python

code_dict = {
    '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': '--..',
    '1': '.----', '2': '..---', '3': '...--',
    '4': '....-', '5': '.....', '6': '-....',
    '7': '--...', '8': '---..', '9': '----.',
    '0': '-----', ', ': '--..--', '.': '.-.-.-',
    '?': '..--..', '/': '-..-.', '-': '-....-',
    '(': '-.--.', ')': '-.--.-', " ": " "
}


def encode( text, code_table, usecaps=True, insep="", outsep=" "):
    if usecaps:
        text = text.upper()
    else:
        return 'use caps must be true'
    for x in text:
        if x != outsep:
            insep += code_table[x] + outsep
    return insep

#print the results of the definition
print(encode("Hello World", code_dict))

在 for 循环中返回code_table[x]只会返回第一个匹配项。 这是由于返回中断了 for 循环。

祝你学习 python 好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM