Morse Clock
Morse Clock
"di-dah di-di-di-dit di-dah-dah di-dah-dah-dah dah-di-dit dah-di-di-dah", sound of Morszelizer clanked out loud.
"What're you doing?" Nikola asked curiously.
"I'm sending our time logs for the last expedition to headquarters, but it's not an easy task..." Stephen grumbled, "Can you imagine that with all the computer power at our disposal, I STILL have to convert this message to Morse-code with only an on/off button... Hrmph... what a pain." He grumbled at the inconvenience.
"Let me look at it." Nikola offered his help, "It looks like a pretty easy solution, we could automate the process."
"Oh.. you hero of my day." Stephen started excitedly. "So, how do we start it?"
"With Python!" Nikola exclaimed.
Help Stephen to create a module for converting a normal time string to a morse time string. As you can see in the illustration, a gray circle means on, while a white circle means off. Every digit in the time string contains a different number of slots. The first digit for the hours has a length of 2 while the second digit for the hour has a length of 4. The first digits for the minutes and seconds have a length of 3 while the second digits for the minutes and seconds have a length of 4. Every digit in the time is converted to binary representation. You will convert every on (or 1) signal to dash ("-") and every off (or 0) signal to dot (".").
An time string could be in the follow formats: "hh:mm:ss", "h:m:s" or "hh:m:ss". The "missing" digits are zeroes. For example, "1:2:3" is the same as "01:02:03".
"h h : m m : s s"
where each digits represented as sequence of "." and "-"
Input: A normal time string as a string (unicode).
def checkio(time_string):
hh_mm_ss = time_string.split(':') formal = [] #format
for each in hh_mm_ss:
if len(each) != 2:
formal.append('' + each)
else:
formal.append(each) hhmmss = ''.join(formal) limit_len = [2, 4, 3, 4, 3, 4] rel = [] for pos in range(0, len(hhmmss)):
tmp = [] binary = bin(int(hhmmss[pos])) num_str = binary[2:] tmp_len = len(num_str) if tmp_len < limit_len[pos]:
tmp.append('.' * (limit_len[pos] - tmp_len)) for each in num_str:
if each == '':
tmp.append('-')
else:
tmp.append('.') rel.append(''.join(tmp)) if pos != 5 and pos & 1:
rel.append(':') return ' '.join(rel)
观摩PositronicLlama的代码
"""
Convert a time to a pseudo-Morse code.
This 'binary-Morse' encoding represents the binary digits of a number as . and -
for 0 and 1 respectively.
""" TO_MORSE = str.maketrans('', '.-') def to_morse(number, bits):
"""Return number in binary-Morse as a string with the given number of bits."""
return "{0:0{1}b}".format(number, bits).translate(TO_MORSE) def to_code(field):
"""Return a space-delimited string of binary-Morse digits."""
tens, ones = divmod(int(field), 10)
return "{} {}".format(to_morse(tens, 3), to_morse(ones, 4)) def checkio(data):
"""Return a string representing the time in a Morse code-like form."""
return ' : '.join(map(to_code, data.split(':')))[1:] # Strip leading .
str.maketrans()是静态方法, 第七行设置str.translate的替换规则, 即在str.translate方法中将0替换为-, 将1替换为.
"{0:0{1}b}".format(number, bits), 嵌套式格式控制, 0{1}b, 表示左对齐{1}, 以0填充, b二进制表示, 对{0}的格式控制, 详情参考http://blog.csdn.net/xiaofeng_yan/article/details/6648493
translate(), 将二进制形式中的0转为-, 1转为.
Morse Clock的更多相关文章
- 修改Linux系统日期与时间date clock
先设置日期 date -s 20080103 再设置时间 date -s 18:24:30 为了永久生效,需要将修改的时间写入CMOS. 查看CMOS的时间: #clock -r 将当前系统时间写到C ...
- 操作系统页面置换算法(opt,lru,fifo,clock)实现
选择调出页面的算法就称为页面置换算法.好的页面置换算法应有较低的页面更换频率,也就是说,应将以后不会再访问或者以后较长时间内不会再访问的页面先调出. 常见的置换算法有以下四种(以下来自操作系统课本). ...
- Cesium应用篇:3控件(1)Clock
创建 跟Clock相关的主要有Animation控件和Timeline控件,通常两者会放在一起使用. 在Cesium中,Viewer默认开启这两个控件,如果你想要不显示控件,可以在Viewer初始化中 ...
- morse code
morse code,摩斯电码,是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号. 摩斯电码,是一种早期的数字化通信形式,但是它不同于现代只使用0和1两种状态的二进制代 ...
- get back to the slower clock rate that allows it to save more power
http://www.howtogeek.com/177790/why-you-cant-use-cpu-clock-speed-to-compare-computer-performance/ Wh ...
- Clock rate
https://en.wikipedia.org/wiki/Clock_rate The clock rate typically refers to the frequency at which a ...
- clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别【转】
转自:http://www.cnblogs.com/krythur/archive/2013/02/25/2932647.html 转自http://blog.sina.com.cn/s/blog_7 ...
- 最少clock
var elClock = document.getElementById("clock");var getTime = function(){ var _ = ['00','01 ...
- 用clock()函数计算多项式的运行时间
百度百科中定义clock():clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下: clock_t clock(void) ; 简 ...
随机推荐
- zoj3640:概率(期望)dp
题目大意:有一个吸血鬼,初始攻击力为f,每天随机走到n个洞里面,每个洞有一个c[i],如果他的攻击力f>c[i] 则可以花费t[i] 的时间逃走,否则则花费一天时间使自己的攻击力增加c[i],求 ...
- 【剑指offer】面试题37:两个链表的第一个公共结点
题目: 输入两个链表,找出它们的第一个公共结点. 思路: 由链表的定义知是单链表.对于单链表,如果两个链表有公共结点,则两个链表必然是像Y型相交.则先计算出各个链表的长度,让长链表的头指针先走多出来的 ...
- Python安装MySQLdb并连接MySQL数据库
当然了,前提是你已经安装了Python和MySQL.我的Python是2.6版本的. Python2.6的“Set”有点兼容性问题,自己照着改一下: http://sourceforge.net/fo ...
- [原创作品]手把手教你怎么写jQuery插件
这次随笔,向大家介绍如何编写jQuery插件.啰嗦一下,很希望各位IT界的‘攻城狮’们能和大家一起分享,一起成长.点击左边我头像下边的“加入qq群”,一起分享,一起交流,当然,可以一起吹水.哈,不废话 ...
- 20 个非常棒的jQuery内容滑动插件
Wow Slider WOW Slider是一款小巧易用的网页滑块设计.该软件内置大量的模版和工具,让你轻松设计出完美的视觉效果.他还可以帮助用户在短时间内创造出梦幻般的滑块,而无需编码和图像编辑, ...
- VMware vSphere 5.5的12个更新亮点(1)
[IT专家网虚拟化]在VMworld 2013大会上发布的VMware vSphere 5.5版本提供的增强和改进,横跨从hypervisor到管理整个堆栈,提升了VMware的性能.可伸缩性和可用性 ...
- TBB入门
获取TBB TBB的官方网站在http://threadingbuildingblocks.org/,可以在它的Downloads页面里找到Commercial Aligned Release,最新版 ...
- hdu 1240 Asteroids! (三维bfs)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- (转)2.4.1 基础知识--添加服务引用与Web引用的区别
<Web服务开发学习实录>第2章构建ASP.NET Web服务,本章我们将学习创建Web服务的各种方法,并重点对使用Visual Studio创建ASP.NET Web服务和修改Web服务 ...
- respondsToSelector的使用
- (BOOL)respondsToSelector:(SEL)aSelector; 用来判断是否有以某个名字命名的方法 +(BOOL) instancesRespondToSelector: sel ...