@Alexenon Yes I did. The sequence starts at n and you genenerate new values until a value is already seen in the sequence. Pick the maximum of them. This should work even for big (long) numbers.
@jrico, i changed from return max to return Long.max(n, max) and still my algorithm can't find the maximum value in a sequence for numbers bigger than 1_000_000
I am returning maximum value when same number occurences second time in the sequence. You did the same ?
When does a sequence ends ? My algorithm finds max until same number is generated second time.
I pass 20 tests and then fails due my maximum is smaller than expected. Could someone please explain me when method should return maximum value :)
The idea is that we calc each next number manually; we know that next number is one of 3 possible routes: to increase 1st expo, 2nd or 3rd by 1. So we calc all 3 numbers (what they will become with increased expo and pick minimal), we increase expos of taken step, save number and repeat all again.
Not that interesting to create a function just to encapsulate a call to another method although I agree on the fact that test cases in example can be inserted in a list with an explicit error message.
Test cases in Python would be better if they were factorized to avoid repetitions. It's easy to create a generic function dotest that does the job for all tests, fixed and random. That would lighten the tests page and would be much more readable. Something like:
defdotest(n, expected):
actual=user_function(inputs)
test.assert_equals(actual, expected, f"Test failed with n = {n}")
# then just do in the tests:dotest(n, expected)
1/ you didn't add the tests with small numbers to the test cases part
2/ you didn't add 1 or 2 tests with big number to the sample tests part
3/ I didn't notice before, but you have to use the new test framework, for python
@Alexenon Yes I did. The sequence starts at n and you genenerate new values until a value is already seen in the sequence. Pick the maximum of them. This should work even for big (long) numbers.
@jrico, i changed from
return max
toreturn Long.max(n, max)
and still my algorithm can't find the maximum value in a sequence for numbers bigger than 1_000_000I am returning maximum value when same number occurences second time in the sequence. You did the same ?
Most likely, you are not including the initial n value. It also happened to me :)
When does a sequence ends ? My algorithm finds max until same number is generated second time.
I pass 20 tests and then fails due my maximum is smaller than expected. Could someone please explain me when method should return maximum value :)
expected:<615043> but was:<1230086>
The idea is that we calc each next number manually; we know that next number is one of 3 possible routes: to increase 1st expo, 2nd or 3rd by 1. So we calc all 3 numbers (what they will become with increased expo and pick minimal), we increase expos of taken step, save number and repeat all again.
Not that interesting to create a function just to encapsulate a call to another method although I agree on the fact that test cases in example can be inserted in a list with an explicit error message.
You can find this Math StackExchange post interesting
How would you prove that it always generates a cycle?
Note that there are interesting cycles, for instance from 4668999 we get:
Test cases in Python would be better if they were factorized to avoid repetitions. It's easy to create a generic function
dotest
that does the job for all tests, fixed and random. That would lighten the tests page and would be much more readable. Something like:You should add in description that this procedure always generates a cycle.
I have added tests with small numbers to test cases, big numbers to sample tests, and am now using the test framework for python.
side note: if the test cases get run alphabetically (by their description) it's best to assure those smaller tests are run before the biger ones
1/ you didn't add the tests with small numbers to the test cases part
2/ you didn't add 1 or 2 tests with big number to the sample tests part
3/ I didn't notice before, but you have to use the new test framework, for python
.
Loading more items...