## Monday, 7 July 2014

### Palindrome Number

Palindrome number are very common question we face in programming field .

### 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 Number
If 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 :

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 .

#### PYThon code to find palindrome number in a given range and store it to a text File :

1. # Python 2.7
2. # store Palindrome Number in given range to a text file
3. def palindrome_in_range(a,b):
4.     '''find palindrome number in range a,b and return a list of these number'''
5.     ls =[] #where all palindrome are going to store
6.     for i in range(a,b):
7.         temp = str(i)   # convert to string
8.         l = len(temp) - 1  # index of last char of string
9.         i=0 #index of first chr of string
10.
11.         flag = True  # flag == false if number failes test of palindrome
12.         while(i<l) :
13.             ''' check for number is palindrome'''
14.             if temp[i]==temp[l] : #then check next chr
15.                 i +=1
16.                 l -=1
17.             else:
18.                 flag = False
19.                 break
20.
21.         if flag: #if flag == True then number is palindrome so add it to list
22.             ls.append(temp)
23.
24.     return ls
25. def data_store(ls,a,b):
26.     '''list as input & write its content to a text file palindrome.txt '''
27.     count = 0 #use to format text in file ,ie only fixed number of number in a line
28.     f=open('palindrome.txt','w') #open text file
29.     f.write('\n\t\t\tPALINDROME number in range '+a+' to '+b)
30.     f.write('\n\t\t\t\t http://beginer2cs.blogspot.com\n\n\t\t\t')
31.     for i in ls:  #write content of ls in text file
32.         if count==10:
33.              f.write('\n\t\t\t'+str(i))
34.              count =0
35.         else:
36.             count +=1
37.             f.write(str(i)+' ,')
38.
39.     f.close() #close file
40. if __name__=='__main__':
41.     a,b=raw_input('Enter Value of a and b separated by space :').split(' ')
42.     ls = palindrome_in_range(int(a),int(b))
43.     data_store(ls,a,b) #call func that store data in txt file
44.     print """
45.    ALL palindrome number in given range is successfully fetch
46.    and store to Palindrome.txt file .\n
47.    Thanks
48.    """

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) :

1. def count_palindrome(a,b):
2.     count = 0
3.     ls=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99,
4.    101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222,
5.    232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353,
6.    363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484,
7.    494, 505, 515, 525, 535, 545, 555, 565, 575, 585, 595, 606, 616,
8.    626, 636, 646, 656, 666, 676, 686, 696, 707, 717, 727, 737, 747,
9.    757, 767, 777, 787, 797, 808, 818, 828, 838, 848, 858, 868, 878,
10.    888, 898, 909, 919, 929, 939, 949, 959, 969, 979, 989, 999, 1001,
11.    1111, 1221, 1331, 1441, 1551, 1661, 1771, 1881, 1991, 2002, 2112,
12.    2222, 2332, 2442, 2552, 2662, 2772, 2882, 2992, 3003, 3113, 3223,
13.    3333, 3443, 3553, 3663, 3773, 3883, 3993, 4004, 4114, 4224, 4334,
14.    4444, 4554, 4664, 4774, 4884, 4994, 5005, 5115, 5225, 5335, 5445,
15.    5555, 5665, 5775, 5885, 5995, 6006, 6116, 6226, 6336, 6446, 6556,
16.    6666, 6776, 6886, 6996, 7007, 7117, 7227, 7337, 7447, 7557, 7667,
17.    7777, 7887, 7997, 8008, 8118, 8228, 8338, 8448, 8558, 8668, 8778,
18.    8888, 8998, 9009, 9119, 9229, 9339, 9449, 9559, 9669, 9779, 9889,
19.    9999]
20.     for i in ls:
21.         if i>=and i<=b:
22.             count +=1
23.     return count
24. if __name__=='__main__':
25.     tc = int(raw_input())
26.     for i in range(tc):
27.         a,b=raw_input().split(' ')
28.         print count_palindrome(int(a),int(b))

Working Range of This program can be extended by extending palindrome numbers in list

Thanks :)

#### 1 comment:

1. I have read some excellent stuff here. Certainly price
bookmarking 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,

THANKS FOR UR GREAT COMMENT

Blogger Widgets