Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Morse Clock by Calen
# create a dictionary where to each digit assigned its pseudo-morse code:
# transtlate digit to binary, remove '0b' preffix, fill from the right with zeroes
# up to 4 symbols, replace zeroes with dots and ones with dashes
code = {x: bin(int(x))[2:].zfill(4).replace('0', '.').replace('1', '-') for x in "0123456789"}
def checkio(time_string: str) -> str:
time = time_string.split(':')
time = [' '.join([code[d] for d in x.zfill(2)])[1:] for x in time]
return ' : '.join(time)[1:]
if __name__ == '__main__':
print("Example:")
print(checkio("10:37:49"))
#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"
print("Coding complete? Click 'Check' to earn cool rewards!")
March 19, 2021