Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views1 page

Count Symmetric Integers

The provided code defines a class Solution with a method countSymmetricIntegers that counts the number of symmetric integers within a given range [low, high]. A symmetric integer is defined as an integer whose first half of digits sums to the same value as the second half. The method uses a helper function isequal to check the symmetry of each integer in the range.

Uploaded by

abbud5859
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Count Symmetric Integers

The provided code defines a class Solution with a method countSymmetricIntegers that counts the number of symmetric integers within a given range [low, high]. A symmetric integer is defined as an integer whose first half of digits sums to the same value as the second half. The method uses a helper function isequal to check the symmetry of each integer in the range.

Uploaded by

abbud5859
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

class Solution(object):

def countSymmetricIntegers(self, low, high):


"""
:type low: int
:type high: int
:rtype: int
"""
def isequal(num):
ns=str(num)
if len(ns)%2==1:
return False
n=(len(ns)//2)
fss=[int(num) for num in ns[:n]]
lss=[int(num) for num in ns[n:]]
return sum(fss)==sum(lss)
c=0
for i in range(low,high+1):
if isequal(i):
c+=1
return c

You might also like