PythonでMorse Clock

f:id:g_YUYUYU:20140602043449p:plain

コード

def checkio(time_string):
	s = time_string.encode("utf-8").split(":")
	b = ""
	for i in s:
		i = i.zfill(2)
		li = list(i)
		if b == "":
			b = b + format(int(li[0]),"b").zfill(2) + " "
		else:
			b = b + format(int(li[0]),"b").zfill(3) + " "
		b = b + format(int(li[1]),"b").zfill(4) + " : "
	b = b.replace("1","-").replace("0",".")

	print b[:-3]


if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
	#checkio(u"10:37:49") #== ".- .... : .-- .--- : -.. -..-", "First Test"
	#checkio(u"21:34:56")# == "-. ...- : .-- .-.. : -.- .--.", "Second Test"
	checkio(u"00:1:02") #== ".. .... : ... ...- : ... ..-.", "Third Test"
	#checkio(u"23:59:59") #== "-. ..-- : -.- -..- : -.- -..-", "Fourth Test"

一言

  • 継続は力なり
  • 工夫したところは、HHの最初だけzfill(2)で0詰めした所
  • 工夫したところは、li = list(i)で二桁の数字を一桁ずつのリストに分割した所