-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_digit.py
More file actions
30 lines (22 loc) · 778 Bytes
/
count_digit.py
File metadata and controls
30 lines (22 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from collections import Counter
class Solution:
def count_digits(self, nums):
for i, num in enumerate(nums):
nums[i] = self._calculate(num)
count_freq = Counter(nums)
max_freq = max(count_freq.values())
candidates = [num for num, freq in count_freq.items() if freq == max_freq]
return max(candidates)
def _calculate(self, num):
num_str = str(num)
while len(num_str) > 1:
total = 0
for digit in num_str:
total += int(digit)
num_str = str(total)
return int(num_str)
if __name__ == "__main__":
solution = Solution()
result = solution.count_digits(["123", "456", "29", "92", "1"])
print(result)
print("Test Case Passed!")