Skip to content

Commit e7565f8

Browse files
authoredOct 30, 2021
Improve Project Euler problem 070 solution 1 (TheAlgorithms#5166)
* Change has_same_digits doctest * Improve has_same_digits function
1 parent e6cf13c commit e7565f8

File tree

1 file changed

+2
-20
lines changed

1 file changed

+2
-20
lines changed
 

‎project_euler/problem_070/sol1.py

+2-20
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,16 @@ def has_same_digits(num1: int, num2: int) -> bool:
6060
Return True if num1 and num2 have the same frequency of every digit, False
6161
otherwise.
6262
63-
digits[] is a frequency table where the index represents the digit from
64-
0-9, and the element stores the number of appearances. Increment the
65-
respective index every time you see the digit in num1, and decrement if in
66-
num2. At the end, if the numbers have the same digits, every index must
67-
contain 0.
68-
6963
>>> has_same_digits(123456789, 987654321)
7064
True
7165
72-
>>> has_same_digits(123, 12)
66+
>>> has_same_digits(123, 23)
7367
False
7468
7569
>>> has_same_digits(1234566, 123456)
7670
False
7771
"""
78-
digits = [0] * 10
79-
80-
while num1 > 0 and num2 > 0:
81-
digits[num1 % 10] += 1
82-
digits[num2 % 10] -= 1
83-
num1 //= 10
84-
num2 //= 10
85-
86-
for digit in digits:
87-
if digit != 0:
88-
return False
89-
90-
return True
72+
return sorted(str(num1)) == sorted(str(num2))
9173

9274

9375
def solution(max: int = 10000000) -> int:

0 commit comments

Comments
 (0)
Please sign in to comment.