Code Lab

Memoize decorator, factorial, combination and permutation

class memoize(dict): def __init__(self, func): dict.__init__(self) self.func = func def __call__(self, *args): return self[args] def __missing__(self, key): result = self[key] = self.func(*key) return result @memoize def factorial(n): if n < 2: return n else: return n * factorial(n - 1) def combination(n, k): return factorial(n) // factorial(n - k) def permutation(n, k): return combination(n, k) // factorial(k) print(factorial(5), combination(33, 6), permutation(33, 6))
:
Input
Input Data (stdin):
Output
120 797448960 1107568
Lint
Position Message Level
1:7 class name 'memoize' should use CapWords convention info
16:5 Unnecessary "else" after "return" info