Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Morse Clock by nubatamax
widths = ((2, 4), (3, 4), (3, 4))
def morsenize(numstr, width):
binstr = ' '.join([format(int(c), '0' + str(w) + 'b')
for (c, w) in zip(numstr, width)])
return binstr.replace('0', '.').replace('1', '-')
def checkio(timestr):
hms = list(map(int, timestr.split(':')))
bcd_strs = list(map(lambda x: format(x, '02d'), hms))
morse_strs = [morsenize(numstr, width)
for (numstr, width) in zip(bcd_strs, widths)]
return ' : '.join(morse_strs)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
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"
Feb. 26, 2014