- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
下面是我对 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
最佳答案
这很好地表达为 binary tree :
关于python - 编写面向对象的摩尔斯电码翻译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12208973/
我正在创建一个多区域 AlwaysOn Sql Availability 组。我在东部有 2 个虚拟机,在西部有 1 个虚拟机。我需要使用面向公众的负载平衡器对这些进行负载平衡。是否可以使用存在于不同
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是 Smalltalk 技术的新手。我的编程经验是使用 C 和 C++。我想了解smalltalk的设计方法论。谁能推荐一些用smalltalk开发的简单的实时应用程序,可以帮助像我这样的初学者探索
现在我已经对 web2py 比较熟悉了,我想试试 Django。 主要区别是什么? 考虑到 web2py 知识,最有效的入门方法是什么? (一定有一些python应用框架的知识,不是吗?) 编辑 另外
我之前发过一个问题,Moving away from VSS ,其中我向使用 VSS 的 Delphi 开发人员询问了最佳的 VCS 控件。大多数开发人员似乎将 svn 与 TortoiseSVN 一
引用this question ,收集对 Delphi 初学者真正有帮助的网站链接可能会很好。 第一个答案指向Delphi Basics ,这是一个非常好的网站。 但一定还有更多。 所以:请为这个社区
我已经构建了一个 saas 产品,其中 Angular 4 与 golang Rest api 集成,并将构建上传到了 aws ec2 实例上。我的项目是一个基于 Multi-Tenancy 的应用程
我是 .NET 开发人员,在此之前使用过 VB6。我已经非常熟悉这些环境,并在垃圾收集语言的上下文中工作。但是,我现在希望通过 native C++ 增强我的技能,但发现自己有点不知所措。具有讽刺意味
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
由于我在数学方面没有任何出色的技能,所以我问您是否存在我应该在未来可能会改变的类(class)中使用的算法。 考虑以下场景: “角色”类具有以下字段: private boolean admin; p
我有许多面向“列”的 csv 文件,我需要对其进行预处理才能最终索引它们。 这是面向时间的数据,每个“设备”都有大量的列(最多 128 列),例如: LDEV_XXXXXX.csv
我想就重构技巧的最佳资源展开讨论,着眼于前端 JavaScript 重构。 一个我尊重其意见的 friend 建议这个 book ,尽管它使用 Java 中的示例。我明白 OO 重构的原则应该翻译成另
我不明白如何使用 Jquery/javascript 处理对象。 以及如何创建私有(private)方法/变量?我在论坛上看到关闭,但我尝试过但没有用。以及如何查看方法/变量是否是私有(private
我已经使用 MATLAB 多年并且非常熟练。我习惯了 MATLAB 调试,您可以在其中跳过行并查看变量及其变化方式,这就是我学习编码的方式。我现在正在研究使用 FORTRAN、C++ 和 Python
我正在学习如何实现“单一 Activity - 多个 fragment ”架构。据我所知,这是如今很普遍的方法,你可以找到很多关于这个主题的文章。例如这个 http://vinsol.com/blog
目前是否有任何简单的方法可以在 Apache Tomcat Servlet 容器前面设置 YAWS Web 服务器,类似于流行的 Apache httpd + Tomcat 设置? 最佳答案 我不相信
我是一个相当有能力的 Ruby 脚本编写者/程序员,但一直感到要扩展到 C++ 的压力。我没能找到任何与“C++ for Ruby Programmers”相关的网站。 This site Pytho
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 5 年前。
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我在表上有一个名为 cart_product_fti 的全文索引 每当我使用它时,我都必须准确列出索引中的所有字段: MATCH (foo,bar,etc) 我的想法是,如果稍后我决定向索引添加/删除
我是一名优秀的程序员,十分优秀!