-
Notifications
You must be signed in to change notification settings - Fork 1
/
phonewords.py
39 lines (30 loc) · 886 Bytes
/
phonewords.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
"""
Phone number to words
Links:
* Phone number to words:
http://www.mobilefish.com/services/phonenumber_words/phonenumber_words.php
"""
from operator import add
DIGIT_TO_WORDS = {
"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",),
}
IGNORE = ("-", "(", ")",)
def _phonewords(numbers):
if not numbers:
return [[]]
else:
return [add([n], x)
for n in DIGIT_TO_WORDS.get(numbers[0], [numbers[0]])
for x in _phonewords(numbers[1:])]
def phonewords(code):
return list(map(lambda seq: "".join(seq),
_phonewords(list(filter(lambda x: x not in IGNORE,
code)))))