Home > front end >  Python code to return a phone number in numbers , after giving inputs in words
Python code to return a phone number in numbers , after giving inputs in words

Time:05-08

Yesterday, I was doing one problem where I have to complete a function which can convert a phone number in numbers after given input in words, I have used s.replace() to do that, and modifier also. Since this question was there in hackerrank problem I cant provide complete code, since test is over now and the page is no longer available. How can I use those modifiers as well, like "two three four double two triple 3" will print 23422333?

def getPhoneNumber(s):
    s.replace("Zero" "0")
    s.replace("one" "1")
    ...


modifier = {
    "double": 2,
    "triple" : 3,
}

CodePudding user response:

d = {'zero':'0','one':'1','two':'2','three':'3','four':'4','five':'5','six':'6','seven':'7','eight':'8','nine':'9'}
mod = {'double':2,'triple':3}

num_word = input('Enter Number In Words').split(' ')

number = ''
m = 1
for a in num_word:
    try:
        m = mod[a.lower()]
        continue
    except KeyError:
        pass
    try:
        number =d[a.lower()]*m
        m = 1
    except KeyError:
        number =a.lower()*m
        m = 1
print(number)

OUTPUT

IN: double two
OUT: 22

IN: two three four double two triple 3
OUT: 23422333

CodePudding user response:

Try this,

def getPhoneNumber(number):
  to_replace = {"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5", "six":"6", "seven":"7", "eight":"8", "nine":"9"}
  modifiers = {"double":2, "triple":3}
  words = list(map(str.lower, number.split()))
  output = ""
  skip = None

  for index, word in enumerate(words):
    if index == skip:
      continue

    if word in modifiers:
      skip = index   1
      try:
        output  = modifiers[word] * to_replace[words[skip]]
      except:
        pass

    else:
      output  = to_replace[word]

  return output

getPhoneNumber("Double One Triple Two Five")

Output -

112225

CodePudding user response:

numbers = {'zero':'0','one':'1','two':'2','three':'3','four':'4','five':'5','six':'6','seven':'7','eight':'8','nine':'9'}
multipliers = {'double':2,'triple':3}

def solve(sentence):
    results = []
    sentence = sentence.lower().split()
    repeat = 1
    for word in sentence:
        if word in multipliers:
            repeat = multipliers[word]
        else:
            if word in numbers:
                word = numbers[word]
            results.append(word * repeat)
            repeat = 1
    return ''.join(results)

CodePudding user response:

I'll throw this out there as a variation on other themes:

d = {'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4',
     'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9'}
mod = {'double': 2, 'triple': 3}

num_word = input('Enter Number In Words: ')

number = []
m = None
for word in map(str.lower, num_word.split()):
    if m:
        number.extend(d.get(word, '') * m)
        m = None
    else:
        if not (m := mod.get(word)):
            number.append(d.get(word))

print(''.join(filter(bool, number)))
  • Related