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 an L 45762893920L will work.
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 and navg * 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.
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:
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:
private static final DateTimeFormatter PARSER = DateTimeFormatter.ofPattern("H[H]|m[m]|s[s]");
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("HH|mm|ss");
"Substrings in the input string are separated by , or ,." as per description.
But there is no test case for that in kotlin, no test with an input string without whitespace like "01|15|59,1|47|16,01|17|20,1|32|34,2|17|17"
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}
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."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.
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.
I'm talking about tests WITHOUT white space, and there isn't such tests.
"Substrings in the input string are separated by
,
or,
." as per description.But there is no test case for that in kotlin, no test with an input string without whitespace like
"01|15|59,1|47|16,01|17|20,1|32|34,2|17|17"
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Loading more items...