Palindrome number are very common question we face in programming field .
Example : a=2353 and reverse(a) = 3532
Since a not equal to reverse(a) so not Palindrome
Compare first digit by last then second by second last until any mismatch found or we reach at mid of number . If mismatch found then it is not a palindrome number else it is a palindrome number .Palindrome number :
Numbers that remain the same when digit is reversed .
Example : 2, 202 , 101 ,191, 99599, 76567 these are palindrome number
Not palindrome : 123, 3452, 6789, 23434 4561, 34546
How check Number Is Palindrome Or Not :
There are many ways to check number is palindrome :
Reverse NumberIf Reversed Num == Number then it is palindrome
Example : a=2353 and reverse(a) = 3532
Since a not equal to reverse(a) so not Palindrome
By iteration :
PYThon code to find palindrome number in a given range and store it to a text File :
- # Python 2.7
- # store Palindrome Number in given range to a text file
- def palindrome_in_range(a,b):
- '''find palindrome number in range a,b and return a list of these number'''
- ls =[] #where all palindrome are going to store
- for i in range(a,b):
- temp = str(i) # convert to string
- l = len(temp) - 1 # index of last char of string
- i=0 #index of first chr of string
- flag = True # flag == false if number failes test of palindrome
- while(i<l) :
- ''' check for number is palindrome'''
- if temp[i]==temp[l] : #then check next chr
- i +=1
- l -=1
- else:
- flag = False
- break
- if flag: #if flag == True then number is palindrome so add it to list
- ls.append(temp)
- return ls
- def data_store(ls,a,b):
- '''list as input & write its content to a text file palindrome.txt '''
- count = 0 #use to format text in file ,ie only fixed number of number in a line
- f=open('palindrome.txt','w') #open text file
- f.write('\n\t\t\tPALINDROME number in range '+a+' to '+b)
- f.write('\n\t\t\t\t http://beginer2cs.blogspot.com\n\n\t\t\t')
- for i in ls: #write content of ls in text file
- if count==10:
- f.write('\n\t\t\t'+str(i))
- count =0
- else:
- count +=1
- f.write(str(i)+' ,')
- f.close() #close file
- if __name__=='__main__':
- a,b=raw_input('Enter Value of a and b separated by space :').split(' ')
- ls = palindrome_in_range(int(a),int(b))
- data_store(ls,a,b) #call func that store data in txt file
- print """
- ALL palindrome number in given range is successfully fetch
- and store to Palindrome.txt file .\n
- Thanks
- """
Output of this program is stored in palindrome.txt file .which is automatically created
Now , If we got a question to find out number of palindrome in given range (max range should be between 0 to 10000)
Yo have two ways to solve this ,
Way 1 :Iterate through all numbers in range and check is palindrome , if palindrome increment count by 1
Way 2:Use your previous learning if you have list of all palindrome number in range 0 to 100000
create a list contain all numbers , and count numbers in list which is in given range , That is our answer for above question it is very fast as compare to way 1 .
Python Program for Number of Palindrome In Given range (max range should be between 0 to 10000) :
- def count_palindrome(a,b):
- count = 0
- ls=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99,
- 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222,
- 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353,
- 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484,
- 494, 505, 515, 525, 535, 545, 555, 565, 575, 585, 595, 606, 616,
- 626, 636, 646, 656, 666, 676, 686, 696, 707, 717, 727, 737, 747,
- 757, 767, 777, 787, 797, 808, 818, 828, 838, 848, 858, 868, 878,
- 888, 898, 909, 919, 929, 939, 949, 959, 969, 979, 989, 999, 1001,
- 1111, 1221, 1331, 1441, 1551, 1661, 1771, 1881, 1991, 2002, 2112,
- 2222, 2332, 2442, 2552, 2662, 2772, 2882, 2992, 3003, 3113, 3223,
- 3333, 3443, 3553, 3663, 3773, 3883, 3993, 4004, 4114, 4224, 4334,
- 4444, 4554, 4664, 4774, 4884, 4994, 5005, 5115, 5225, 5335, 5445,
- 5555, 5665, 5775, 5885, 5995, 6006, 6116, 6226, 6336, 6446, 6556,
- 6666, 6776, 6886, 6996, 7007, 7117, 7227, 7337, 7447, 7557, 7667,
- 7777, 7887, 7997, 8008, 8118, 8228, 8338, 8448, 8558, 8668, 8778,
- 8888, 8998, 9009, 9119, 9229, 9339, 9449, 9559, 9669, 9779, 9889,
- 9999]
- for i in ls:
- if i>=a and i<=b:
- count +=1
- return count
- if __name__=='__main__':
- tc = int(raw_input())
- for i in range(tc):
- a,b=raw_input().split(' ')
- print count_palindrome(int(a),int(b))
Working Range of This program can be extended by extending palindrome numbers in list
Thanks :)
I have read some excellent stuff here. Certainly price
ReplyDeletebookmarking for revisiting. I surprise how much effort you set to make such
a wonderful informative site.
Also visit my homepage :: search engine optimization, seofornown4eva.com,