Kaleidoscopes in Ruby | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

16th Apr 2020, 2:02 PM
Eva B
Eva B - avatar
18 Answers
+ 5
Eva , please add your code in the description linked to Playground, so someone can check it.
16th Apr 2020, 2:05 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 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
17th Apr 2020, 8:24 AM
Daljeet Singh
Daljeet Singh - avatar
+ 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
17th Apr 2020, 4:44 AM
Daljeet Singh
Daljeet Singh - avatar
+ 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).
16th Apr 2020, 3:05 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 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
16th Apr 2020, 4:23 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 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
16th Apr 2020, 7:42 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Eva B , check again with using Bigdecimal, please. I changed the code. https://code.sololearn.com/cM3qa87U0KmO/?ref=app
16th Apr 2020, 8:05 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 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.
16th Apr 2020, 8:21 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 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
16th Apr 2020, 9:06 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 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.
17th Apr 2020, 5:56 AM
Eva B
Eva B - avatar
0
Inserted, thanks!
16th Apr 2020, 2:57 PM
Eva B
Eva B - avatar
0
Thanks for checking TheWh¡teCat, I had checked it already but that’s not it. Must be something else...
16th Apr 2020, 3:11 PM
Eva B
Eva B - avatar
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...
16th Apr 2020, 7:24 PM
Eva B
Eva B - avatar
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 :(
16th Apr 2020, 7:47 PM
Eva B
Eva B - avatar
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...
16th Apr 2020, 8:07 PM
Eva B
Eva B - avatar
0
... but I’m not really sure about anything with some of these challenges. I wish the test cases were not hidden.
16th Apr 2020, 8:17 PM
Eva B
Eva B - avatar
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.
16th Apr 2020, 8:25 PM
Eva B
Eva B - avatar
0
I want an example of Ruby
5th Jun 2021, 11:56 PM
حسين شكر فتحي المولى
حسين شكر فتحي المولى - avatar