【问题标题】:Writing an Object Oriented morse code translator编写面向对象的莫尔斯电码翻译器
【发布时间】:2012-08-25 21:29:30
【问题描述】:

以下是我对 reddit.com/r/dailyprogrammer #93 的解决方案(简单)。这是一个在英语和摩尔斯电码之间转换的python脚本。

String_To_Translate 是一个类的好例子吗?我违反约定了吗?如何改进此代码并使其更具可读性和 Python 风格?

非常感谢任何建议!

import re

morse_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':'-----',
                    ',':'--..--',
                    '.':'.-.-.-',
                    '?':'..--..',
                    '/':'-..-.',
                    '-':'-....-',
                    '(':'-.--.',
                    ')':'-.--.-',
                    ' ':' ',
                    }


class String_To_Translate:
    def __init__(self, string):
        self.string = string
        self.Translated = ''
        self.lang = ''

    def determine_lang(self):
        exp = r'[a-zA-Z\,\?\/\(\)]'#regexp for non morse code char
        if re.search(exp, self.string) == None:
            self.lang = 'm'
        else:
            self.lang = 'eng'

    def prep(self):
        if self.lang == 'eng':
            #get rid of whitespace & warn user
            (self.string, numWhitespaceReplacements) = re.subn(r'\s', ' ', self.string)
            if numWhitespaceReplacements > 0:
                print 'Some whitespace characters have been replaced with spaces.'
            #get rid of characters not in dict & warn user
            (self.string, numReplacements) = re.subn(r'[^a-zA-Z\,\?\/\(\)\.\-\s]', ' ', self.string)
            if numReplacements > 0:
                print 'Some ({0}) characters were unrecognized and could not be translated, they have been replaced by spaces.'.format(numReplacements)
            #convert to lowercase
            self.string = self.string.lower()

    def translate_from_morse(self):
        eng_dict = dict([(v, k) for (k, v) in morse_code_dict.iteritems()])

        def split_morse(string):
            d = '  '
            charList = [[char for char in word.split() + [' ']] for word in string.split(d) if word != '']
            charList = [item for sublist in charList for item in sublist]
            print charList
            return charList

        charList = split_morse(self.string)
        for char in charList:
            self.Translated += eng_dict[char]

    def translate_from_eng(self):
        charList=list(self.string)
        for char in charList:
            self.Translated += morse_code_dict[char] + ' '

    def translate(self):
        self.determine_lang()
        if self.lang == 'm':
            self.translate_from_morse()
        elif self.lang == 'eng':
            self.prep()
            self.translate_from_eng()
        else:
            print 'shit'

if __name__=="__main__":
    translateme = String_To_Translate(raw_input('enter a string: '))
    translateme.translate()
    print translateme.Translated
    print 'translating back'
    test = String_To_Translate(translateme.Translated)
    test.translate()
    print test.Translated

【问题讨论】:

  • 当我需要将状态与行为结合起来时,我会去寻找对象。这个任务似乎都是关于行为的。
  • 您可以使用pylint 之类的工具根据 PEP8 标准对您的代码进行评分......并在需要的地方进行修改......评分为 8 及以上可以认为是好的
  • 您应该尝试将其发布到 codereview.stackexchange.com。
  • @julian cool 不知道 codereview.stackexchange.com

标签: python class reddit


【解决方案1】:

这很好地表达为binary tree:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多