You should 'spread out' your code more, k==0
kinda looks like a variable,
and if k==0: return n==0
doesn't really look like an if statement at first.
Single letter variable names are bad, amount_bottles
is better then n
.
You could even use bottles
and it would still be better.
T
is not an allowed name for a function according to PEP8 Python's style guide.
Capital letters are only used in constants, and classes.
As for your performance concern,
I would recommend using memoization in a wrapper.
memoization remembers output, and so you re-use the output rather then re-find it.
It's really good in recursive functions.
def memoize(fn):
outputs = {}
def inner(*args):
try:
return outputs[args]
except KeyError:
outputs[args] = fn(*args)
return outputs[args]
return inner
def permutations(bottles, crates, BOTTLES_CRATE):
@memoize
def inner(bottles, crates):
if crates == 0:
return bottles == 0
crates -= 1
return sum(
inner(bottles - i, crates)
for i in xrange(BOTTLES_CRATE[crates] + 1)
)
return inner(bottles, crates)
print(permutations(30, 20, range(1, 21)))
This took under a second to get 6209623185136
.
Just to note, memoization requires no side effects to happen.
So if you use an outside variable then that can break the memoization.
For example, if we were to memoize addition of a number plus a global variable,
it is prone to breakage.
inc = 4
@memoize
def add(num):
return num + inc
add(4) # 8
inc = 0
add(4) # 8
add(0) # 0
If you were to want to expand on your above code,
and you make BOTTLES_CRATE
a variable,
then you would have to pass BOTTLES_CRATE
via the memoizer too.
Just like we should inc
in the above.
@memoize
def add(a, b):
return a + b
add(4, 4) # 8
add(4, 0) # 4
As I use a dictionary to store the arguments,
you will need to cast BOTTLES_CRATE
to a hash-able type, e.g. a tuple.
This may lead to a non-transparent solution, where you can just add @memoize
.
Another bad use-case is if add
'returns' from it's internals to another variable.
list_ = []
@memoize
def push(num):
list_.append(num)
push(2) # list_ == [2]
push(2) # list_ == [2]
And so if there are possible side effects, you may want to chose a different method in future.