I was just working through problem 6.7, phone mnemonics in the strings chapter and was wondering if my solution (seemingly quite different from the proposed solution) is valid. On average, my solution appears to be more performant:
❯ python epi_judge_python/phone_number_mnemonic.py [10:56:03]
Test PASSED (102/102) [ 566 us]
Average running time: 88 us
Median running time: 37 us
*** You've passed ALL tests. Congratulations! ***
❯ python epi_judge_python_solutions/phone_number_mnemonic.py [10:56:38]
Test PASSED (102/102) [ 1 ms]
Average running time: 264 us
Median running time: 89 us
*** You've passed ALL tests. Congratulations! ***
My implementation:
def phone_mnemonic(phone_number):
phone_dict = {
"0" : ["0"],
"1" : ["1"],
"2" : ['A', 'B', 'C'],
"3" : ['D', 'E', 'F'],
"4" : ['G', 'H', 'I'],
"5" : ['J', 'K', 'L'],
"6" : ['M', 'N', 'O'],
"7" : ['P', 'Q', 'R', 'S'],
"8" : ['T', 'U', 'V'],
"9" : ['W', 'X', 'Y', 'Z']
}
# convert int to list of chars
phone_number = str(phone_number)
results = []
for num in phone_number:
results = __subroutine(results, phone_dict[num])
return results
def __subroutine(strings, chars):
if not strings:
return chars
else:
results = []
for s in strings:
for c in chars:
results.append(s + c)
return results