Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Morse Clock by werepire
def checkio(time_string):
def dec2bin(n):
bin=""
while(n>0):
bin=str(n%2)+bin
n=n//2
return bin
def dec2morse(n, morse):
s=dec2bin(n)
i=len(s)-1
length=len(morse)
j=length-1
while j>=0 and i>=0:
if s[i]=='1':
morse=morse[0:j]+'-'+morse[j+1:length]
i=i-1
j=j-1
return morse
morse=["..", "....", "...", "....", "...", "...."]
time=time_string.split(':')
i=0 #index of morse[]
j=0 #index of time[]
while i<6:
if len(time[j])==1:
i=i+1
d=int(time[j])
morse[i]=dec2morse(d, morse[i])
else:
d=int(time[j][0])
morse[i]=dec2morse(d, morse[i])
i=i+1
d=int(time[j][1])
morse[i]=dec2morse(d, morse[i])
i=i+1
j=j+1
res=morse[0]+' '+morse[1]+' : '+morse[2]+' '+morse[3]+' : '+morse[4]+' '+morse[5]
return res
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. 4, 2016