diff options
Diffstat (limited to 'count_symmetric_integers.py')
-rw-r--r-- | count_symmetric_integers.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/count_symmetric_integers.py b/count_symmetric_integers.py new file mode 100644 index 0000000..b7ccbd2 --- /dev/null +++ b/count_symmetric_integers.py @@ -0,0 +1,34 @@ +# 2843. Count Symmetric Integers + +""" +you are given two positive integers 'low' and 'high'. an integer 'x' +consisting of '2 * n' digits is symmetric if the sum of the first 'n' digits +of 'x' is equal to the sum of the last 'n' digits of 'x'. numbers with an odd +number of digits are never symmetric. return the number of symmetric integers +in the range '[low, high]'. +""" + + +class Solution(object): + def countSymmetricIntegers(self, low, high): + """ + :type low: int + :type high: int + :rtype: int + """ + cnt = 0 + for i in range(low, high + 1): + if i < 100 and i % 11 == 0: + cnt += 1 + elif 1000 <= i < 10000: + left = i // 1000 + (i % 1000) // 100 + right = (i % 100) // 10 + (i % 10) + if left == right: + cnt += 1 + return cnt + + +if __name__ == "__main__": + obj = Solution() + print(obj.countSymmetricIntegers(low=1, high=100)) + print(obj.countSymmetricIntegers(low=1200, high=1230)) |