Dec-08-2018, 06:33 AM
Dec-08-2018, 07:14 AM
What exactly is your question/goal. Read what Palindrome is.
Dec-08-2018, 09:03 AM
I don't know what is 'implement' is that case.
You can easily get the palindromes in the first 1000 numbers:
You can easily get the palindromes in the first 1000 numbers:
for p in range(1000): pa = list(str(p)) pa.reverse() pn = int(''.join(pa)) if pn == p: print(p,pn)
Dec-09-2018, 04:16 PM
Hi again,
I realize my answer was silly.
Of course, palindromes can be implemented as a python class.
For instance, a very basic sample :
$ python3 pldrm.py
11 is a palindrome
And the docstring is available by:
$python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import palindrome
>>> help(palindrome)
>>> help(palindrome.Palindrome)
You can invent more complex functions... probably not very useful, but so fun
I realize my answer was silly.
Of course, palindromes can be implemented as a python class.
For instance, a very basic sample :
'''The palindrome module is a very basic implementation of palindromes. Provided as the palindrome.py file''' class Palindrome(): '''The Palindrome class provides three public members functions: Palindrome(n) : the instanciation member, where n is an integer isPalindrome() : this function returns True is the class was instantiated with a palindrome integer __str__() : provides a printable form of the palindrome ''' def __init__(self,candidate): self.value = candidate def isPalindrome(self): pa = list(str(self.value)) pa.reverse() pn = int(''.join(pa)) if pn == self.value: return True return False def __str__(self): if self.isPalindrome: return '%s is a palindrome' % self.value else: return '%s is not a palindrome' % self.valueThis can be used by the following:
import palindrome for i in (10,11,12): obj = palindrome.Palindrome(i) if obj.isPalindrome(): print(obj)The result is :
$ python3 pldrm.py
11 is a palindrome
And the docstring is available by:
$python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import palindrome
>>> help(palindrome)
>>> help(palindrome.Palindrome)
You can invent more complex functions... probably not very useful, but so fun
Dec-09-2018, 10:05 PM
(Dec-09-2018, 04:16 PM)jeanMichelBain Wrote: [ -> ]Hi again,I don't think he mean that "implemented" most be a class.
I realize my answer was silly.
Of course, palindromes can be implemented as a python class.
The answer to his question yes,then he should try something himself.
Your answer with
reverse()
is okay.When first have started can also bring in
[::-1]
,that's common to use with palindrome.Eg:
def is_palindrome(n): n = str(n) return n == n[::-1]Test:
>>> lst = [10, 11, 12, 99, 24, 44] >>> filter(is_palindrome, lst) [11, 99, 44]Also a newer string formatting than old
%s
.return f'{self.value} is a palindrome'
Dec-11-2018, 07:24 AM
A palindrome is a phrase, a word, or a sequence that reads the same forward and backward. One such example will be pip! An example of such a phrase will be ‘nurses run’.
>>> def isPalindrome(string):
left,right=0,len(string)-1
while right>=left:
if not string[left]==string[right]:
return False
left+=1;right-=1
return True
>>> isPalindrome('redrum murder')
True
>>> isPalindrome('CC.')
False
Well, there are other ways to do this too. Let’s try using an iterator.
>>> def isPalindrome(string):
left,right=iter(string),iter(string[::-1])
i=0
while i<len(string)/2:
if next(left)!=next(right):
return False
i+=1
return True
>>> isPalindrome('redrum murder')
True
>>> isPalindrome('CC.')
False
>>> isPalindrome('CCC.')
False
>>> def isPalindrome(string):
left,right=0,len(string)-1
while right>=left:
if not string[left]==string[right]:
return False
left+=1;right-=1
return True
>>> isPalindrome('redrum murder')
True
>>> isPalindrome('CC.')
False
Well, there are other ways to do this too. Let’s try using an iterator.
>>> def isPalindrome(string):
left,right=iter(string),iter(string[::-1])
i=0
while i<len(string)/2:
if next(left)!=next(right):
return False
i+=1
return True
>>> isPalindrome('redrum murder')
True
>>> isPalindrome('CC.')
False
>>> isPalindrome('CCC.')
False