python/CodeChallenge_12.py

41 lines
1.0 KiB
Python

'''
Given an integer n, determine how many integers x (where 1 ≤ x ≤ n) are interesting. An integer x is considered interesting if the sum of its digits decreases when incremented by 1, i.e., S(x+1) < S(x), where S(x) represents the sum of the digits of x in the decimal system.
Your task is to count how many such interesting numbers exist within the given range.
Example 1
Input:
9
Output:
1
Example 2
Input:
19
Output:
2
Example 3
Input:
1
Output:
0
'''
class Solution(object):
def solve(self, n:int) -> int:
print(f"n: {n}")
interesting_count = 0
for x in range(1, n + 1):
if self.is_interesting(x):
interesting_count += 1
return interesting_count
def is_interesting(self, x:int) -> bool:
return self.digit_sum(x + 1) < self.digit_sum(x)
def digit_sum(self, x:int) -> int:
return sum(int(digit) for digit in str(x))
s = Solution()
print(s.solve(9)) # Output: 1
print(s.solve(19)) # Output: 2
print(s.solve(1)) # Output: 0
print(s.solve(160)) # Output: 9