-1

So i'm not too sure how to proceed with this.
I want to replace 1 with one, 2 with two and so on.

I'm supposed to do this with a list.. but I'm not sure how to do so.

The user inputs a 10 digit number, which gets re-arranged into a phone number and then dashes are added in the appropriate areas.

I have completed the first two parts but can't figure out the conversion to words.

Edit** I am saying this is not a duplicate due to the fact that I was looking for a simple method to doing this. Every other thread I looked at contained coding beyond the level of understanding that I possess.

def makePhoneNums():
    original = getNumber()
    phone_num = fixPhoneNum(original)
    phone_word = getWordForm(phone_num)
    printPhoneNums(original, phone_num, phone_word) 

def getNumber():
    original = input("Input a 10 digit number: ")
    while 10 != len(original) or original.isdecimal == False:
        original = input("Error! Input a 10 digit number!: ")
    print()
    return original

def fixPhoneNum(original):
    switched = original[-1] + original[5:9] + original[1:5] + original[0]
    phone_num = switched[:3] + '-' + switched[3:6] + '-' + switched[6:]
    return phone_num

def getWordForm(phone_num):
    words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
             'eight', 'nine']
    s = '0123456789'
    telNum = ''
    for i in range(len(s)):
        telNum += words[int(s[i])]
    return words[i]

def printPhoneNums(original, phone_num, phone_word):
    print(original, '\t', phone_num, '\t    ', phone_word)

4 Answers4

1

Firstly, you seem to be returning the wrong thing!

def getWordForm(phone_num):
    words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
             'eight', 'nine']
    s = '0123456789'
    telNum = ''
    for i in range(len(s)):
        telNum += words[int(s[i])]
    return telNum  # <== return telNum here

Furthermore, you seem to not be using phone_num. I don't think you need s there at all. So:

def getWordForm(phone_num):
    words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
             'eight', 'nine']
    telNum = ''
    for digit in phone_num:
        telNum += words[int(digit)]
    return telNum  

I'm not sure if you mean to but spaces between the words, but it should be easy to figure out.

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
1

Few issues in your code -

  1. Main issue, in your getWordForm() function, you create the string with words as telNum , but then you return words[i] , you should return the telNum and you should iterate over phone_num the argument to your function, not s. Please note this would not include any spaces between the words, a better way to do this would be to use ' '.join() if you want spaces. Example -

    def getWordForm(phone_num):
        words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
                 'eight', 'nine']
        return ' '.join([words[int(ch)] if ch.isnumeric() else ch for ch in phone_num])
    

    You need the condition if ch.isnumeric() because you are sending in the fixed phone number (with - inbetween) , if you do not want spaces you can simply use '' (empty string) for str.join() .

    As asked in comments, a method without using str.join() -

    def getWordForm(phone_num):
        words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
                 'eight', 'nine']
        result = ''
        for ch in phone_num:
            if ch.isnumeric():
                result += words[int(ch)] + ' '
            else:
                result += ch + ' '
        return result[:-1]
    

    Please note, the method without str.join() would not include spaces, I would recommend using str.join() over this.

  2. Secondary issue , you should call the function isdecimal in getNumber() , as - while 10 != len(original) or original.isdecimal() == False:

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • So this code you've provided does work. However, I haven't used .join, ch, etc yet. The specifications I need to follow includes staying within my level of understanding. Is there a more introductory way of doing this? – Cameron Rogozinsky Oct 21 '15 at 04:44
  • Do you need spaces between the words? Or even better would be to understand how `str.join()` works, its easy and a powerful tool. – Anand S Kumar Oct 21 '15 at 04:45
  • Yes, I need spaces between both the words and the dashes. It's just that I haven't had str.join() demonstrated in class and if I use it I would be docked marks for doing the lab which should be completed using what I've already been taught in class. – Cameron Rogozinsky Oct 21 '15 at 04:51
  • @CameronRogozinsky - Why would you be docked marks for using a method the class hasn't taught you yet? Is it because it would heavily imply that you didn't do your own homework? – TigerhawkT3 Oct 21 '15 at 05:11
  • @TigerhawkT3 This is my first experience with computer programming. I have been doing my homework but frankly this lab is the hardest of the semester, both my Lab instructor and my professor have said so. Please tell me that when you started out you didn't feel overwhelmed at times. – Cameron Rogozinsky Oct 21 '15 at 22:03
0

One liner:

call = lambda s: ' '.join(map(lambda c:['oh','one','two','three','four','five','six','seven','eight','niieeiieeine','-'][int(c) if c.isdigit() else -1],s))

For a good time:

>>> print(call('8675309'))
eight six seven five three oh niieeiieeine

Alternative one-liner from PM 2Ring:

call = lambda s: ' '.join([['oh','one','two','three','four','five','six','seven','eight','niieeiiee‌​ine','-'][int(c) if c.isdigit() else -1] for c in s]

Prettier IMO, but unclear if it's more efficient:

In [75]: timeit.timeit("lambda s: ' '.join([['oh','one','two','three','four','five','six','seven','eight','niieeiiee‌​ine','-'][int(c) if c.isdigit() else -1] for c in s])('8675309')",number=200000000)
Out[75]: 17.842306826962158

In [76]: timeit.timeit("lambda s: ' '.join(map(lambda c:['oh','one','two','three','four','five','six','seven','eight','niieeiieeine','-'][int(c) if c.isdigit() else -1],s))('8675309')",number=200000000)
Out[76]: 17.543266678927466
Community
  • 1
  • 1
dogwynn
  • 408
  • 3
  • 13
  • Cute (although you're showing your age with that Tommy Tutone ref :) ). OTOH, `lambda s: ' '.join([['oh','one','two','three','four','five','six','seven','eight','niieeiieeine','-'][int(c) if c.isdigit() else -1] for c in s])` is marginally shorter & avoids the `map` function call. Also, `map` in Python 3 returns an iterator, which `join` needs to convert to a list, since `join` has to make two passes over its arg (the 1st pass is to calculate the destination string size). So it's slightly more efficient to pass `join` a list comp than an iterator or generator. – PM 2Ring Oct 21 '15 at 09:37
  • @PM2Ring, glad someone got it. :) Agreed that your version is cleaner looking, but the two versions are within margin of error of each other, timing-wise. Updated my answer with timing. – dogwynn Oct 21 '15 at 16:13
  • It's hard to get true timing data from stuff like this. I _suspect_ that the time to build the number words list is not negligible relative to the time it takes for the list comp to loop over 7 digits. Of course, you could pre-build that list, but then it wouldn't be a [one-liner](http://meta.stackoverflow.com/a/287631/4014959). ;) And there will probably be a speed difference running it on Python 3 vs Python 2, for the reason I mentioned earlier. – PM 2Ring Oct 22 '15 at 14:36
  • Probably so. Nice discussion all the same. – dogwynn Oct 22 '15 at 14:39
0
>>> words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
>>> phone_number="123 1234 123"
>>> '-'.join([words[int(num)] for num in list(phone_number) if num.isdigit()])
'one-two-three-one-two-three-four-one-two-three'

Or map solution,

>>> '-'.join(map(lambda x: words[int(x)] if x.isdigit() else '', list(phone_number)))
'one-two-three--one-two-three-four--one-two-three'

In the function

def getWordForm(phone_number):
    words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    return '-'.join([words[int(num)] for num in list(phone_number) if num.isdigit()])
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42