banner ad

A palindromic number by python

| January 27, 2013 | 0 Comments
0 Flares 0 Flares ×

A palindromic number is a number that remains the same when its digits are reversed. The term palindromic is derived from palindrome, which refers to a word whose spelling is unchanged when its letters are reversed. The palindromic numbers are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101,
All numbers in base 10 with one digit are palindromic. The number of palindromic numbers with two digits is 9:{11, 22, 33, 44, 55, 66, 77, 88, 99}.

A palindromic number by python:

#! /usr/bin/env python
#coding=utf-8

a = input()
b = str(a)
flag = True
for i in range(len(b)/2):
    if b[i]!=b[len(b)-i-1]:
        flag = False
        break
if flag:
    print "%d is huiwen number!" % a
else:
    print "%d is not huiwen number!" % a

itertools — Functions creating iterators for efficient looping.
It implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python. The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.

from itertools import product

def palindromeNum(n):
   return [n*'%s'%tuple(list(i)+list(i[n*(n-1)/2%(n-1)-1::-1])) for i in product(*([range(1,10)]+[range(0,10)]*((n+1)/2-1)))]
#n>1
print map(palindromeNum, [2,3,4])
Download PDF
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Reddit 0 StumbleUpon 0 0 Flares ×

Tags:

Category: Itertools

About the Author ()

My name is John Link.I am 26 years old. My major is Computer science and technology. I am a junior programmer with Python.

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Reddit 0 StumbleUpon 0 0 Flares ×