Python: Why this tuple is printing the result and "None"? Also: Is there a better way to achieve the result? -


i need calculate given number, how many "fives", "twos", , "ones" can numbers. sorry english little limited sort of explanation :) maybe example better:

excercise: print stamps(8) result should be: (1, 1, 1) ( 1 5p stamp, 1 2p stamp , 1 1p stamp)

i´ve found way achieve that, tuple() printing result , "none", , don´t know why. know there´s better, shorter way correct result.

this i´ve done:

def stamps(dinero):     p5=dinero/5     p5a=p5*5     resultado1=dinero-p5a     dinero=resultado1     p2=dinero/2     p2a=p2*2     resultado2=dinero-p2a     dinero=resultado2     p1=dinero/1     p1a=p1*1     resultado3=dinero-p1a     dinero=resultado3     print tuple([p5,p2,p1]) 

the result with: print stamps(8) (1, 1, 1) none

update: i´ve found better solution, i´m posting here in case wonders better solution:

def stamps(n):     #basically, thats same return n/5, n%5/2, n%5%2     return n/5, (n-5*(n/5))/2, (n-5*(n/5))-2*((n-5*(n/5))/2) 

as people have stated can change print return, big improvement code use % (or modulo) operator.

def stamps(dinero):     p5=dinero/5     dinero=dinero%5     p2=dinero/2     dinero=dinero%2     p1=dinero/1     return tuple([p5,p2,p1])  print stamps(8) >>> (1,1,1) 

in code line:

p5=dinero/5 

performs integer division, while below gets remainder, multiplying number of multiples of divisor in original number , subtracting it:

p5a=p5*5 resultado1=dinero-p5a dinero=resultado1 

most languages offer modulo function performs in single step:

dinero=dinero%5 

this same part divide 3, , when divide 1 there never integer remainer, can remove code completely.

python has way can shorten again using divmod() returns both divisor , modulus:

def stamps(dinero):     p5,dinero=divmod(dinero,5)     p2,dinero=divmod(dinero,2)     p1=dinero     return tuple([p5,p2,p1])  print stamps(8) >>> (1,1,1) 

and lastly, can generisise completely, having function take both amount , array of stamp values , call that:

def stamps(dinero):     return allstamps(dinero,[5,2,1])  def allstamps(dinero=1,stamps=[]):     vals = []     stamp in sorted(list(set(stamps)), reverse=true):         val,dinero=divmod(dinero,stamp)         vals.append(val)     return tuple(vals)  print stamps(8) >>> (1,1,1) print allstamps(8,[5,3,1]) >>> (1,1,0) 

regarding code execution speed:

i ran timeit on of options, , calls / , % turned out faster single call divmod():

> python -m timeit 'a=1000;b=a/5;c=b*5;d=a-c;a=d'  10000000 loops, best of 3: 0.156 usec per loop > python -m timeit 'a=1000;b=a/5;a=a-b*5;'  10000000 loops, best of 3: 0.127 usec per loop > python -m timeit 'a=1000;a=a-(a/5)*5;'  10000000 loops, best of 3: 0.121 usec per loop > python -m timeit 'a=1000/13;b=1000%13;'  10000000 loops, best of 3: 0.0755 usec per loop root@meteordev:~# python -m timeit 'a,b=divmod(1000,13);'  10000000 loops, best of 3: 0.183 usec per loop 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -