Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Morse Clock by tomasz.pucka
def binary(x, pos, element_number):
bin_string=""
while(x>0):
bin_string+=str(x%2)
x=x//2
bin_string=bin_string[::-1]
while(element_number==0 and pos==0 and len(bin_string)<2): bin_string="0"+bin_string
while((element_number==0 or element_number==1 or element_number==2) and pos==1 and len(bin_string)<4): bin_string="0"+bin_string
while((element_number==1 or element_number==2) and pos==0 and len(bin_string)<3): bin_string="0"+bin_string
return bin_string
def checkio(time_string):
element_number=-1
morse_string=""
time_list=time_string.split(":")
counter=-1
for element in time_list:
counter+=1
if(len(element)==1):
time_list[counter]="0"+time_list[counter]
for element in time_list:
pos=0
element_number+=1
while(pos<2):
binary_string=binary(int(element[pos]), pos, element_number)
morse_string+=binary_string.replace("1", "-").replace("0", ".")+" "
pos+=1
morse_string+=": "
return morse_string[:len(morse_string)-3]
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"
Oct. 29, 2016