mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Improve Project Euler problem 070 solution 1 (#5166)
* Change has_same_digits doctest * Improve has_same_digits function
This commit is contained in:
parent
e6cf13cc03
commit
e7565f8bfc
@ -60,34 +60,16 @@ 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
|
||||
|
||||
>>> has_same_digits(123, 12)
|
||||
>>> has_same_digits(123, 23)
|
||||
False
|
||||
|
||||
>>> 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:
|
||||
|
Loading…
Reference in New Issue
Block a user