0 like 0 dislike
1,897 views
| 1,897 views 0 like 0 dislike

solution

My solution in Python:

``````mapping = {"0": "O", "1": "I", "2": "Z", "3": "E", "4": "H", "5": "S", "6": "G", "7": "L", "8": "B", "9": "G"}
valid_words = {"ZOO", "HELL"}

def is_valid_word(num, mapping, valid_words) -> bool:
# convert num to corresponding word
word = ''
for i in range(len(num) - 1, -1, -1):
word += mapping[num[i]]

# now, check if word is a valid word.
return word in valid_words``````
by Expert (35,210 points)
0 like 0 dislike

When typing numbers into a calculator, some numbers, when the calculator is flipped upside down, can be read as a word.

Some examples:
"7734" -> "HELL"
"002" -> "ZOO"
(bonus example: "5318008")

Suppose you are also given a mapping of characters to their respective letters that they can be read as when the calculator is upside down:

1 = I, 2 = Z, 3 = E, 4 = H, 5 = S, 7 = L, 8 = B, 9 = G
(6 has no corresponding letter)

## Q: Write a function that takes in a number as the input (that would be typed into the calculator), and a list of valid words, and returns whether the number, when flipped upside, contains a valid word.

Some thoughts:

• Clarify with the interviewer whether the input should be a `string` or a `int`, and discuss how it would be better to have the input be a `string` since `int` would remove leading zeroes, making inputs like `"002"` just become `2`. Also, we are only typing numbers into the calculator and not performing operations, so when you type 002 on a calculator it has not been converted to 2 yet.
• I think this problem is more about how you communicate your thoughts than to pose a difficult coding challenge.
• My solution was O(n) but was asked "are there any optimizations you can think of?" and couldn't think of any. My solution was to parse the input in reverse, convert it to a word based on the given mapping, and then check if it is present in the given list of valid words.
by Expert (35,210 points)