From 588a9b3efd679b999bb5f5a9f82df61fdf0fe075 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 9 Oct 2021 02:56:51 +0300 Subject: [PATCH 1/2] Change has_same_digits doctest --- project_euler/problem_070/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_070/sol1.py b/project_euler/problem_070/sol1.py index e106800d5716..e2526470ed79 100644 --- a/project_euler/problem_070/sol1.py +++ b/project_euler/problem_070/sol1.py @@ -69,7 +69,7 @@ def has_same_digits(num1: int, num2: int) -> bool: >>> has_same_digits(123456789, 987654321) True - >>> has_same_digits(123, 12) + >>> has_same_digits(123, 23) False >>> has_same_digits(1234566, 123456) From 9f2087ba9608b3de84701b62f235d4814840102f Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 9 Oct 2021 02:58:34 +0300 Subject: [PATCH 2/2] Improve has_same_digits function --- project_euler/problem_070/sol1.py | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/project_euler/problem_070/sol1.py b/project_euler/problem_070/sol1.py index e2526470ed79..d42b017cc476 100644 --- a/project_euler/problem_070/sol1.py +++ b/project_euler/problem_070/sol1.py @@ -60,12 +60,6 @@ def has_same_digits(num1: int, num2: int) -> bool: Return True if num1 and num2 have the same frequency of every digit, False otherwise. - digits[] is a frequency table where the index represents the digit from - 0-9, and the element stores the number of appearances. Increment the - respective index every time you see the digit in num1, and decrement if in - num2. At the end, if the numbers have the same digits, every index must - contain 0. - >>> has_same_digits(123456789, 987654321) True @@ -75,19 +69,7 @@ def has_same_digits(num1: int, num2: int) -> bool: >>> has_same_digits(1234566, 123456) False """ - digits = [0] * 10 - - while num1 > 0 and num2 > 0: - digits[num1 % 10] += 1 - digits[num2 % 10] -= 1 - num1 //= 10 - num2 //= 10 - - for digit in digits: - if digit != 0: - return False - - return True + return sorted(str(num1)) == sorted(str(num2)) def solution(max: int = 10000000) -> int: