Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Morse Clock by marcelina.gorzelana
def dec2bin(n):
res = ""
while n!=0:
res = str(n%2) + res
n//= 2
return res
def checkio(time_string):
res = ""
time = time_string.split(':')
if len(time[0]) == 1:
time[0] = '0' + time[0]
bin = dec2bin(int(time[0][0]))
while len(bin) < 2:
bin = '0' + bin
for b in bin:
if b == '1':
res+= '-'
else:
res+= '.'
res+= ' '
bin = dec2bin(int(time[0][1]))
while len(bin) < 4:
bin = '0' + bin
for b in bin:
if b == '1':
res+= '-'
else:
res+= '.'
res+= " : "
for i in range(1, 3):
if len(time[i]) == 1:
time[i] = '0' + time[i]
bin = dec2bin(int(time[i][0]))
while len(bin) < 3:
bin = '0' + bin
for b in bin:
if b == '1':
res+= '-'
else:
res+= '.'
res+= ' '
bin = dec2bin(int(time[i][1]))
while len(bin) < 4:
bin = '0' + bin
for b in bin:
if b == '1':
res+= '-'
else:
res+= '.'
res+= " : "
res2 = res[ : len(res) - 3]
return res2
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. 25, 2016