Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test each specific digit or character

I would like to receive 5 digits inputted by the user and then print something for each specific digit.

For example, if the user enters 12345, I would like to print a specific output for 1 first, then another output for 2, etc.

How would I go about doing this? I would prefer to create a function if possible.

#!/usr/bin/python3

zipcode = int(raw_input("Enter a zipcode: "))

if zipcode == 1:
       print ":::||"
elif zipcode == 2:
       print "::|:|"
elif zipcode == 3:
       print "::||:"
elif zipcode == 4:
       print ":|::|"
elif zipcode == 5:
       print ":|:|:"
elif zipcode == 6:
       print ":||::"
elif zipcode == 7:
       print "|:::|"
elif zipcode == 8:
       print "|::|:"
elif zipcode == 9:
       print "|:|::"
elif zipcode == 0:
       print "||:::"
like image 228
bob george Avatar asked Nov 28 '22 07:11

bob george


2 Answers

A nice work-around

  • Store them in a tuple (and not a dictionary as all your values are in sequence, list or a tuple is better in such cases than to access by keys and values )

    list_bars = (":::||","::|:|",...)
    

    In this way you don't need the numerous if, elif stuff

  • Don't convert it to int leave it as a str itself. Using this you can iterate over the string rather than the converted numeral.

Finally get all you code at one place,

zipcode = raw_input("Enter a zipcode: ")
list_bars = (":::||","::|:|","::||:",":|::|",":|:|:",":||::","|:::|","|::|:","|:|::","||:::")
for i in zipcode:
    print(list_bars[int(i)-1])

Now for a small demo

Enter a zipcode: 123
:::||
::|:|
::||:

Using the timeit module to test the difference between list, tuple and dictionary as a data structure

bhargav@bhargav:~$ python -m timeit 'list_bars = [":::||","::|:|","::||:",":|::|",":|:|:",":||::","|:::|","|::|:","|:|::","||:::"]; [list_bars[int(i)-1] for i in "12345"]'
100000 loops, best of 3: 3.18 usec per loop
bhargav@bhargav:~$ python -m timeit 'list_bars={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}; [list_bars[int(i)] for i in "12345"]'
100000 loops, best of 3: 3.61 usec per loop
bhargav@bhargav:~$ python -m timeit 'list_bars = (":::||","::|:|","::||:",":|::|",":|:|:",":||::","|:::|","|::|:","|:|::","||:::"); [list_bars[int(i)-1] for i in "12345"]'
100000 loops, best of 3: 2.6 usec per loop

As you can see, a tuple is the fastest as compared to the others.

like image 26
Bhargav Rao Avatar answered Dec 06 '22 09:12

Bhargav Rao


You could use a dictionary and then iterate through the input:

zipcode = raw_input("Enter a zipcode: ")

codes={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}

for num in zipcode:
    print codes[int(num)], #add a comma here if you want it on the same line

This would give you:

>>> 
Enter a zipcode: 54321
:|:|: :|::| ::||: ::|:| :::||

EDIT:

For no spaces:

zipcode = raw_input("Enter a zipcode: ")

codes={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}

L = [] #create a list

for num in zipcode:
    L.append(codes[int(num)]) #append the values to a list

print ''.join(L) #join them together and then print

Now this would print:

>>> 
Enter a zipcode: 54321
:|:|::|::|::||:::|:|:::||
like image 83
logic Avatar answered Dec 06 '22 11:12

logic