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).
1 def checkio(time_string): 2 hh_mm_ss = time_string.split(':') 3 4 formal = [] 5 6 #format 7 for each in hh_mm_ss: 8 if len(each) != 2: 9 formal.append('0' + each) 10 else: 11 formal.append(each) 12 13 hhmmss = ''.join(formal) 14 15 limit_len = [2, 4, 3, 4, 3, 4] 16 17 rel = [] 18 19 for pos in range(0, len(hhmmss)): 20 tmp = [] 21 22 binary = bin(int(hhmmss[pos])) 23 24 num_str = binary[2:] 25 26 tmp_len = len(num_str) 27 28 if tmp_len < limit_len[pos]: 29 tmp.append('.' * (limit_len[pos] - tmp_len)) 30 31 for each in num_str: 32 if each == '1': 33 tmp.append('-') 34 else: 35 tmp.append('.') 36 37 rel.append(''.join(tmp)) 38 39 if pos != 5 and pos & 1: 40 rel.append(':') 41 42 return ' '.join(rel)
观摩PositronicLlama的代码
1 """ 2 Convert a time to a pseudo-Morse code. 3 This 'binary-Morse' encoding represents the binary digits of a number as . and - 4 for 0 and 1 respectively. 5 """ 6 7 TO_MORSE = str.maketrans('01', '.-') 8 9 def to_morse(number, bits): 10 """Return number in binary-Morse as a string with the given number of bits.""" 11 return "{0:0{1}b}".format(number, bits).translate(TO_MORSE) 12 13 def to_code(field): 14 """Return a space-delimited string of binary-Morse digits.""" 15 tens, ones = divmod(int(field), 10) 16 return "{} {}".format(to_morse(tens, 3), to_morse(ones, 4)) 17 18 def checkio(data): 19 """Return a string representing the time in a Morse code-like form.""" 20 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转为.