I am out of trial and error again Lists

Seymore4Head Seymore4Head at Hotmail.invalid
Thu Oct 23 10:00:38 EDT 2014


On Thu, 23 Oct 2014 09:23:29 -0400, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:

>On Thu, 23 Oct 2014 00:10:28 -0700, Larry Hudson
><orgnut at yahoo.com.dmarc.invalid> declaimed the following:
>
>
>>I know you are trying to explore lists here, but I found myself somewhat intrigued with the 
>>problem itself, so I wrote a different version.  This version does not use lists but only 
>>strings.  I'm just presenting it as-is to let you try to follow the logic, but if you ask, I'll 
>>explain it in detail.  It turns your long sequence of if's essentially into a single line -- 
>>unfortunately 's' and 'z' have to be handled as special-cases, which turns that single line into 
>>a six-line if/elif/else set.  You might consider this line 'tricky', but I'll just say it's just 
>>looking at the problem from a different viewpoint.  BTW, this version accepts upper-case as well 
>>as lower-case.  isdigit() and isalpha() are standard string methods.
>>
>
><SNIP>
>
>	This is another example where changing data structures can simplify
>things a lot...
>
>-=-=-=-=-=-=-
>keys = {    "a" : "2",  "b" : "2",  "c" : "2",  # 1 is not assigned
>            "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" : "7",
>            "t" : "8",  "u" : "8",  "v" : "8",
>            "w" : "9",  "x" : "9",  "y" : "9",  "z" : "9"   }
>
>def textToNumber(inp):
>    outp = []
>    for c in inp.lower():
>        outp.append(keys.get(c, c))
>    return "".join(outp)
>
>if __name__ == "__main__":
>    while True:
>        #NOTE: Python 2.7!
>        aNumStr = raw_input("\nEnter Phone Number String: ")
>        if not aNumStr: break
>        print "Converted to all numbers: %s" % textToNumber(aNumStr)
>
I'll have a look at this one too.  I haven't used a dictionary yet.
Thanks
>-=-=-=-=-=-=-
>Microsoft Windows [Version 6.1.7601]
>Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
>
>C:\Users\Wulfraed\Documents\Python Progs>Phone.py
>
>Enter Phone Number String: 1-800-getcharter
>Converted to all numbers: 1-800-4382427837
>
>Enter Phone Number String: 1-800-leo laporte
>Converted to all numbers: 1-800-536 5276783
>
>Enter Phone Number String: 1 800 callaprogrammer
>Converted to all numbers: 1 800 225527764726637
>
>Enter Phone Number String: +1(800)Leo-Laporte
>Converted to all numbers: +1(800)536-5276783
>
>Enter Phone Number String:
>
>C:\Users\Wulfraed\Documents\Python Progs>



More information about the Python-list mailing list