-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuntitled0.py
95 lines (64 loc) · 2.65 KB
/
untitled0.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 9 20:58:17 2019
@author: bijayamanandhar
"""
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 7 09:39:19 2019
@author: bijayamanandhar
"""
#Vanity Phone Numbers
"""
Given:
A list(array) of vanity code in alpha characters.
A list of telephone numbers.
Form a list(array) of phone numbers selected to match vanity codes.
Key-pad has alpha characters as:
2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PQRS, 8=TUV, 9=WXYZ.
"""
def match_vanity_phone(codes, numbers):
#dictionary of alpha-chars corresponding to key-numbers
dic = {'2':'ABC', '3':'DEF', '4':'GHI', '5':'JKL',
'6':'MNO', '7':'PQRS', '8':'TUV', '9':'WXYZ'}
#array to collect number converted from code
code_to_num_arr = []
#array to collect selected phone-nos
selected_phones = []
#string to form code-converted number
temp = ''
#iterates thru array of codes
for code in codes:
#checking each chars to convert to number
for i in range(len(code)):
#tracks corresponding number matched with alpha-character
for key in dic:
#matching each character to get corresponding number
if code[i] in dic[key]:
#forming corresponding number to match alpha-code
temp += key
#forming array of code-converted numbers
code_to_num_arr.append(temp)
#after each alpha-code in the array, temp is made empty
temp = ''
#search for matching number to select
for num in code_to_num_arr:
#iterates array of telephone numbers in search of corresponding alpha codes
for number in numbers:
#checks if the alpha code is present as the last string in the number
if num == number[-len(num):]:
#if found, adds to the array
selected_phones.append(number)
return selected_phones
#Test case:
alpha_codes = ['TWILIO', 'HELLO', 'ENGLAND', 'JAPAN', 'ITALY', 'FRANCE']
telephone_numbers = ['+1415996643556', '+291348877773238',
'+38461324372623', '+46457127848259',
'+56536894546', '+63945129952726',
'+77377634254777991', '+8401892383645263']
expected = ['+56536894546', '+1415996643556',
'+8401892383645263', '+63945129952726',
'+46457127848259', '+38461324372623']
print(match_vanity_phone(alpha_codes, telephone_numbers) == expected) #True