Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Morse Clock by yingr1208
def checkio(time_string):
nums = []
for i in time_string:
if i !=':' :
nums.append(int(i))
elif len(nums)% 2 == 1 :
nums.append(nums[-1])
nums[-2] = 0
if len(nums) % 2 == 1:
nums.append(nums[-1])
nums[-2] = 0
ans = ''
for j in range(len(nums)):
t = str(bin(nums[j]))
if j % 2==0:
if j == 0:
if len(t)<4:
for x in range(len(t), 4):
ans += '.'
else:
if len(t)<5:
for x in range(len(t), 5):
ans +='.'
else:
if len(t)<6:
for x in range(len(t),6):
ans += '.'
for k in t[2:len(t)]:
if k == '1':
ans += '-'
else:
ans += '.'
ans += ' '
if j % 2 != 0:
ans += ': '
return(ans[:-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"
assert checkio("0:10:2") == ".. .... : ..- .... : ... ..-.", "Fifth Test"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
Sept. 20, 2016