From 859c63397e194efeb0f3e35db038e3082a458ddd Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:14:33 +0530 Subject: [PATCH 01/11] Create minimums_squares_to_represent_a_number.py added a dynamic programming approach of finding the minimum number of square to represent a number. eg : 25 = 5*5 37 = 6*6 + 1*1 21 = 4*4 + 2*2 + 1*1 --- .../minimums_squares_to_represent_a_number.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 dynamic_programming/minimums_squares_to_represent_a_number.py diff --git a/dynamic_programming/minimums_squares_to_represent_a_number.py b/dynamic_programming/minimums_squares_to_represent_a_number.py new file mode 100644 index 000000000000..71ef804eac31 --- /dev/null +++ b/dynamic_programming/minimums_squares_to_represent_a_number.py @@ -0,0 +1,35 @@ +import math,sys + +def minimum_squares_to_represent_a_number(number: int) -> int: + """ + Count the number of minimum squares to represent a number + >>> minimum_squares_to_represent_a_number(25) + 1 + >>> minimum_squares_to_represent_a_number(37) + 2 + >>> minimum_squares_to_represent_a_number(21) + 3 + >>> minimum_squares_to_represent_a_number(58) + 2 + >>> minimum_squares_to_represent_a_number(-1) + Traceback (most recent call last): + ... + ValueError: the value of input must be positive + """ + if number < 0: + raise ValueError("the value of input must be positive") + dp = [-1 for x in range(number+1)] + dp[0] = 0 + for i in range(1,number+1): + ans = sys.maxsize + root = int(math.sqrt(i)) + for j in range(1,root+1): + currAns = 1 + dp[i-(j**2)] + ans = min(ans,currAns) + dp[i] = ans + return dp[number] + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 3c62fc8fbd8d842e90885b9a45353e1ba6afb36e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 11:46:18 +0000 Subject: [PATCH 02/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../minimums_squares_to_represent_a_number.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/minimums_squares_to_represent_a_number.py b/dynamic_programming/minimums_squares_to_represent_a_number.py index 71ef804eac31..d41344576d9c 100644 --- a/dynamic_programming/minimums_squares_to_represent_a_number.py +++ b/dynamic_programming/minimums_squares_to_represent_a_number.py @@ -1,4 +1,6 @@ -import math,sys +import math +import sys + def minimum_squares_to_represent_a_number(number: int) -> int: """ @@ -18,17 +20,18 @@ def minimum_squares_to_represent_a_number(number: int) -> int: """ if number < 0: raise ValueError("the value of input must be positive") - dp = [-1 for x in range(number+1)] + dp = [-1 for x in range(number + 1)] dp[0] = 0 - for i in range(1,number+1): + for i in range(1, number + 1): ans = sys.maxsize root = int(math.sqrt(i)) - for j in range(1,root+1): - currAns = 1 + dp[i-(j**2)] - ans = min(ans,currAns) + for j in range(1, root + 1): + currAns = 1 + dp[i - (j**2)] + ans = min(ans, currAns) dp[i] = ans return dp[number] + if __name__ == "__main__": import doctest From 164ad931edf0c77b23f22923343f91547e648ab6 Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:17:43 +0530 Subject: [PATCH 03/11] Update and rename minimums_squares_to_represent_a_number.py to minimum_squares_to_represent_a_number.py updated the code --- ...t_a_number.py => minimum_squares_to_represent_a_number.py} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename dynamic_programming/{minimums_squares_to_represent_a_number.py => minimum_squares_to_represent_a_number.py} (92%) diff --git a/dynamic_programming/minimums_squares_to_represent_a_number.py b/dynamic_programming/minimum_squares_to_represent_a_number.py similarity index 92% rename from dynamic_programming/minimums_squares_to_represent_a_number.py rename to dynamic_programming/minimum_squares_to_represent_a_number.py index d41344576d9c..03ca40f0109c 100644 --- a/dynamic_programming/minimums_squares_to_represent_a_number.py +++ b/dynamic_programming/minimum_squares_to_represent_a_number.py @@ -26,8 +26,8 @@ def minimum_squares_to_represent_a_number(number: int) -> int: ans = sys.maxsize root = int(math.sqrt(i)) for j in range(1, root + 1): - currAns = 1 + dp[i - (j**2)] - ans = min(ans, currAns) + curr_ans = 1 + dp[i - (j**2)] + ans = min(ans, curr_ans) dp[i] = ans return dp[number] From f149bb64e74cd855fc671c53855082c7618085ea Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Wed, 26 Oct 2022 09:03:53 +0530 Subject: [PATCH 04/11] Update minimum_squares_to_represent_a_number.py I have added the appropriate checks for 0 and 12.34. It would be great if you could suggest a name for the dp array --- .../minimum_squares_to_represent_a_number.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/dynamic_programming/minimum_squares_to_represent_a_number.py b/dynamic_programming/minimum_squares_to_represent_a_number.py index 03ca40f0109c..39d8cec60531 100644 --- a/dynamic_programming/minimum_squares_to_represent_a_number.py +++ b/dynamic_programming/minimum_squares_to_represent_a_number.py @@ -1,7 +1,3 @@ -import math -import sys - - def minimum_squares_to_represent_a_number(number: int) -> int: """ Count the number of minimum squares to represent a number @@ -17,10 +13,20 @@ def minimum_squares_to_represent_a_number(number: int) -> int: Traceback (most recent call last): ... ValueError: the value of input must be positive + >>> minimum_squares_to_represent_a_number(0) + 1 + >>> minimum_squares_to_represent_a_number(12.34) + Traceback (most recent call last): + ... + ValueError: the value of input must be a natural number """ + if number != int(number): + raise ValueError ("the value of input must be a natural number") if number < 0: raise ValueError("the value of input must be positive") - dp = [-1 for x in range(number + 1)] + if number == 0: + return 1 + dp = [-1] * (number + 1) dp[0] = 0 for i in range(1, number + 1): ans = sys.maxsize @@ -34,5 +40,3 @@ def minimum_squares_to_represent_a_number(number: int) -> int: if __name__ == "__main__": import doctest - - doctest.testmod() From 6137e42ce1661705b7cf314cecbc42ddcf6f5076 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 03:34:54 +0000 Subject: [PATCH 05/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/minimum_squares_to_represent_a_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/minimum_squares_to_represent_a_number.py b/dynamic_programming/minimum_squares_to_represent_a_number.py index 39d8cec60531..408d798241f5 100644 --- a/dynamic_programming/minimum_squares_to_represent_a_number.py +++ b/dynamic_programming/minimum_squares_to_represent_a_number.py @@ -21,7 +21,7 @@ def minimum_squares_to_represent_a_number(number: int) -> int: ValueError: the value of input must be a natural number """ if number != int(number): - raise ValueError ("the value of input must be a natural number") + raise ValueError("the value of input must be a natural number") if number < 0: raise ValueError("the value of input must be positive") if number == 0: From cdde8629b9c4010a8021d537bcfd9b2698dc6310 Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Wed, 26 Oct 2022 09:08:23 +0530 Subject: [PATCH 06/11] Update minimum_squares_to_represent_a_number.py --- .../minimum_squares_to_represent_a_number.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/minimum_squares_to_represent_a_number.py b/dynamic_programming/minimum_squares_to_represent_a_number.py index 408d798241f5..601a0142b4a7 100644 --- a/dynamic_programming/minimum_squares_to_represent_a_number.py +++ b/dynamic_programming/minimum_squares_to_represent_a_number.py @@ -1,3 +1,5 @@ +import math +import sys def minimum_squares_to_represent_a_number(number: int) -> int: """ Count the number of minimum squares to represent a number @@ -21,7 +23,7 @@ def minimum_squares_to_represent_a_number(number: int) -> int: ValueError: the value of input must be a natural number """ if number != int(number): - raise ValueError("the value of input must be a natural number") + raise ValueError ("the value of input must be a natural number") if number < 0: raise ValueError("the value of input must be positive") if number == 0: @@ -40,3 +42,5 @@ def minimum_squares_to_represent_a_number(number: int) -> int: if __name__ == "__main__": import doctest + + doctest.testmod() From 53d094033c03ed1a4539a44bac876573643499ed Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 03:39:24 +0000 Subject: [PATCH 07/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../minimum_squares_to_represent_a_number.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/minimum_squares_to_represent_a_number.py b/dynamic_programming/minimum_squares_to_represent_a_number.py index 601a0142b4a7..b25339c51b90 100644 --- a/dynamic_programming/minimum_squares_to_represent_a_number.py +++ b/dynamic_programming/minimum_squares_to_represent_a_number.py @@ -1,5 +1,7 @@ -import math +import math import sys + + def minimum_squares_to_represent_a_number(number: int) -> int: """ Count the number of minimum squares to represent a number @@ -23,7 +25,7 @@ def minimum_squares_to_represent_a_number(number: int) -> int: ValueError: the value of input must be a natural number """ if number != int(number): - raise ValueError ("the value of input must be a natural number") + raise ValueError("the value of input must be a natural number") if number < 0: raise ValueError("the value of input must be positive") if number == 0: @@ -42,5 +44,5 @@ def minimum_squares_to_represent_a_number(number: int) -> int: if __name__ == "__main__": import doctest - + doctest.testmod() From 929fe5bfa74362cf6b392419717f09fb2eed6949 Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:23:47 +0530 Subject: [PATCH 08/11] Update minimum_squares_to_represent_a_number.py updated --- .../minimum_squares_to_represent_a_number.py | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/dynamic_programming/minimum_squares_to_represent_a_number.py b/dynamic_programming/minimum_squares_to_represent_a_number.py index b25339c51b90..d92eda9236e8 100644 --- a/dynamic_programming/minimum_squares_to_represent_a_number.py +++ b/dynamic_programming/minimum_squares_to_represent_a_number.py @@ -1,7 +1,6 @@ import math import sys - def minimum_squares_to_represent_a_number(number: int) -> int: """ Count the number of minimum squares to represent a number @@ -27,20 +26,19 @@ def minimum_squares_to_represent_a_number(number: int) -> int: if number != int(number): raise ValueError("the value of input must be a natural number") if number < 0: - raise ValueError("the value of input must be positive") + raise ValueError("the value of input must not be a negative number") if number == 0: return 1 - dp = [-1] * (number + 1) - dp[0] = 0 - for i in range(1, number + 1): - ans = sys.maxsize + answers = [-1] * (number+1) + answers[0] = 0 + for i in range(1,number+1): + answer = sys.maxsize root = int(math.sqrt(i)) - for j in range(1, root + 1): - curr_ans = 1 + dp[i - (j**2)] - ans = min(ans, curr_ans) - dp[i] = ans - return dp[number] - + for j in range(1,root+1): + current_answer = 1 + answers[i-(j**2)] + answer = min(answer,current_answer) + answers[i] = answer + return answers[number] if __name__ == "__main__": import doctest From 776e38938b2849536c4f099e9eb4b635bf53804b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 13:54:43 +0000 Subject: [PATCH 09/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../minimum_squares_to_represent_a_number.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/minimum_squares_to_represent_a_number.py b/dynamic_programming/minimum_squares_to_represent_a_number.py index d92eda9236e8..0ebca0a30041 100644 --- a/dynamic_programming/minimum_squares_to_represent_a_number.py +++ b/dynamic_programming/minimum_squares_to_represent_a_number.py @@ -1,6 +1,7 @@ import math import sys + def minimum_squares_to_represent_a_number(number: int) -> int: """ Count the number of minimum squares to represent a number @@ -29,17 +30,18 @@ def minimum_squares_to_represent_a_number(number: int) -> int: raise ValueError("the value of input must not be a negative number") if number == 0: return 1 - answers = [-1] * (number+1) + answers = [-1] * (number + 1) answers[0] = 0 - for i in range(1,number+1): + for i in range(1, number + 1): answer = sys.maxsize root = int(math.sqrt(i)) - for j in range(1,root+1): - current_answer = 1 + answers[i-(j**2)] - answer = min(answer,current_answer) + for j in range(1, root + 1): + current_answer = 1 + answers[i - (j**2)] + answer = min(answer, current_answer) answers[i] = answer return answers[number] + if __name__ == "__main__": import doctest From dbaa6062fc5e75642732d141bbef62dd8ba725da Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:28:08 +0530 Subject: [PATCH 10/11] Update minimum_squares_to_represent_a_number.py updated --- .../minimum_squares_to_represent_a_number.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/dynamic_programming/minimum_squares_to_represent_a_number.py b/dynamic_programming/minimum_squares_to_represent_a_number.py index 0ebca0a30041..35321e8551d1 100644 --- a/dynamic_programming/minimum_squares_to_represent_a_number.py +++ b/dynamic_programming/minimum_squares_to_represent_a_number.py @@ -1,6 +1,4 @@ -import math -import sys - +import math,sys def minimum_squares_to_represent_a_number(number: int) -> int: """ @@ -16,7 +14,7 @@ def minimum_squares_to_represent_a_number(number: int) -> int: >>> minimum_squares_to_represent_a_number(-1) Traceback (most recent call last): ... - ValueError: the value of input must be positive + ValueError: the value of input must not be a negative number >>> minimum_squares_to_represent_a_number(0) 1 >>> minimum_squares_to_represent_a_number(12.34) @@ -30,18 +28,17 @@ def minimum_squares_to_represent_a_number(number: int) -> int: raise ValueError("the value of input must not be a negative number") if number == 0: return 1 - answers = [-1] * (number + 1) + answers = [-1] * (number+1) answers[0] = 0 - for i in range(1, number + 1): + for i in range(1,number+1): answer = sys.maxsize root = int(math.sqrt(i)) - for j in range(1, root + 1): - current_answer = 1 + answers[i - (j**2)] - answer = min(answer, current_answer) + for j in range(1,root+1): + current_answer = 1 + answers[i-(j**2)] + answer = min(answer,current_answer) answers[i] = answer return answers[number] - if __name__ == "__main__": import doctest From 04b5374760652c0e49441cd4723001b61ff113fe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 13:59:03 +0000 Subject: [PATCH 11/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../minimum_squares_to_represent_a_number.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/minimum_squares_to_represent_a_number.py b/dynamic_programming/minimum_squares_to_represent_a_number.py index 35321e8551d1..bf5849f5bcb3 100644 --- a/dynamic_programming/minimum_squares_to_represent_a_number.py +++ b/dynamic_programming/minimum_squares_to_represent_a_number.py @@ -1,4 +1,6 @@ -import math,sys +import math +import sys + def minimum_squares_to_represent_a_number(number: int) -> int: """ @@ -28,17 +30,18 @@ def minimum_squares_to_represent_a_number(number: int) -> int: raise ValueError("the value of input must not be a negative number") if number == 0: return 1 - answers = [-1] * (number+1) + answers = [-1] * (number + 1) answers[0] = 0 - for i in range(1,number+1): + for i in range(1, number + 1): answer = sys.maxsize root = int(math.sqrt(i)) - for j in range(1,root+1): - current_answer = 1 + answers[i-(j**2)] - answer = min(answer,current_answer) + for j in range(1, root + 1): + current_answer = 1 + answers[i - (j**2)] + answer = min(answer, current_answer) answers[i] = answer return answers[number] + if __name__ == "__main__": import doctest