From fd3a7a57a10bf3964464fe6cdaf1ecc12759789c Mon Sep 17 00:00:00 2001 From: Usajjad123 Date: Tue, 12 Oct 2021 05:08:48 +0500 Subject: [PATCH 1/5] Added solution for Project Euler problem 90 --- project_euler/problem_090/__init__.py | 0 project_euler/problem_090/sol1.py | 56 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 project_euler/problem_090/__init__.py create mode 100644 project_euler/problem_090/sol1.py diff --git a/project_euler/problem_090/__init__.py b/project_euler/problem_090/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_090/sol1.py b/project_euler/problem_090/sol1.py new file mode 100644 index 000000000000..5ccc366f7c16 --- /dev/null +++ b/project_euler/problem_090/sol1.py @@ -0,0 +1,56 @@ +from itertools import permutations, combinations + +def setToBit(s): + """ + returns bit representation of a given iterable, preferably set + """ + res = 0 + for i in s: + res |= 1< Date: Tue, 12 Oct 2021 05:20:46 +0500 Subject: [PATCH 2/5] fixed naming conventions and added comments --- project_euler/problem_090/sol1.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/project_euler/problem_090/sol1.py b/project_euler/problem_090/sol1.py index 5ccc366f7c16..ce178391a5b4 100644 --- a/project_euler/problem_090/sol1.py +++ b/project_euler/problem_090/sol1.py @@ -1,34 +1,39 @@ from itertools import permutations, combinations -def setToBit(s): +def set_to_bit(s): """ returns bit representation of a given iterable, preferably set + @param s - set representing the 1 bits """ res = 0 for i in s: res |= 1< Date: Tue, 12 Oct 2021 06:02:03 +0500 Subject: [PATCH 3/5] added type hints, doctests, and problem statement --- project_euler/problem_090/sol1.py | 87 +++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 22 deletions(-) diff --git a/project_euler/problem_090/sol1.py b/project_euler/problem_090/sol1.py index ce178391a5b4..cac5ea7a7141 100644 --- a/project_euler/problem_090/sol1.py +++ b/project_euler/problem_090/sol1.py @@ -1,31 +1,68 @@ +""" +Project Euler Problem 90: https://projecteuler.net/problem=90 +Cube digit pairs: + +Each of the six faces on a cube has a different digit (0 to 9) written on it; +the same is done to a second cube. +In fact, by carefully choosing the digits on both cubes it is possible to display +all of the square numbers below one-hundred: 01, 04, 09, 16, 25, 36, 49, 64, and 81. +For example, one way this can be achieved is by placing {0, 5, 6, 7, 8, 9} on one cube +and {1, 2, 3, 4, 8, 9} on the other cube. + +However, for this problem we shall allow the 6 or 9 to be turned upside-down so that +an arrangement like {0, 5, 6, 7, 8, 9} and {1, 2, 3, 4, 6, 7} allows for all +nine square numbers to be displayed; otherwise it would be impossible to obtain 09. +In determining a distinct arrangement we are interested in the digits on each cube, +not the order. +{1, 2, 3, 4, 5, 6} is equivalent to {3, 6, 4, 1, 2, 5} +{1, 2, 3, 4, 5, 6} is distinct from {1, 2, 3, 4, 5, 9} + +But because we are allowing 6 and 9 to be reversed, the two distinct sets +in the last example both represent the extended set {1, 2, 3, 4, 5, 6, 9} +for the purpose of forming 2-digit numbers. +How many distinct arrangements of the two cubes allow for all of the +square numbers to be displayed? +""" + from itertools import permutations, combinations -def set_to_bit(s): + +def set_to_bit(t: tuple) -> int: """ - returns bit representation of a given iterable, preferably set - @param s - set representing the 1 bits + returns bit representation of a given iterable, preferably tuple. + @param t - tuple representing the 1 bits + >>> set_to_bit((0,1)) + 3 + >>> set_to_bit((1,3)) + 10 """ res = 0 - for i in s: - res |= 1< bool: """ - checks if a given bit is 1 in the given number (work around for 6 and 9) - @param n - the number/set to search in - @param c - the index to look for + checks if a given bit is 1 in the given number (work around for 6 and 9) + @param n - the number/set to search in + @param c - the index to look for + >>> is_bit_set(10, 1) + True + >>> is_bit_set(64, 9) + True """ if c == 6 or c == 9: - return (1<<6 & n) or (1<<9 & n) - return 1< int: """ - returns the solution of problem 90 using helper functions - e.g 1217 for the default argument values + returns the solution of problem 90 using helper functions + e.g 1217 for the default argument values + + @param n - the number of squares to validate. + @param m - the number of dice to use. - @param n - the number of squares to validate. - @param m - the number of dice to use. + >>> solution(3, 1) + 55 + >>> solution(7, 2) + 2365 """ - sq = [str(i**2).zfill(m) for i in range(1, n+1)] + sq = [str(i ** 2).zfill(m) for i in range(1, n + 1)] all_dices = [set_to_bit(c) for c in combinations(range(10), 6)] dices = [p for p in combinations(all_dices, m)] From 3e8d53bca5bfc69866db997133e760f57611af71 Mon Sep 17 00:00:00 2001 From: Usajjad123 Date: Tue, 12 Oct 2021 06:25:10 +0500 Subject: [PATCH 4/5] used descriptive names --- project_euler/problem_090/sol1.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/project_euler/problem_090/sol1.py b/project_euler/problem_090/sol1.py index cac5ea7a7141..0dd52ee6a885 100644 --- a/project_euler/problem_090/sol1.py +++ b/project_euler/problem_090/sol1.py @@ -27,7 +27,7 @@ from itertools import permutations, combinations -def set_to_bit(t: tuple) -> int: +def set_to_bit(bit_tuple: tuple) -> int: """ returns bit representation of a given iterable, preferably tuple. @param t - tuple representing the 1 bits @@ -37,32 +37,34 @@ def set_to_bit(t: tuple) -> int: 10 """ res = 0 - for i in t: + for i in bit_tuple: res |= 1 << i return res -def is_bit_set(n: int, c: int) -> bool: +def is_bit_set(number: int, bit: int) -> bool: """ checks if a given bit is 1 in the given number (work around for 6 and 9) - @param n - the number/set to search in - @param c - the index to look for + @param number - the number/set to search in + @param bit - the index to look for >>> is_bit_set(10, 1) True >>> is_bit_set(64, 9) True """ - if c == 6 or c == 9: - return bool((1 << 6 & n) or (1 << 9 & n)) - return bool(1 << c & n) + if bit == 6 or bit == 9: + return bool((1 << 6 & number) or (1 << 9 & number)) + return bool(1 << bit & number) -def validate_cubes(cubes: tuple, sq: list): +def validate_cubes(cubes: tuple, sq: list) -> bool: """ verifies whether or not the selected combination of cubes is valid, by iterating through all square values. @param cubes - tuple of cubes represented by numbers (having six 1 bits (0-9)) @param sq - list of squares to validate + >>> validate_cubes((63,), ['4']) + True """ for s in sq: res = False @@ -76,22 +78,18 @@ def validate_cubes(cubes: tuple, sq: list): return True -def solution(n: int = 9, m: int = 2) -> int: +def solution(squares: int = 9, number_of_dice: int = 2) -> int: """ returns the solution of problem 90 using helper functions e.g 1217 for the default argument values - - @param n - the number of squares to validate. - @param m - the number of dice to use. - >>> solution(3, 1) 55 >>> solution(7, 2) 2365 """ - sq = [str(i ** 2).zfill(m) for i in range(1, n + 1)] + sq = [str(i ** 2).zfill(number_of_dice) for i in range(1, squares + 1)] all_dices = [set_to_bit(c) for c in combinations(range(10), 6)] - dices = [p for p in combinations(all_dices, m)] + dices = [p for p in combinations(all_dices, number_of_dice)] res = 0 for d in dices: From 5693e5a7f1efdf7fcbbb3be4159d50ca83333823 Mon Sep 17 00:00:00 2001 From: Usajjad123 <36397831+Usajjad123@users.noreply.github.com> Date: Thu, 14 Oct 2021 22:59:22 +0500 Subject: [PATCH 5/5] Update project_euler/problem_090/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_090/sol1.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/project_euler/problem_090/sol1.py b/project_euler/problem_090/sol1.py index 0dd52ee6a885..7198faa7a180 100644 --- a/project_euler/problem_090/sol1.py +++ b/project_euler/problem_090/sol1.py @@ -91,11 +91,7 @@ def solution(squares: int = 9, number_of_dice: int = 2) -> int: all_dices = [set_to_bit(c) for c in combinations(range(10), 6)] dices = [p for p in combinations(all_dices, number_of_dice)] - res = 0 - for d in dices: - if validate_cubes(d, sq): - res += 1 - return res + return len([d for d in dices if validate_cubes(d, sq)]) if __name__ == "__main__":