Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Morse Clock by Nuszwkartlo
def checkio(time_string):
if (time_string[1] == ':'):
time_string = '0' + time_string
if (time_string[4] == ':'):
time_string = time_string[:3] + '0' + time_string[3:]
if (len(time_string) == 7):
time_string = time_string[:6] + '0' + time_string[6:]
return (DigitToBeeps(int(time_string[0]),2) + ' ' + DigitToBeeps(int(time_string[1]),4) + ' : ' +
DigitToBeeps(int(time_string[3]),3) + ' ' + DigitToBeeps(int(time_string[4]),4) + ' : ' +
DigitToBeeps(int(time_string[6]),3) + ' ' + DigitToBeeps(int(time_string[7]),4))
def DigitToBeeps(digit, precision):
ret = ''
if precision == 4:
if digit >= 8:
ret = ret + '-'
digit = digit - 8
else:
ret = ret + '.'
if (precision == 4 or precision ==3):
if digit >= 4:
ret = ret + '-'
digit = digit - 4
else:
ret = ret + '.'
if digit >= 2:
ret = ret + '-'
digit = digit - 2
else:
ret = ret + '.'
if digit == 1:
ret = ret + '-'
else:
ret = ret + '.'
return ret
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"
Jan. 20, 2016