+ 2
Stuck with the Halloween Candy Challenge
I solved the challenge (https://sololearn.com/coach/4/?ref=app) and it passes the first two sample data, but it wonât pass the last three. As the input in the last 3 is hidden, I donât know whatâs wrong with my code. Hereâs the code: houses = int(input()) #your code goes here result=200/houses print((round(result)) If you would help me out, Iâd really appreciate it. Thanks, guys!!
7 Answers
+ 6
Instead of round()
use
import math
math.ceil()
round(result) â
math.ceil(result) âď¸
+ 3
Juan Macarlupu According to problem we need most nearest whole number so in this case ceil will work fine because round just give round off valve means 5.4 will be 5 and 5.6 will be 6 but when we use ceil we get only one value that is 6. So
round (5.4) = 5
round (5.6) = 6
but
ceil (5.4) = 6
ceil (5.6) = 6
that is what we need in this problem.
+ 2
Thanks! would you mind briefly explaining why? I really appreciate your help!
+ 2
just found this:
Math.ceil( ) computes the ceiling functionâi.e., it returns the closest integer value that is greater than or equal to the function argument. Math.ceil( ) differs from Math.round( ) in that it always rounds up, rather than rounding up or down to the closest integer. Also note that Math.ceil( ) does not round negative numbers to larger negative numbers; it rounds them up toward zero.
in https://www.oreilly.com/library/view/javascript-the-definitive/0596101996/re106.html
+ 2
Juan Macarlupu I think the problem is that round() rounds down as well...
print(round(3.33)) # 3
print(math.ceil(3.33)) # 4
in the above example, round() will round the 3.33 down to give 3
But math.ceil() will give the next greatest integer after 3 that is 4.
so
print(round(3.001))
will give 3
but
print(math.ceil(3.001))
will give 4
+ 2
Thanks so much!!
0
For C# guys the Method name is Math.Ceiling not Math.ceil !