Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
lambda, recursion solution in Clear category for Count Morse by Max0526
D = {
"a": ".-",
"b": "-...",
"c": "-.-.",
"d": "-..",
"e": ".",
"f": "..-.",
"g": "--.",
"h": "....",
"i": "..",
"j": ".---",
"k": "-.-",
"l": ".-..",
"m": "--",
"n": "-.",
"o": "---",
"p": ".--.",
"q": "--.-",
"r": ".-.",
"s": "...",
"t": "-",
"u": "..-",
"v": "...-",
"w": ".--",
"x": "-..-",
"y": "-.--",
"z": "--..",
}
count_morse = lambda mes, let: 1 if not let else sum(count_morse(mes.removeprefix(D[s]), let.replace(s, '')) \
for s in let if mes.startswith(D[s]))
print("Example:")
print(count_morse("-------.", "omg"))
# These "asserts" are used for self-checking
assert count_morse("-------.", "omg") == 2
assert count_morse(".....-.-----", "morse") == 4
assert count_morse("-..----.......-..-.", "xtmisuf") == 4
print("The mission is done! Click 'Check Solution' to earn rewards!")
May 5, 2023