8 kyu
Miles per gallon to kilometers per liter
7,920 of 20,484emporio
Loading description...
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
This comment has been hidden.
one on the expected answers with c has a 1 all the way in the 10thousandths place.
The issue is with your code. It appears you looked up the conversion factor for mpg to kpl online, but that number isn't precise enough. You should use the numbers provided in the description to solve the kata. The numbers that you see in the test results that have more decimal places than necessary is due to the fact that some floating point numbers cannot be precisely represented in binary. Your solution also generates numbers like this. You will see them if you hit the attempt button a few times with your current solution.
This comment has been hidden.
This comment has been hidden.
The function calls for returning a double, but you're converting your return value to a float. Due to the differences in their binary representations of 12.74, the two values are not equal. The kata should be using an approximate equality comparison for its tests, but that issue has already been noted below.
Also description is inconsistent here:
These are for all languages except JavaScript
This is for JavaScript
All-in-all, the rounding requirement should be removed for all languages (in-accordance with issue below) and replaced with the description in JavaScript
All languages should follow Java by using approximate equality comparison when writing custom tests
This comment has been hidden.
in python, this doesn't work...
return mpg / 2.35
10 - 4.25 (rounded) - expected 3.54
typed on calculator, got 4.25 (again rounded)
That's because 2.35 is the conversion factor from mpg to kpl of a US gallon. The kata is specifically asking about imperial gallons, which are larger than US gallons. It's better to just use the numbers provided in the kata rather than search online for a conversion factor anyway, otherwise you are likely to run into rounding errors.
problem with big numbers at random test on C.
Mathematics are correct, idk what wrong with them. Example:
Submitted: 7.636e+07
Expected: 7.64e+07
the problem comes from your code. you do not use the appropriate datatypes (note the return type)
doesn't work properly, at least in c++. tests are written incorrectly. the constant by which the parameter is multiplied in the first two cases is different from the last
Doesn't work for random values in Python. For example, for mpg 8540, my solution gives 3023.16. It fails saying the right answer should be 3023.21. Why this is failing?
Use the numbers stated in the kata's description. Where did you get that
0.354
factor? Not a kata issue.//distance per volue
//km per gall
//km per litr
//какую дистанцию за еденицу объёма
//сколько километров на галон
//сколько километров на литр
This comment has been hidden.
Read this: https://docs.codewars.com/training/training-example
Not a kata issue.
Thank you Chrono79. I'm just a beginneer at coding, and have smol bren. Thank you.
You're welcome. There are some more useful docs there too, make sure you check them out.
python new test framework is required. updated in this fork
Approved
This comment has been hidden.
I get 10.62 with my formula.
I thought it wanted L/100 km which is the normal way of counting fuel..
This comment has been hidden.
Aren't they at the end of the description?
They are. You are supposed to do the remaining math yourself.
This comment has been hidden.
Read this: https://www.codewars.com/kata/557b5e0bddf29d861400005d/discuss#5cbe0deb01081d000d861f2f
NASM translation
It may be helpful to detail the expected rounding technique in the description. After fiddling around with all sorts of different ones I found that the solution is looking for a "half-up" rounding technique. The term "rounding off" can either be a bit vague or suggest (as I initially thought) that one should cut off any remainders (floor-based rounding).
The reference solution in C# expected half-up rounding. In other languages, it just expects the default rounding mode of the language. Though, most sunmitted solutions in C# use the default rounding too, this particular feature of C# test is not justified. Unless I miss something, I think it just be aligned on other languages.
This comment has been hidden.
No, that data should be there. Read the posts below.
Ruby 3.0 should be enabled.
done
This comment has been hidden.
This requirement is idiotic and wrong on more than one level.
Returning a
double
, how can I even be able to return 5.5, but not 5.50?I adjusted epsilon by 1 and also adapted print formatting to at least show the correct differences in the error messages.
LGTM
@hobovsky: I removed this non-sensical requirement from the description.
For some reason it doesn't recognize the answer as a Number. (In javascript anyways).
So even if the answer's right, it might not be recognized as a number. (That was the case for mine anyways.)
Post your code and mark the post as having spoiler content, I don't know what you meant there.
This comment has been hidden.
The toFixed() method returns a string
the math in the expected answer is wrong isn't it? 12mpg = 5.10 kpl not 4.whatever
There are 2 different kinds of mpg, imperial and metric. The kata here is asking for imperial
@nwardoh. https://en.wikipedia.org/wiki/Gallon
EDIT: But I am still confused as to why it would be using British imperial Gallons, yet at same time using American spelling of Liter (instead of Litre).
Crystal translation kumited :) (author inactive)
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
C translation kumited
please scrutinize and approve
domo!
As you wish !!!
_0_ExampleTests Expected: equal to 12.74 Actual: 12.74 _1_BasicTests Expected: equal to 3.54 Actual: 3.54 _2_RandomTests Expected: equal to 25.49 Actual: 25.49
What's up with it?
I'm getting similar issues with this kata on the last expected test in C++. The test is expeting 12.74 and my result is 12.74 but still marks it as incorrect.
It seems you're returning a float instead of a double. See the type of your function in the initial code.
Chrono79 said it all.
This comment has been hidden.
One thing that makes this kata very difficult is that you have to get the numbers exactly right. If the values you use to convert mile to km and gallon to liter are ever so slightly off, a case will come up in random testing where it causes a rounding error, causing your solution to fail.
This comment has been hidden.
And it is not possible to do it. Removed.
Seems like this is a persistent problem: Test Results: Tests ExampleTests
Expected: equal to 12.74 Actual: 12.74
Despite the above, my program was said to have failed the test. Looks pretty good to me!
My converter should work: Test Results: Expected: 3.54, instead got: '3.54' Expected: 7.08, instead got: '7.08' Expected: 10.62, instead got: '10.62'
but it's still saying I have failed the tests - When I attempted it I got the same and checked all the results and they were correct. Seems to be an issue here?
The final return output should be of a number type (float) but your final output is a string. This is shown by the quotation marks around the number indicating that it is a string. You need to cast the string to a float.
Be mindful that toFixed() returns a number as a string, so you first need to convert back to a number for the return statement. The test error description for 3.54 for example that says it saw '3.54' instead of 3.54 is saying '3.54' (in single quotes) is a number string and 3.54 (not in quotes) is a number. This will fail if strictly compared using 3.54 === '3.54'. This tripped me up as a Javascript novice :)
This comment has been hidden.
I would agree with this suggestion since this is an 8kyu kata. If it were a 7kyu kata I think working with floats would be fine. In any case, I did appreciate that this used floats as it gave me a good excuse to work with something that I rarely (never XD) work with.
Java already using doubles, resolving this.
Not giving right values: converter should work, answers are wrong.
General advice:
I just completed this kata in all available languages. Only the Java version seems to have a problem (see issue below), so I'm marking this issue as resolved.
This comment has been hidden.
Your solution is almost correct. The result type is
double
, but you usefloat
for the constants, the intermediate value and the final result.You can print the exact value of
float
s anddouble
s like this:Floating point calculation can be tricky, even the tests of the Java version got it wrong. BTW: The
if
block does not modify the value stored inkmpl
, so you could just delete that.Can't pass this on C++ because of random tests. For example for input 94 result is 33.2615 = 33.27, but random test expected 33.28.
This comment has been hidden.
so strange kata for Europe
In JS I'm getting "✘ Expected: 3.54, instead got: 3.54 " using to.Fixed. There's what appears to be 2 space chars after the second 3.54 which I cannot replicate here. Something appears to be adding these two space chars? Assuming that's the problem here.
I'm getting exactly the same issue!
Make sure to round off the result to two decimal points. If the answer ends with a 0, it should be rounded off without the 0. So instead of 5.50, we should get 5.5.
...but 3.54 is rounded to 2 decimals and does not end with zero.
Please return a Number, not a String.
This comment has been hidden.
use data from description
In C++ ExampleTests
✘ Expected: equal to 12.74 Actual: 12.74
What am I missing here?
Hi martinc,
can you share your solution hier marked as spoiler?
This comment has been hidden.
The instructions are incorrect. The expected result is a floating point number (at least in Python) but the instructions implicitly callf or a string when they talk about removing the extra 0 in 5.50: "If the answer ends with a 0, it should be rounded off without the 0. So instead of 5.50, we should get 5.5." Please fix.
C++-Translation kumited!
Please check it and approve. Thanks!:)
done
{"stdout":"","stderr":"/tmp/csharp11665-17-1nt3r3j/fixture.cs(28,8): warning CS0219: The variable `test' is assigned but its value is never used\n","wallTime":0}
Can't solve issue, because of Error 500 (can't open site), so perhaps next help for next kata;-). You can try it here (normally quick help;-)): https://www.codewars.com/topics/bug-reports (posting your issue + error 500 message after "edit kata")
Cannot currently complete in C#.
The variable 'test' has been assigned but its value is never used.
Can someone repair this, I am not C#
I could just solve this in C# without problems.
This comment has been hidden.
in c# i had the same issue
In Java random test is wrong rounding
for the input 4145 is result 1467.3550763 and its rounded to 1467.36
But the randomTest expect 1467.35
Can someone proficient in Java repair this?
@emporio, I think the solution (in Java) is something along the lines of (where n is a double or float) (n*100)/100.00
I could reproduce this issue. The Java tests use floats for the constants. So the expected value before rounding of
mpgToKPM(4145)
is calculated as1467.35498046875
(instead of1467.35505046589605626650154590606689453125
if doubles were used).@emporio, @AngusJGoldsmith or anyone with write access, please fix this by replacing
in the tests with
@siebenschlaefer does this mean that this one can't be completed in java until it is fixed? My quest to finish all the 8kyu in java is going down the drain haha
Or is there a way to do it in java without using math.round or DecimalFormat?
Java Fork
deprecated API
warning by removing java.math.BigDecimalfork approved, issue fixed
Misleading method name mpgToKPM. Shouldn't it be mpgToKPL?
I'm newbie here... Is this discourse not allowing to POST the answer?
No
It means that we can share our ANSWER to anyone who doesn't take the TRAINING yet...!!!
I don't get this. Am I over complicating this or something? I can get all the tests to pass except for one. my out put is 8.50 and not 8.5. This seems silly considering it says to make sure you preserve 2 decimal points. What am I doing wrong?
Try to solve it some other way. The 0 is not necessary in mathematics so try to find a workaround that will provide 3.5 without the extra nought. I've gone and updated the kata.
I think it would be helpful to change "gallon" to "imperial gallon" everywhere it appears. When I read "gallon", I automatically think of US gallons... The "useful constants" does mention imperial gallons, which is helpful, but only if you read that section (I just lifted the constants off the web).
Thanks, I otherwise would have thought this was just a broken implementation in C#.
Done
This comment has been hidden.
String vs Number.
Please return a Number, not a String.
Expected: 3.54, instead got: 3.54 I tried converting it to a string but still the same result. I'm not sure why the test is failing?
Please, ALWAYS post the relevant language; if it is a Ruby/Python issue, post under my comment below, so that I can get notified.
Sorry about that, I'm using Javascript
Any luck with this? have the same problem.
Yes, I was returning it as a string, it was expecting a number, once I converted it the test passed.
22 Passed 23 Failed jeez
How come we are returning a string even though we just do a normal math calculation in the code? And the supposed string doesn't have a ""... Is it really not a bug?
why round ,not floor?
Because Math.floor(num) rounds num down to the nearest whole integer regardless of the decimal value. Math.ceiling(num) does the opposite; it rounds num up if it's not a whole integer. Math.round(num) decides whether to use floor or ceiling. Think of it as an if/else statement that rounds it up if the decimal is ≥ 0.5 and rounds it down else.
I am a little confused here - kilometers per liter is not a unit of measurement that is typically used, if I'm not mistaken. Wouldn't a better test case be MPG to liters per 100KM? Is this a typo?
You are right that L/100 km is a more common unit of measurement, but apparently it is not the goal of this kata, so this is not a typo.
Yes, I think creator of the kata is from US and isn't aware that others use somwhat inverse value.
Added a CoffeeScript translation.
This comment has been hidden.
To quote acraileanu from some other comments here: "Most likely this is a type issue (you return a string and the assert is expecting an integer).". Hope that helps!
hmm... interesting. Let me try that, but I'm almost positive I'm returning a float.
Alright, I figured it out... I'm not sure of why this actually happens, but it was an easier fix than I expected.
Added a C# translation as well.
Added a Java translation.
The tests internally use
float
s instead ofdouble
s which can lead to failing random tests. It's easy to fix, see the issue by @kayakeroThis comment has been hidden.
This comment has been hidden.
Most likely this is a type issue (you return a string and the assert is expecting an integer).
thanks ^^ sometimes I get lost in a glass of water... :D
This comment has been hidden.
I'm experiencing this too. This is also technically a spoiler.
Flagged!
@BenPiggot: I've gone and updated the test cases if that helps.
Still getting the same outcome - my output matches the expected output, but I fail the test anyway. Again - maybe it is a JavaScript floating point thing?
As per another comment, this is most likely a type issue (you return a string and the assert is expecting an integer).
Needs more testing.
Try now ;)
Looks better :)
When comes out of beta?
Now ;)
First one to upvote this kata and I gladly translated it into both Python and Ruby, if you may appreciate and approve them :)
Edit: oh, and let me know if you may appreciate random test cases on JS too :)
Thanks a lot! I'm going to approve your kata as soon as I know how.
Also, I'm going to imrpove my solution so that you can add your test cases.
-- > Update
Ok so I approved your kata. Solution is improved thanks to OverZealous. Waiting for your test cases.
I miss what part had OZ, but sorry to both him and you if I missed anything important; tests added :)
Not sure what value random tests adds any value. My round() doesn't seem to work with random tests.
Which language?
And I don't get your question very well: can you provide some example and, possibly, your own code with a spoiler flag?