Regarding optimizing: before spending lots of time on this kata, you should know that this is a very difficult 3 kyu rating - perhaps more difficult than some 2 kyu katas, depending on your previous knowledge.
Some of the inputs involve values of n > 10**200, which means that if your approach involves "trying all possible combinations up to n" in some way, you will definitely time out.
If you don't get any good ideas straight away, don't worry - my best advice would be that you will learn some of the techniques needed for this one by solving the 5 & 4 kyu mathematics and number theory tagged katas on the site first and returning to this one after.
I thought that it was impossible to use numbers less than three, because otherwise we would have 0 in the denominator or the factorial of a negative number. Now it works, but now I need to optimize the code somehow, since it runs for more than 12 seconds
Hi @Rimanagi - what is happening is that if your current solution loop never reaches the i + j + k == n step, it will never return a result. In that case your function returns None.
To avoid this, add a fail-safe return statement at the bottom of your function - something like:
return [-1,-1,-1]
That way the tests will not fail due to your function not returning a list.
edit btw the reason this is happening is that your code is starting with q = 3 while in fact the solution(s) for n = 69 all require f(2):
69 = f(0) + f(2) + f(11) = f(2) + f(6) + f(9) <- taken from Description
So replace q=3 by q=1 and you should be able to pass the sample tests.
OP solved it, closing
This comment is hidden because it contains spoiler information about the solution
Nice! It looks like resolved now. :)
approved
@Rimanagi You're welcome!
Regarding optimizing: before spending lots of time on this kata, you should know that this is a very difficult 3 kyu rating - perhaps more difficult than some 2 kyu katas, depending on your previous knowledge.
Some of the inputs involve values of
n > 10**200
, which means that if your approach involves "trying all possible combinations up ton
" in some way, you will definitely time out.If you don't get any good ideas straight away, don't worry - my best advice would be that you will learn some of the techniques needed for this one by solving the 5 & 4 kyu
mathematics
andnumber theory
tagged katas on the site first and returning to this one after.Thank you @benjaminzwhite
I thought that it was impossible to use numbers less than three, because otherwise we would have 0 in the denominator or the factorial of a negative number. Now it works, but now I need to optimize the code somehow, since it runs for more than 12 seconds
Hi @Rimanagi - what is happening is that if your current solution loop never reaches the
i + j + k == n
step, it will never return a result. In that case your function returnsNone
.To avoid this, add a fail-safe return statement at the bottom of your function - something like:
return [-1,-1,-1]
That way the tests will not fail due to your function not returning a list.
edit btw the reason this is happening is that your code is starting with
q = 3
while in fact the solution(s) forn = 69
all requiref(2)
:69 = f(0) + f(2) + f(11) = f(2) + f(6) + f(9)
<- taken from DescriptionSo replace
q=3
byq=1
and you should be able to pass the sample tests.This comment is hidden because it contains spoiler information about the solution