Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
First element of the array should be 0; you had -6. So your code is wrong.
hint: operator precedence.
45762893920
is bigger than Integer.MAX_VALUE and java can't use it as an int. You have to tell java to treat it as a long by adding anL
45762893920L
will work.This comment is hidden because it contains spoiler information about the solution
It fails since digitize(0L) will return an empty array instead of returning {0}
Thanks for introducing me to the stream methods available, really nice pattern.
Approved by author secretly
This comment is hidden because it contains spoiler information about the solution
I agree that the division introduces a loss of precision, that is the problem when calculating with floating numbers. And that's why it is recommended to compare doubles with an offset.
Mathematically
(navg - (sum / newSize)) * newSize
andnavg * newSize - sum
are the same, the later being the simplified version. I unfortunately did not saw at first sight that it could be simplified. That was my mistake.I looked at your solution. I am rather sure that the problem comes from your division (division introduces an added error, error "multiplied" by a multiplication) in your calculation of "newAvg". I re-wrote that line and then your solution passes every time.
"navg = 115053.00000000001" is the input, not the result of the function.
The random tests are failing randomly with my solution.
The test randomFailure above which I extracted from the tests is failing everytime with my solution.
I had to modify the testing method like explained above to make it pass.
Thanks for your work! I see that you finally passed the kata. The description says:
The answer must be an integer so I don't see a reason to use a tolerance.
To get "navg = 115053.00000000001" I suppose that in a first approach you didn't round up(?).
There is definitely rounding issues in java (and probably other language) (for example if you don't simplify the calculation like my solution, sorry)
Here is an example of a failing random test that I have extracted through my solution:
and her the output of that test:
My suggestion to avoid that is that the testing method should recalculate the average from the input and then doing a comparison with delta if expected and actual are not equal:
very good solution,
but you could also have used your formatter
DTF
for the parsing, instead splitting on the PIPE:LocalTime.parse(s, DTF).toSecondOfDay
sorry, I didn't saw that the inputs have randomly 1 or two numbers, so the Formatter can't be used for Parsing; you would need a FORMATTER and a PARSER, where the PARSER will use optional section:
Ok, thanks for your reply.
It's all clear now.
Sorry I didn't understand your post.
When I wrote: Substrings in the input string are separated by
,
or,
" I didn't mean that in each language there would be the two types of separator; I meant that the types could be one or the other depending on the language. It happens that Kotlin has the first type of separator and Swift has the other type. Anyway with a regex (as yo did) the difference is not a problem. I added that sentence after a post from ByteEater that you can see below.Loading more items...