Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Morse Clock by pawel1337
def to(k, n):
c = int(k)
s = ""
while c != 0:
if c % 2 == 1:
s = '-' + s
else:
s = '.' + s
c //= 2
while len(s) < n:
s = '.' + s
return s
def checkio(t):
out = ''
par = []
i = t.split(':')
for element in i:
if len(element) == 1:
par.append("0" + element)
else:
par.append(element)
return(to(par[0][0], 2) + ' ' + to(par[0][1], 4) + ' : ' + to(par[1][0], 3) + ' ' + to(par[1][1], 4) + ' : ' + to(par[2][0], 3) + ' ' + to(par[2][1], 4))
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"
Nov. 5, 2016