Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Count Morse by dig
MORSE = {'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': '--..', '0': '-----',
'1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....',
'7': '--...', '8': '---..', '9': '----.'
}
def morse(text):
return "".join(MORSE[c.lower()] for c in text)
def recursive(message, letters, start, comb):
for letter in letters:
if message.startswith(morse(start+letter)):
start2 = start+letter
letters2 = letters.replace(letter,'')
if len(morse(start2)) == len(message):
comb.append(start2)
recursive(message, letters2, start2, comb)
return comb
def count_morse(message: str, letters: str) -> int:
#print(recursive(message, letters, start='', comb=[])) if you want to see combinations with same morse
return len(recursive(message, letters, start='', comb=[]))
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!")
April 23, 2023