Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5997cc0

Browse files
committed
Phone Number Mnemonics
1 parent 6abb3c8 commit 5997cc0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎Chapter9/phone_number_mnemonics.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# phone_number_mnemonics.py
2+
# From Classic Computer Science Problems in Python Chapter 9
3+
# Copyright 2018 David Kopec
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
from typing import Dict, Tuple, Iterable, List
17+
from itertools import product
18+
19+
phone_mapping: Dict[str, Tuple[str, ...]] = {"1": ("1",),
20+
"2": ("a", "b", "c"),
21+
"3": ("d", "e", "f"),
22+
"4": ("g", "h", "i"),
23+
"5": ("j", "k", "l"),
24+
"6": ("m", "n", "o"),
25+
"7": ("p", "q", "r", "s"),
26+
"8": ("t", "u", "v"),
27+
"9": ("w", "x", "y", "z"),
28+
"0": ("0",)}
29+
30+
31+
def possible_mnemonics(phone_number: str) -> Iterable[Tuple[str, ...]]:
32+
letter_tuples: List[Tuple[str, ...]] = []
33+
for digit in phone_number:
34+
letter_tuples.append(phone_mapping.get(digit, (digit,)))
35+
return product(*letter_tuples)
36+
37+
38+
if __name__ == "__main__":
39+
phone_number: str = input("Enter a phone number:")
40+
print("Here are the potential mnemonics:")
41+
for mnemonic in possible_mnemonics(phone_number):
42+
print("".join(mnemonic))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /