+ 21

'Paint Cost' task in Code Coach

'A number that represents the cost of your purchase *rounded up* to the nearest whole number' they write. However, ceil let's the code fail, round let's it pass. What's going on?

19th Dec 2019, 6:31 PM
HonFu
HonFu - avatar
34 Antworten
+ 11
Well spotted, Coder Kitten! I guess they have to revamp their tests. It's not good if you make people fail with the logical solution and pass with some side track that only works because of some language specific float shenanigans.
19th Dec 2019, 9:08 PM
HonFu
HonFu - avatar
+ 9
Use "n*5.5+44" instead. Multiplying by 1.1 can lead to floating-point inacurracies. For example, you can get 42.00000000000003 as the result. If you use round() you get 42, but if you use ceil() you get 43. See for yourself: import math print("n f(n) round() ceil()\n"+"-"*36) for n in range(42): x = (40+5*n)*1.1 print(f"{n:<4}{x:<20}{round(x):<5}{math.ceil(x)}")
19th Dec 2019, 7:24 PM
Diego
Diego - avatar
+ 7
Ok this is how did it in java if ( cost / 100d >=5) {//ceil it) else if (cost / 100d < 5) {round it} // basically if the decimal point is > = 5,I'd make the cost go up, else make round off to real digit
19th Dec 2019, 8:15 PM
ASM
ASM - avatar
+ 7
Schindlabua You mean even after type conversion??
20th Dec 2019, 5:45 AM
ASM
ASM - avatar
+ 6
Daniel Cooper, I'm not asking for help, I passed the task. But when I used ceil, as the description demands, the code failed, and when I used round, the code succeeded. Both in C and Python.
19th Dec 2019, 6:47 PM
HonFu
HonFu - avatar
+ 6
I solved it using Python/C/C++/Ruby and ceil() works, but round() doesn't.
19th Dec 2019, 6:56 PM
Diego
Diego - avatar
+ 5
Aah, interesting! oO So it's basically a coincidence that rounding with my version passes all the tests? It's a bit strange to write it like that, because intuitively you want to calculate the cost, then add taxes. I get the feeling, the tasks could use a bit of tweaking!
19th Dec 2019, 7:30 PM
HonFu
HonFu - avatar
+ 5
I ran into the same thing in C++ and ended up writing a solution that uses integer arithmetic only. Floats are kind of annoying, once you go float you can never go back. :/
20th Dec 2019, 12:25 AM
Schindlabua
Schindlabua - avatar
+ 5
Schindlabua Yess I completely agree on that, once A-float, niether . floor nor . ceil-ing can save you from getting point errors 😂😂
20th Dec 2019, 9:35 AM
ASM
ASM - avatar
+ 4
Okay, that's strange.
19th Dec 2019, 6:56 PM
HonFu
HonFu - avatar
+ 4
Diego, this was my Python code: n = int(input()) print(round((40 + 5*n) *1.1)) And this is the C version (basically the same): #include <stdio.h> #include <math.h> int main() { unsigned colors; scanf("%d", &colors); printf("%d", (int)round((40+colors*5)*1.1)); return 0; } Do you see how they could have passed, while with ceil they failed?
19th Dec 2019, 6:58 PM
HonFu
HonFu - avatar
+ 4
This works for me in Python: import math n = int(input()) cost = ((40 + (n * 5)) * 11) cost = math.ceil(cost/10) print(cost) But it also looks like a work a round 😉
19th Dec 2019, 9:58 PM
Coding Cat
Coding Cat - avatar
+ 4
Selin Genkur, I don't think it's a good idea to generalize that. Also, sometimes the problem may be a bit more tricky than that. Read for example here: https://www.sololearn.com/discuss/2104980/?ref=app I think the tasks are just designed a bit sloppily (too little time invested in testing properly and to find precise wording) and could use some adjustments.
24th Dec 2019, 12:32 AM
HonFu
HonFu - avatar
+ 3
Diego, I just tried it: If I use your method in Python, I fail one test with round, but pass them all with ceil. But in C, it *still* works, even with round! Although the underlying 'doubles' should be the very same in Python and C! Coder Kitten, writing literals in C should create doubles rather than floats. So the same problem should occur in the C version as well, right?
19th Dec 2019, 7:37 PM
HonFu
HonFu - avatar
+ 3
A. S. M. [ iNACTIVE ] No I mean, if you use floats you'll run into floating point errors and all the maths is slightly wrong and you can't really prevent that. For finance stuff you'd better use integral or fractional datatypes so stuff like that doesn't happen.
20th Dec 2019, 7:52 AM
Schindlabua
Schindlabua - avatar
+ 3
A. S. M. [ iNACTIVE ] All we can do is floor and pray :D
20th Dec 2019, 9:51 AM
Schindlabua
Schindlabua - avatar
+ 2
What do you mean by the line with Indians, Selin Genkur? What's that got to do with it?
23rd Dec 2019, 11:46 AM
HonFu
HonFu - avatar
+ 2
Selin Genkur Ohh no no no,You can't offend us Indians anymore, You see Sundar Pichai is the CEO of google, We have our own Silicon valley in India called Bangalore, So guess what, the jokes on you 😁😝
23rd Dec 2019, 6:22 PM
ASM
ASM - avatar
+ 2
I tried and: round() failed, ceil() failed, so I had to first round it up to two decimal places and then ceil()... it is usually unexpected 0.0000000001 that causes the problem
6th Jan 2020, 1:05 PM
Alice
Alice - avatar
+ 1
HonFu Can I see your attempt?
19th Dec 2019, 6:44 PM
Daniel Cooper
Daniel Cooper - avatar