Multiple of 10 closest to a sum

Asked

Viewed 913 times

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 the following: the somamulti sum 2 values, in this case can be 120 + 31 = 151 and my function will first test if this value is multiple of 10, and, if it were, give me this result. otherwise would return me the nearest multiple of 10 greater than or equal to the sum of the two values above, in which case would return me 160.

What I don’t know is how I get to that 160, which means I can’t do the last part of else.

  • I don’t know if I understand your problem, I think what you want is just this: return somamulti / 10 + 1) * 10 http://ideone.com/tNoe4r Got a problem with this? Solve what you want?

  • It doesn’t seem to be the case if .. Lse. As @bigown mentioned, it pays to return the rounded value without doing any testing. One line of code is enough: return somamulti - ( somamulti % 10 ) would serve well if it were rounded down, with adjustments can round up;

  • @Bacco that would give me 150 and I want the multiples of 10 over 151, ie 160.

  • In this case the code line @bigown gave me works! Thanks! :)

  • @Bigown posts a full answer, I think that’s the case.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

  • Ceil(,-1) does not roll ?

Show 2 more comments

3 answers

4

A version similar to 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 on IDEONE.


Version without if:

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

See working on IDEONE.

  • That’s still 150 :/

  • I put the links in IDEONE for 2 versions, seems correct, but if something is not as expected, let us know. Anyway, it’s the same solution as @bigown, just posted as example of using module operator/rest (%).

2

You don’t need to use the if In this case, you can solve it with pure mathematics. You need to make sure it goes to the next ten, so we’re 9 to the number found. We divide by 10 to lose unity and stay only from the ten as something relevant. Multiply by 10 to vote on the previous scale. This only works for integers which seems to be the case

Would look like this:

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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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

If you take 153 plus 9 equals 162, dividing by 10 equals 16 since integer cannot have decimal place that is discarded and multiplying by 10 equals 160.

  • this works yes, but what if the value was another without 151? would not work anymore... and I want it for any value

  • Show a value that doesn’t work. I edited the code in ideone to show from 150 to 160, they all work. Unless you are saying that if you find 150 then it should go to 160. Then just change the 9 p/ the 10, but it does not seem that it is what you want.

  • ah you’re right, it works in other values too, I just can’t understand it is because we added 9 to somamulti

  • How do you go to the next ten guaranteed? In thesis you would have to add 10. But if Momar 10 gives problem when you are already in the next ten. In other words, if you have 150 and add 10, it goes to 160 and I understand that this is wrong, when you are in 150 you are in the ten that you want. Right? So it adds up to 9 so it doesn’t happen. If you add up 10, when you get 150, it goes to 160. Is that right? I don’t think so, but if you are change from 9 to 10. And let me know.

  • yes, if you’re 150 you’re not supposed to do anything anymore because it’s already a multiple of 10. but now I get it, thank you!

0

This is a quotient problem and rest

def multiplo():
    somamulti = somaImpares(x) + somaPares(x)
    quociente, resto = somamulti // 10, somamulti % 10
    return 10 * (quociente + (resto != 0))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.