Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Morse Clock by Rounin
import re
def checkio(time_string):
time_string = re.split('[^0-9]', time_string)
out = []
for i in range(len(time_string)):
number = list(('' if len(time_string[i]) == 2 else '0') + time_string[i])
for d in range(len(number)):
number[d] = bin(int(number[d]))[2:].replace('0', '.').replace('1', '-')
targetLength = 3 + d - (1 if i == 0 and d == 0 else 0)
while len(number[d]) < targetLength:
number[d] = '.' + number[d]
out.append(' '.join(number))
return ' : '.join(out)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio("10:37:49") == ".- .... : .-- .--- : -.. -..-", "First Test"
assert checkio("21:34:56") == "-. ...- : .-- .-.. : -.- .--.", "Second Test"
assert checkio("00:1:02") == ".. .... : ... ...- : ... ..-.", "Third Test"
assert checkio("23:59:59") == "-. ..-- : -.- -..- : -.- -..-", "Fourth Test"
July 21, 2017