From 49654da954d843f7b78747f620d342960e5e61a7 Mon Sep 17 00:00:00 2001 From: Sanders Lin <45224617+SandersLin@users.noreply.github.com> Date: Sat, 18 Apr 2020 00:23:36 +0800 Subject: [PATCH 1/4] Update factorial.py --- dynamic_programming/factorial.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/dynamic_programming/factorial.py b/dynamic_programming/factorial.py index 0269014e7a18..2bb4a7e205a0 100644 --- a/dynamic_programming/factorial.py +++ b/dynamic_programming/factorial.py @@ -1,6 +1,4 @@ # Factorial of a number using memoization -result = [-1] * 10 -result[0] = result[1] = 1 def factorial(num): @@ -12,26 +10,26 @@ def factorial(num): >>> [factorial(i) for i in range(5)] [1, 1, 2, 6, 24] """ + result = [-1] * (num + 1) if num < 0: return "Number should not be negative." + + return factorial_aux(num, result) + + +def factorial_aux(num, result): + + if num == 0 or num == 1: + return 1 + if result[num] != -1: return result[num] else: - result[num] = num * factorial(num - 1) - # uncomment the following to see how recalculations are avoided - # print(result) + result[num] = num * factorial_aux(num - 1, result) return result[num] -# factorial of num -# uncomment the following to see how recalculations are avoided -##result=[-1]*10 -##result[0]=result[1]=1 -##print(factorial(5)) -# print(factorial(3)) -# print(factorial(7)) - if __name__ == "__main__": import doctest From e95fb724c06e0fcfe13a1a39eb3c064e3b84197c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 23 Jun 2020 09:33:43 +0000 Subject: [PATCH 2/4] updating DIRECTORY.md --- DIRECTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 4ffd20da58de..a12ed78d64f2 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -16,6 +16,7 @@ * [All Subsequences](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_subsequences.py) * [Coloring](https://github.com/TheAlgorithms/Python/blob/master/backtracking/coloring.py) * [Hamiltonian Cycle](https://github.com/TheAlgorithms/Python/blob/master/backtracking/hamiltonian_cycle.py) + * [Knight Tour](https://github.com/TheAlgorithms/Python/blob/master/backtracking/knight_tour.py) * [Minimax](https://github.com/TheAlgorithms/Python/blob/master/backtracking/minimax.py) * [N Queens](https://github.com/TheAlgorithms/Python/blob/master/backtracking/n_queens.py) * [Rat In Maze](https://github.com/TheAlgorithms/Python/blob/master/backtracking/rat_in_maze.py) @@ -71,6 +72,8 @@ ## Compression * [Burrows Wheeler](https://github.com/TheAlgorithms/Python/blob/master/compression/burrows_wheeler.py) * [Huffman](https://github.com/TheAlgorithms/Python/blob/master/compression/huffman.py) + * [Lempel Ziv](https://github.com/TheAlgorithms/Python/blob/master/compression/lempel_ziv.py) + * [Lempel Ziv Decompress](https://github.com/TheAlgorithms/Python/blob/master/compression/lempel_ziv_decompress.py) * [Peak Signal To Noise Ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py) ## Computer Vision @@ -199,6 +202,7 @@ * [Longest Increasing Subsequence O(Nlogn)](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_increasing_subsequence_o(nlogn).py) * [Longest Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_sub_array.py) * [Matrix Chain Order](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/matrix_chain_order.py) + * [Max Non Adjacent Sum](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_non_adjacent_sum.py) * [Max Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sub_array.py) * [Max Sum Contiguous Subsequence](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sum_contiguous_subsequence.py) * [Minimum Partition](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/minimum_partition.py) From b7ecbf636037564f915fc826c16004e6bd3cda93 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 5 Aug 2020 12:41:29 +0200 Subject: [PATCH 3/4] Update dynamic_programming/factorial.py --- dynamic_programming/factorial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/factorial.py b/dynamic_programming/factorial.py index bae219361422..aab8cf518026 100644 --- a/dynamic_programming/factorial.py +++ b/dynamic_programming/factorial.py @@ -1,7 +1,7 @@ # Factorial of a number using memoization -def factorial(num): +def factorial(num: int) -> int: """ >>> factorial(7) 5040 From 6a038a15a1fd5b3291a26cb547f617f9c8eee519 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 5 Aug 2020 12:53:44 +0200 Subject: [PATCH 4/4] Update factorial.py --- dynamic_programming/factorial.py | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/dynamic_programming/factorial.py b/dynamic_programming/factorial.py index aab8cf518026..1c9c927f5af3 100644 --- a/dynamic_programming/factorial.py +++ b/dynamic_programming/factorial.py @@ -1,35 +1,24 @@ # Factorial of a number using memoization +from functools import lru_cache + +@lru_cache def factorial(num: int) -> int: """ >>> factorial(7) 5040 - >>> factorial(-1) - 'Number should not be negative.' - - >>> [factorial(i) for i in range(5)] - [1, 1, 2, 6, 24] + Traceback (most recent call last): + ... + ValueError: Number should not be negative. + >>> [factorial(i) for i in range(10)] + [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880] """ - result = [-1] * (num + 1) - if num < 0: - return "Number should not be negative." - - return factorial_aux(num, result) - - -def factorial_aux(num, result): - - if num == 0 or num == 1: - return 1 + raise ValueError("Number should not be negative.") - if result[num] != -1: - return result[num] - else: - result[num] = num * factorial_aux(num - 1, result) - return result[num] + return 1 if num in (0, 1) else num * factorial(num - 1) if __name__ == "__main__":