Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
F-Strings solution in Creative category for Morse Clock by scarabeus6
# number of bits for each digit (zero = colon)
FORMAT_PRECISION = [2,4,0,3,4,0,3,4]
def get_bit_value(number: int) -> str:
return f"{number:b}"
def fix_format(item : str, number_of_digits: int) -> str:
return f"{item:0>{number_of_digits}}"
def checkio(time_string: str) -> str:
output = []
numbers = time_string.split(":")
fixed_numbers = []
for number in numbers:
fixed_numbers.append(f"{number:0>2}")
for char in ":".join(fixed_numbers):
if char.isdigit():
output.append(get_bit_value(int(char)))
else:
output.append(char)
for idx in range(len(output)):
output[idx] = fix_format(output[idx], FORMAT_PRECISION[idx])
return " ".join(output).replace("0", ".").replace("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!")
Oct. 1, 2020
Comments: