Multiple of 10 closest to one sum

0
def multiplo(x):

    somamulti= somaImpares(x) + somaPares(x)

    if somamulti/10==0:
        return somamulti/10
    else:
        return

In this function I did what happens is as follows: somamulti adds 2 values, in this case it can be 120 + 31 = 151 and my first function will test if this value is multiple of 10, and, if it would give me that result. otherwise it would return me the nearest multiple of 10 greater than or equal to the sum of the two above values, in which case it would return me 160.

What I do not know is how I get to this value 160, that is, I can not do the last part of else .

    
asked by anonymous 17.03.2015 / 22:52

2 answers

3

A version similar to that of the question, but using module:

(I still prefer the purely mathematical solution)

def multiplo(x):

    somamulti= somaImpares(x) + somaPares(x)

    if somamulti % 10 == 0:
        return somamulti
    else:
        return somamulti + 10 - ( somamulti % 10 )

See working at IDEONE .


Version without if:

def multiplo( x ):
    somamulti = somaImpares( x ) + somaPares( x )
    return somamulti + 9 - ( somamulti + 9 ) % 10

See working at IDEONE .

    
17.03.2015 / 23:16
1

You do not need to use if in this case, you can solve with pure math. You need to make sure you move on to the next ten, so we're 9 to the number you found. We divide by 10 to lose the unit and to stay only from the decade as something relevant. We multiplied by 10 to vote on the previous scale. This only works for integers that seems to be the case

It would look like this:

def multiplo():
    somamulti = somaImpares(x) + somaPares(x)
    return (somamulti + 9) / 10 * 10

See running on ideone .

In your example 151 + 9 gives 160, dividing by 10 gives 16 and multiplying by 10 gives 160.

If you get 153 adding with 9 gives 162, dividing by 10 gives 16 since integer can not have a decimal place that is discarded and multiplying by 10 gives 160.

    
17.03.2015 / 23:15