From edc37bd3be9329beb1761c58f8c7c8041c86007b Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 7 Oct 2020 22:35:41 -0700 Subject: [PATCH 01/11] Add solution for Project Euler 62 --- DIRECTORY.md | 2 ++ project_euler/problem_62/__init__.py | 0 project_euler/problem_62/sol1.py | 50 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 project_euler/problem_62/__init__.py create mode 100644 project_euler/problem_62/sol1.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 6a3d31709ed6..691bf4a6fe61 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -650,6 +650,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py) * Problem 56 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_56/sol1.py) + * Problem 62 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_62/sol1.py) * Problem 63 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_63/sol1.py) * Problem 67 diff --git a/project_euler/problem_62/__init__.py b/project_euler/problem_62/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py new file mode 100644 index 000000000000..330712b280a5 --- /dev/null +++ b/project_euler/problem_62/sol1.py @@ -0,0 +1,50 @@ +""" +Project Euler 62 +https://projecteuler.net/problem=62 + +The cube, 41063625 (345^3), can be permuted to produce two other cubes: +56623104 (384^3) and 66430125 (405^3). In fact, 41063625 is the smallest cube +which has exactly three permutations of its digits which are also cube. + +Find the smallest cube for which exactly five permutations of its digits are +cube. +""" + +from collections import defaultdict + +def solution(): + """ + Iterate through every possible cube and sort the cube's digits in + ascending order. Sorting maintains an ordering of the digits that allows + you to compare permutations. Store each sorted sequence of digits in a + dictionary, whose key is the sequence of digits and value is a list of + numbers that are the base of the cube. + + Once you find 5 numbers that produce the same sequence of digits, return + the smallest one, which is at index 0 since we insert each base in + ascending order. + """ + freqs = defaultdict(list) + num = 0 + + while (True): + digits = get_digits(num) + freqs[digits].append(num) + + if (len(freqs[digits]) == 5): + base = freqs[digits][0] ** 3 + return base + + num += 1 + +def get_digits(num): + """ + Computes the sorted sequence of digits of the cube of num. + """ + cube = num ** 3 + digits = [str(x) for x in str(cube)] + digits.sort() + return "".join(digits) + +if __name__ == "__main__": + print(solution()) From 3da81bc4746e9787aa261e672f61423ad1a105cf Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 7 Oct 2020 22:43:36 -0700 Subject: [PATCH 02/11] Add doctests and annotate function params and return values for get_digits() --- project_euler/problem_62/sol1.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py index 330712b280a5..4c17fec7f969 100644 --- a/project_euler/problem_62/sol1.py +++ b/project_euler/problem_62/sol1.py @@ -37,9 +37,16 @@ def solution(): num += 1 -def get_digits(num): +def get_digits(num: int) -> str: """ Computes the sorted sequence of digits of the cube of num. + + >>> get_digits(3) + '27' + >>> get_digits(99) + '027999' + >>> get_digits(123) + '0166788' """ cube = num ** 3 digits = [str(x) for x in str(cube)] From 4d133e4d0272d065ec44e763ee2afb7478fc762d Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 7 Oct 2020 22:54:26 -0700 Subject: [PATCH 03/11] Add extra newline between functions to fix flake8 errors --- project_euler/problem_62/sol1.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py index 4c17fec7f969..1964f0797f0a 100644 --- a/project_euler/problem_62/sol1.py +++ b/project_euler/problem_62/sol1.py @@ -13,6 +13,7 @@ from collections import defaultdict def solution(): + """ Iterate through every possible cube and sort the cube's digits in ascending order. Sorting maintains an ordering of the digits that allows @@ -37,6 +38,7 @@ def solution(): num += 1 + def get_digits(num: int) -> str: """ Computes the sorted sequence of digits of the cube of num. @@ -52,6 +54,7 @@ def get_digits(num: int) -> str: digits = [str(x) for x in str(cube)] digits.sort() return "".join(digits) + if __name__ == "__main__": print(solution()) From ee984f82f87c8f1c9d28f46e83d79e45865046cf Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 7 Oct 2020 22:57:38 -0700 Subject: [PATCH 04/11] Add extra newlines between function names --- project_euler/problem_62/sol1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py index 1964f0797f0a..80c739803176 100644 --- a/project_euler/problem_62/sol1.py +++ b/project_euler/problem_62/sol1.py @@ -12,6 +12,7 @@ from collections import defaultdict + def solution(): """ @@ -54,7 +55,7 @@ def get_digits(num: int) -> str: digits = [str(x) for x in str(cube)] digits.sort() return "".join(digits) - + if __name__ == "__main__": print(solution()) From a4e322c14d13752393a119a6d2ca2650a16103ae Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 7 Oct 2020 23:00:17 -0700 Subject: [PATCH 05/11] Add missing return type for solution() --- project_euler/problem_62/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py index 80c739803176..6c4575ea8772 100644 --- a/project_euler/problem_62/sol1.py +++ b/project_euler/problem_62/sol1.py @@ -13,7 +13,7 @@ from collections import defaultdict -def solution(): +def solution() -> int: """ Iterate through every possible cube and sort the cube's digits in From 022fb765a00cbee78a6dd5a03c50c479a7e00098 Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 7 Oct 2020 23:02:30 -0700 Subject: [PATCH 06/11] Remove parenthesis from if statement --- project_euler/problem_62/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py index 6c4575ea8772..8721310481ca 100644 --- a/project_euler/problem_62/sol1.py +++ b/project_euler/problem_62/sol1.py @@ -33,7 +33,7 @@ def solution() -> int: digits = get_digits(num) freqs[digits].append(num) - if (len(freqs[digits]) == 5): + if len(freqs[digits]) == 5: base = freqs[digits][0] ** 3 return base From 0f088c6617cb491fcf19583ea567356058e9a883 Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 7 Oct 2020 23:26:37 -0700 Subject: [PATCH 07/11] Remove parentheses from while loop --- project_euler/problem_62/sol1.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py index 8721310481ca..7bc56bd601d3 100644 --- a/project_euler/problem_62/sol1.py +++ b/project_euler/problem_62/sol1.py @@ -14,7 +14,6 @@ def solution() -> int: - """ Iterate through every possible cube and sort the cube's digits in ascending order. Sorting maintains an ordering of the digits that allows @@ -29,7 +28,7 @@ def solution() -> int: freqs = defaultdict(list) num = 0 - while (True): + while True: digits = get_digits(num) freqs[digits].append(num) From 04a44c5321853c40b4dccfe76a4b4ed90ce2a821 Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Thu, 8 Oct 2020 00:19:23 -0700 Subject: [PATCH 08/11] Add to explanation and fix second Travis build --- project_euler/problem_62/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py index 7bc56bd601d3..e27201de88cc 100644 --- a/project_euler/problem_62/sol1.py +++ b/project_euler/problem_62/sol1.py @@ -22,7 +22,7 @@ def solution() -> int: numbers that are the base of the cube. Once you find 5 numbers that produce the same sequence of digits, return - the smallest one, which is at index 0 since we insert each base in + the smallest one, which is at index 0 since we insert each base number in ascending order. """ freqs = defaultdict(list) From 62aa88cedad9d8cd0948f3a2bc9fa651d263ea7f Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 14 Oct 2020 20:18:54 -0700 Subject: [PATCH 09/11] Compress get_digits(), add tests for solution(), add fstring and positional arg for solution() --- project_euler/problem_62/sol1.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py index e27201de88cc..e5974b3c91e7 100644 --- a/project_euler/problem_62/sol1.py +++ b/project_euler/problem_62/sol1.py @@ -13,7 +13,7 @@ from collections import defaultdict -def solution() -> int: +def solution(max_base: int = 5) -> int: """ Iterate through every possible cube and sort the cube's digits in ascending order. Sorting maintains an ordering of the digits that allows @@ -24,6 +24,13 @@ def solution() -> int: Once you find 5 numbers that produce the same sequence of digits, return the smallest one, which is at index 0 since we insert each base number in ascending order. + + >>> solution(2) + 125 + >>> solution(3) + 41063625 + >>> solution(5) + 127035954683 """ freqs = defaultdict(list) num = 0 @@ -32,7 +39,7 @@ def solution() -> int: digits = get_digits(num) freqs[digits].append(num) - if len(freqs[digits]) == 5: + if len(freqs[digits]) == max_base: base = freqs[digits][0] ** 3 return base @@ -50,11 +57,8 @@ def get_digits(num: int) -> str: >>> get_digits(123) '0166788' """ - cube = num ** 3 - digits = [str(x) for x in str(cube)] - digits.sort() - return "".join(digits) + return "".join(sorted(list(str(num ** 3)))) if __name__ == "__main__": - print(solution()) + print(f"{solution(5) = }") From c2857095171dbf665e7cab49c88f6974821ccb60 Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 14 Oct 2020 20:21:26 -0700 Subject: [PATCH 10/11] Remove input param when calling solution() --- project_euler/problem_62/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py index e5974b3c91e7..2611db105be3 100644 --- a/project_euler/problem_62/sol1.py +++ b/project_euler/problem_62/sol1.py @@ -61,4 +61,4 @@ def get_digits(num: int) -> str: if __name__ == "__main__": - print(f"{solution(5) = }") + print(f"{solution() = }") From 9616ef63cdc8bbd997c760f383de8f37d1f38e92 Mon Sep 17 00:00:00 2001 From: "peteryao7@gmail.com" Date: Wed, 14 Oct 2020 20:33:11 -0700 Subject: [PATCH 11/11] Remove test case for the answer --- project_euler/problem_62/sol1.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/project_euler/problem_62/sol1.py b/project_euler/problem_62/sol1.py index 2611db105be3..83286c801301 100644 --- a/project_euler/problem_62/sol1.py +++ b/project_euler/problem_62/sol1.py @@ -29,8 +29,6 @@ def solution(max_base: int = 5) -> int: 125 >>> solution(3) 41063625 - >>> solution(5) - 127035954683 """ freqs = defaultdict(list) num = 0