My code is
def search(n):
lis = []
pad = {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'], 0: ' '}
for i in n:
for key,values in pad.items():
if i.upper() in values:
lis.append(key)
return lis
def main():
m = input("Enter String")
print(search(m))
main()
my output is
Enter String - hello world
[4, 3, 5, 5, 6, 0, 9, 6, 7, 5, 3]
It gives output as the single key only. However I want output as [44,33,555,555,666,9,666,777,555,3]
CodePudding user response:
Refactor your code to build a reverse dictionary to directly map the code from the characters instead of repeatedly searching (this is inefficient):
pad = {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'],
0: ' '}
mapper = {c: str(num)*i for num, l in pad.items()
for i,c in enumerate(l, start=1)}
# {'.': '1', ',?': '11', '!': '111', ':': '1111', 'A': '2' ...}
def str_to_code(s):
return [mapper[c.upper()] for c in s]
str_to_code('hello world')
output:
['44', '33', '555', '555', '666', '0', '9', '666', '777', '555', '3']
CodePudding user response:
This is because you after you find the key, you only append the key once.
You will need to add the logic to duplicate the key string several times.
I only added one line and modified one line of your original code. Hope this helps.
def search(n):
lis = []
pad = {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'], 0: ' '}
for i in n:
for key, values in pad.items():
if i.upper() in values:
temp = str(key) * (values.index(i.upper()) 1) // added
lis.append(temp) // modified
return lis
def main():
m = input("Enter String")
print(search(m))
main()
CodePudding user response:
Following your approach:
def search(n):
lis = []
pad = {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'], 0: ' '}
for i in n:
for key,values in pad.items():
if i.upper() in values:
lis.append(1111*key*int(values.index(i.upper()) == 3)
111*key*int(values.index(i.upper()) == 2)
11*key*int(values.index(i.upper()) == 1)
1*key*int(values.index(i.upper()) == 0)
)
return lis
def main():
m = input("Enter String")
print(search(m))
main()
But I think the approach of @mozway is better.
CodePudding user response:
Consider creating a reverse mapping:
PAD = {
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'],
0: [' ']
}
REVERSED_PAD = {
v: f'{str(k) * (i 1)}'
for k, values in PAD.items() for i, v in enumerate(values)
}
def t9_keyboard_presses(message: str) -> list[int]:
result = []
for ch in message:
uppercase_ch = ch.upper()
if uppercase_ch not in REVERSED_PAD:
raise ValueError('message contains character not on t9 keyboard.')
result.append(REVERSED_PAD[uppercase_ch])
return result
def main() -> None:
message = input('Enter message: ')
print(t9_keyboard_presses(message))
if __name__ == '__main__':
main()
Example Usage:
Enter message: Hello World
['44', '33', '555', '555', '666', '0', '9', '666', '777', '555', '3']