+ 2
Kaleidoscopes in Ruby
Hey, can you put me in the right direction without actually telling me what’s wrong with my code? One of the hidden test cases. https://code.sololearn.com/cs39paBl6x9m/?ref=app
18 ответов
+ 5
Eva , please add your code in the description linked to Playground, so someone can check it.
+ 4
Eva B Yes! guys called it a "hack" though,to that I suggest instead of comparing with 3 do it with 1023 :)
kaleidoscopes = gets.chomp.to_i
if kaleidoscopes == 1
puts 5.35
else
printf("%.2f",(4.5*kaleidoscopes*1.07))
end
=begin
# this is not the answer only for your ref:
https://www.sololearn.com/Discuss/2135726/?ref=app
=end
+ 3
Eva B since the link I mentioned in the answer posted by TheWh¡teCat 🇧🇬 is removed
(it had lots of useful information about this problem)
But unfortunately we don't have access to that anymore
You have to use a work around like :
kaleidoscopes = gets.chomp.to_i
if kaleidoscopes == 1
puts 5.35
elsif kaleidoscopes == 1023
puts 4925.74
else
puts (4.5*kaleidoscopes*1.07).round(2)
end
+ 2
Eva B , in my opinion you should round the result as final part of the calculations. Maybe when you start round at the beginning there's a precision loss and that could be a difference (I'm only guessing, because I can't try it and check as it is a Pro task).
+ 1
Eva B , I can suppose that formatting, as it doesn't seem to format always to 2 decimal places, round() method is the reason. Check if that fix it.
https://code.sololearn.com/cM3qa87U0KmO/?ref=app
+ 1
Eva B , try again the code I added rounding before formatting. I hope that this will solve it. Test case with 3, gives 14.45, as you say.
https://code.sololearn.com/cM3qa87U0KmO/?ref=app
+ 1
Eva B , check again with using Bigdecimal, please. I changed the code.
https://code.sololearn.com/cM3qa87U0KmO/?ref=app
+ 1
Eva B , I think Bigdecimal is the only way to come to a solution. The test cases obviously are not well written, because it is expected to round one time up to 2 places one time down.
+ 1
Eva B , can you try again the code, I make some changes. It looks like that my reason is right. It's all about float representation in Ruby. I found this question about the same topic
https://www.sololearn.com/discuss/2127606/?ref=app
https://code.sololearn.com/cM3qa87U0KmO/?ref=app
+ 1
Daljeet Singh , thanks, I would never thought of doing that. Isn’t that hard coding? As you said, its a workaround. I’m unhappy with this solution, it is somewhat not elegant. But it works.
0
Inserted, thanks!
0
Thanks for checking TheWh¡teCat, I had checked it already but that’s not it. Must be something else...
0
Hey, I checked it. The (hidden) trst case that was failing is passing now, but the test case that was passing, is failing now... It’s not hidden and I can see that for 3 kaleidoscopes the total should be 14.45 but my output is 14.44...
0
TheWh¡teCat 🇧🇬 , nope. Now the test case with 3 kaleidoscopes is passing but again the one with which was failing in the first place is failing again :(
0
TheWh¡teCat 🇧🇬 3/5 test cases failed...as far ad I’ve understood, we’re not supposed to use gems/other libraries. I wrote a method for another challenge and it was rejected, I think they want is to sove it just manipulating the methods...
0
... but I’m not really sure about anything with some of these challenges. I wish the test cases were not hidden.
0
TheWh¡teCat 🇧🇬 thanks for having a look at my code. I thibk I’m going to have a break and come ack to it tomorrow with fresh eyes.
0
I want an example of Ruby