Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Morse Clock by robertabegg
def morse_digit(i: int, length: int) -> str:
""" Convert int to morse code string with len of length. """
return "".join(['-' if on else '.' for on in
[(i >> x) & 1 for x in reversed(range(length))]])
def checkio(time_string: str) -> str:
hh, mm, ss = map(int, time_string.split(':'))
morse_time = "{} {} : {} {} : {} {}".format(
morse_digit(hh // 10, 2), morse_digit(hh % 10, 4),
morse_digit(mm // 10, 3), morse_digit(mm % 10, 4),
morse_digit(ss // 10, 3), morse_digit(ss % 10, 4)
)
return morse_time
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!")
Sept. 26, 2021
Comments: