Python Forum
[split] Alphabetic Telephone Number Translator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Alphabetic Telephone Number Translator
#1
(Apr-24-2018, 01:11 AM)MEH012 Wrote:
""" This Program will ask the user to enter a 10-character telephone number in the format of XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numberic equivalent. For example, if the user enters 555-GET-FOOD the application should display 555-438-3663 """ phoneNum = input("Enter the number in the format of XXX-XXX-XXXX: ") phoneNum= phoneNum.split('-') for var in phoneNum[1:2]: for char in phoneNum: if char == 'A' or char == 'B' or char == 'C': char == '2' elif char == 'D' or char == 'E' or char == 'F': char = '3' elif char == 'G' or char == 'H' or char == 'I': char = '4' elif char == 'J' or char == 'K' or char == 'L': char = '5' elif char == 'M' or char == 'N' or char == 'O': char = '6' elif char == 'P' or char == 'Q' or char == 'R' or char == 'S': char = '7' elif char == 'T' or char == 'U' or char == 'V': char = '8' elif char == 'W' or char == 'X' or char == 'Y' or char == 'Z': char = '9' print(phoneNum) 
That above is my code, and the question is asking the following: albetic Telephone Number Translator: companies use telephone numbers like 555-GET-FOOD so the number is easier for their customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion:A, B, and C = 2 D, E, and F = 3 G, H, and I = 4 J, K, and L = 5 M, N, and O = 6 P, Q, R, and S = 7 T, U, and V = 8 W, X, Y, and Z = 9 Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD the application should display 555-438-3663


can i do "dictionary" on these letters?
like {x={"A":2,...}}
Reply
#2
What have you tried?
Reply
#3
I think that would be the hard way. I'd loop through the characters and have if statements like
if ch in ("ABC"):
  ch = "2"
Reply
#4
You can also use a dictionary, which can also check for invalid input
phone_dict={"ABC":2, "DEF":3, "GHI":4}  ## etc 
for key in phone_dict:
    if ch in key:
        ch = phone_dict[key]
print(ch) 
Reply
#5
I saw two subtasks here:

1. Create dictionary of letter-number mappings for lookup
2. Construct new string using lookup mapping

I thought that would be fun to try to accomplish it with two oneliners. I came up with such solution (technically not oneliners as it requires three imports but still :-)):

from string import ascii_uppercase
from textwrap import wrap
from collections import ChainMap


tel_num = '555-GET-FOOD'
d = ChainMap(*(dict.fromkeys(chunk, i) for i, chunk in enumerate(wrap(ascii_uppercase, 3), start=2)))
print(''.join((str(d[char]) if char.isalpha() else char for char in tel_num)))

# outputs 55-438-3663 as required
However, first step is actually failing as characters are not evenly distributed and lookup dictionary is:

Output:
ChainMap({'A': 2, 'B': 2, 'C': 2}, {'D': 3, 'E': 3, 'F': 3}, {'G': 4, 'H': 4, 'I': 4}, {'J': 5, 'K': 5, 'L': 5}, {'M': 6, 'N': 6, 'O': 6}, {'P': 7, 'Q': 7, 'R': 7}, {'S': 8, 'T': 8, 'U': 8}, {'V': 9, 'W': 9, 'X': 9}, {'Y': 10, 'Z': 10})
So lookup dictionary is not correct.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Alphabetic Telephone Number Translator MEH012 4 22,093 Apr-25-2018, 08:47 PM
Last Post: woooee
  Translator Zatoichi 13 28,935 Feb-15-2018, 04:01 AM
Last Post: Zatoichi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020