From 5c4b0aab8eceb5f8b939f98c279f277eab70106e Mon Sep 17 00:00:00 2001 From: Rolv Apneseth Date: Sat, 24 Oct 2020 00:58:21 +0100 Subject: [PATCH 1/4] Made improvements to combinations.py --- maths/combinations.py | 53 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/maths/combinations.py b/maths/combinations.py index fd98992e6c16..8e7b34f3e8c4 100644 --- a/maths/combinations.py +++ b/maths/combinations.py @@ -1,19 +1,66 @@ +""" +This function will return the total number of different combinations of +k length which can be made from n values. + +Wikipedia page here: https://en.wikipedia.org/wiki/Combination + +For doctests, run: `python3 -m doctest -v combinations.py` + +Run `python3 combinations.py` for print statements which demonstrate use +cases for this function. +""" from math import factorial -def combinations(n, k): +def combinations(n: int, k: int) -> int: """ + Returns the number of different combinations of k length which can + be made from n values, where n >= k. + + Examples: >>> combinations(10,5) 252 + >>> combinations(6,3) 20 + >>> combinations(20,5) 15504 + + >>> combinations(52, 5) + 2598960 + + >>> combinations(0, 0) + 1 + + >>> combinations(-4, -5) + Invalid value for n and/or k given. + -1 """ + + # If either of the conditions are true, the function is being asked + # to calculate a factorial of a negative number, which is not possible + if n < k or k < 0: + print("Invalid value for n and/or k given.") + return -1 return int(factorial(n) / ((factorial(k)) * (factorial(n - k)))) if __name__ == "__main__": - from doctest import testmod - testmod() + print( + "\nThe number of five-card hands possible from a standard", + f"fifty-two card deck is: {combinations(52, 5)}", + ) + + print( + "\nIf a class of 40 students must be arranged into groups of", + f"4 for group projects, there are {combinations(40, 4)} ways", + "to arrange them.\n", + ) + + print( + "If 10 teams are competing in a Formula One race, there", + f"are {combinations(10, 3)} ways that first, second and", + "third place can be awarded.\n", + ) From 2e8812768336fb071e2a4b0583b00ccf4f42567a Mon Sep 17 00:00:00 2001 From: Rolv Apneseth Date: Sat, 24 Oct 2020 21:14:39 +0100 Subject: [PATCH 2/4] Update maths/combinations.py Co-authored-by: Du Yuanchao --- maths/combinations.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/maths/combinations.py b/maths/combinations.py index 8e7b34f3e8c4..a5b7ae79b204 100644 --- a/maths/combinations.py +++ b/maths/combinations.py @@ -1,13 +1,5 @@ """ -This function will return the total number of different combinations of -k length which can be made from n values. - -Wikipedia page here: https://en.wikipedia.org/wiki/Combination - -For doctests, run: `python3 -m doctest -v combinations.py` - -Run `python3 combinations.py` for print statements which demonstrate use -cases for this function. +https://en.wikipedia.org/wiki/Combination """ from math import factorial From f24ceb0688f0bc6836b67e27a0c1a5092c080b09 Mon Sep 17 00:00:00 2001 From: Rolv Apneseth Date: Sun, 25 Oct 2020 15:50:24 +0000 Subject: [PATCH 3/4] Function now raises an error when given invalid input --- maths/combinations.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/maths/combinations.py b/maths/combinations.py index a5b7ae79b204..ee3a1878d07e 100644 --- a/maths/combinations.py +++ b/maths/combinations.py @@ -26,15 +26,14 @@ def combinations(n: int, k: int) -> int: 1 >>> combinations(-4, -5) - Invalid value for n and/or k given. - -1 + Traceback (most recent call last): + ValueError: Please enter positive integers for n and k where n >= k """ # If either of the conditions are true, the function is being asked # to calculate a factorial of a negative number, which is not possible if n < k or k < 0: - print("Invalid value for n and/or k given.") - return -1 + raise ValueError("Please enter positive integers for n and k where n >= k") return int(factorial(n) / ((factorial(k)) * (factorial(n - k)))) From 985d7aae7fe223b000f88806511571c968b7bbe8 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Mon, 26 Oct 2020 14:42:09 +0800 Subject: [PATCH 4/4] Update maths/combinations.py --- maths/combinations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/combinations.py b/maths/combinations.py index ee3a1878d07e..40f4f7a9f850 100644 --- a/maths/combinations.py +++ b/maths/combinations.py @@ -26,6 +26,7 @@ def combinations(n: int, k: int) -> int: 1 >>> combinations(-4, -5) + ... Traceback (most recent call last): ValueError: Please enter positive integers for n and k where n >= k """