Re: palindrome

2015-11-17 Thread Peter Otten
Abhiram R wrote: > ​Haha. Nice. Although with your length of string and the range you're > picking from,the chances of you getting a palindrome are (1/24!) :D ​ Are you sure? >>> candidates = list(itertools.product(string.ascii_lowercase, repeat=4)) >>> le

Re: palindrome

2015-11-17 Thread Peter Otten
ring is a palindrome. With that the loop will become tries = 0 while True: tries += 1 candidate = random_string(length=4) print(candidate) if is_palindrome(candidate): break print(tries, "tries") 2. If you plan to reuse these functions put the above code in a funct

Re: palindrome

2015-11-16 Thread Abhiram R
for i in range(letcount): > >> a=random.choice(abc) > >> str1+=a > >> print str1 > >> count+=1 > >> if str1==str1[::-1]: > >> break > >> else: > >> str1="" > >&

Re: palindrome

2015-11-16 Thread Seymore4Head
+=1 >> if str1==str1[::-1]: >> break >> else: >> str1="" >> print "Tries= ",count >> print str1 >> -- >> >> ? > >The question asks to get an input from the user and print if it's a >palindrome or not

Re: palindrome

2015-11-16 Thread Abhiram R
opqrstuvwxyz' > while True: > for i in range(letcount): > a=random.choice(abc) > str1+=a > print str1 > count+=1 > if str1==str1[::-1]: > break > else: > str1="" > print "Tries= ",count > p

palindrome

2015-11-16 Thread Seymore4Head
http://www.practicepython.org/exercise/2014/03/12/06-string-lists.html Here is my answers. What would make it better? import random str1="" letcount=4 count=0 abc='abcdefghijklmnopqrstuvwxyz' while True: for i in range(letcount): a=random.choice(abc) str1+=a print str1

Re: Where is 'palindrome' defined?

2015-06-02 Thread Marko Rauhamaa
John Ladasky : > On Monday, June 1, 2015 at 2:22:02 AM UTC-7, Marko Rauhamaa wrote: > >> Sota-apina nakataan raastimeen. >> Apelle pane emit. >> Saarnaa takanani paatos. >> >> (= "The war monkey will be chucked into a grater. >> Hand the pistils to father-in-law. >

Re: Where is 'palindrome' defined?

2015-06-02 Thread John Ladasky
On Monday, June 1, 2015 at 2:22:02 AM UTC-7, Marko Rauhamaa wrote: > Sota-apina nakataan raastimeen. > Apelle pane emit. > Saarnaa takanani paatos. > > (= "The war monkey will be chucked into a grater. > Hand the pistils to father-in-law. > Pathos does prea

Re: Where is 'palindrome' defined?

2015-06-01 Thread MRAB
On 2015-06-02 01:55, fl wrote: On Sunday, May 31, 2015 at 9:46:56 PM UTC-7, fl wrote: Hi, When I search solution of reverse a string/number, I came across a short function online: >>> def palindrome(num): return str(num) == str(num)[::-1] I thought that it is a general func

Re: Where is 'palindrome' defined?

2015-06-01 Thread sohcahtoa82
On Monday, June 1, 2015 at 5:55:14 PM UTC-7, fl wrote: > On Sunday, May 31, 2015 at 9:46:56 PM UTC-7, fl wrote: > > Hi, > > > > When I search solution of reverse a string/number, I came across a short > > function online: > > > > >>> def palindrom

Re: Where is 'palindrome' defined?

2015-06-01 Thread fl
On Sunday, May 31, 2015 at 9:46:56 PM UTC-7, fl wrote: > Hi, > > When I search solution of reverse a string/number, I came across a short > function online: > > >>> def palindrome(num): > return str(num) == str(num)[::-1] > > I thought that it is a gene

Re: Where is 'palindrome' defined?

2015-06-01 Thread Denis McMahon
On Sun, 31 May 2015 21:46:31 -0700, fl wrote: >>>> def palindrome(num): Note carefully the spelling(1): palindrome >>>> parlindrome(a) Note carefully the spelling(2): parlindrome > NameError: name 'parlindrome' is not defined Compare carefully spelli

Re: Where is 'palindrome' defined?

2015-06-01 Thread Grant Edwards
On 2015-06-01, Gary Herron wrote: > On 05/31/2015 09:46 PM, fl wrote: >> Hi, >> >> When I search solution of reverse a string/number, I came across a short >> function online: >> >>>>> def palindrome(num): >> return str(num) == str(nu

Re: Where is 'palindrome' defined?

2015-06-01 Thread David Palao
2015-06-01 11:21 GMT+02:00 Marko Rauhamaa : > David Palao : > >> Because "palindrome" != "parlindrome"? >> Have you read the error message? Did you try to understand it? > > When you are starting with any new thing, even the simplest problems > look b

Re: Where is 'palindrome' defined?

2015-06-01 Thread Marko Rauhamaa
David Palao : > Because "palindrome" != "parlindrome"? > Have you read the error message? Did you try to understand it? When you are starting with any new thing, even the simplest problems look baffling. Once you have achieved a few successes, such error messages start

Re: Where is 'palindrome' defined?

2015-06-01 Thread Omar Abou Mrad
On Mon, Jun 1, 2015 at 7:46 AM, fl wrote: > Hi, > > When I search solution of reverse a string/number, I came across a short > function online: > > >>> def palindrome(num): > return str(num) == str(num)[::-1] > > I thought that it is a general funct

Re: Where is 'palindrome' defined?

2015-06-01 Thread Larry Hudson via Python-list
On 05/31/2015 09:46 PM, fl wrote: Hi, When I search solution of reverse a string/number, I came across a short function online: def palindrome(num): return str(num) == str(num)[::-1] I thought that it is a general function. And with the following variable: a '1234

Re: Where is 'palindrome' defined?

2015-06-01 Thread David Palao
Hi, Because "palindrome" != "parlindrome"? Have you read the error message? Did you try to understand it? Best 2015-06-01 6:46 GMT+02:00 fl : > Hi, > > When I search solution of reverse a string/number, I came across a short > function online: > >>>>

Re: Where is 'palindrome' defined?

2015-05-31 Thread Gary Herron
On 05/31/2015 09:46 PM, fl wrote: Hi, When I search solution of reverse a string/number, I came across a short function online: def palindrome(num): return str(num) == str(num)[::-1] I thought that it is a general function. And with the following variable: No, this function is

Where is 'palindrome' defined?

2015-05-31 Thread fl
Hi, When I search solution of reverse a string/number, I came across a short function online: >>> def palindrome(num): return str(num) == str(num)[::-1] I thought that it is a general function. And with the following variable: >>> a '1234_' >>

Re: palindrome iteration

2010-09-14 Thread Bearophile
for your function to not just ignore some inputs, but give some kind of error for the inputs you don't want to consider. Let's say you accept simple Unicode strings, you don't want to ignore white space, an empty string is palindrome, text cases need to be ignored. I don't like

Re: palindrome iteration

2010-09-13 Thread Cousin Stanley
> To deal with "real" palindromes such as, "Madam, I'm Adam," > you should probably strip all spaces and punctuation: > > # untested > pat = re.compile(r'[a-z]') > def is_palindrome(s): > letters = pat.findall(s.lower()) > return letters == reversed(letters) Using python 2.5 the above

Re: palindrome iteration

2010-09-12 Thread Chris Colbert
;) In [29]: s = 'bannab' In [30]: a = np.frombuffer(s.lower(), dtype='uint8') In [31]: np.all(a == a[::-1]) Out[31]: True In [32]: s = 'bannac' In [33]: a = np.frombuffer(s.lower(), dtype='uint8') In [34]: np.all(a == a[::-1]) Out[34]: False -- http://mail.python.org/mailman/listinfo/pytho

Re: palindrome iteration

2010-09-11 Thread Aahz
In article , Dave Angel wrote: > >def is_palindrom(s): >s = s.lower() >return s == s[::-1] To deal with "real" palindromes such as, "Madam, I'm Adam," you should probably strip all spaces and punctuation: # untested pat = re.compile(r'[a-z]') def is_palindrome(s): letters = pat.find

Re: palindrome iteration

2010-08-29 Thread Roy Smith
In article , MRAB wrote: > On 29/08/2010 21:34, Roy Smith wrote: > > In article<8dunm7fv5...@mid.individual.net>, > > Gregory Ewing wrote: > > > >> Steven D'Aprano wrote: > >>> I'm not entirely sure what the use-case for swapcase is. > >> > >> Obviously it's for correcting things that were

Re: palindrome iteration

2010-08-29 Thread MRAB
On 29/08/2010 21:34, Roy Smith wrote: In article<8dunm7fv5...@mid.individual.net>, Gregory Ewing wrote: Steven D'Aprano wrote: I'm not entirely sure what the use-case for swapcase is. Obviously it's for correcting things that were typed in with tHE cAPS lOCK kEY oN bY mISTAKE. :-) So

Re: palindrome iteration

2010-08-29 Thread Josh English
I have no idea. That's a lower level of programming than I'm used to dealing with. Josh (I also only tried the one value. Had I tried with other strings that would fail the test, some functions may have performed better.) On Aug 29, 2:19 am, Matteo Landi wrote: > Well, I tried the also the solu

Re: palindrome iteration

2010-08-29 Thread Roy Smith
In article <8dunm7fv5...@mid.individual.net>, Gregory Ewing wrote: > Steven D'Aprano wrote: > > I'm not entirely sure what the use-case for swapcase is. > > Obviously it's for correcting things that were typed > in with tHE cAPS lOCK kEY oN bY mISTAKE. :-) So it would seem (http://bugs.python

Re: palindrome iteration

2010-08-29 Thread bruno.desthuilli...@gmail.com
On 29 août, 06:39, Gregory Ewing wrote: > Steven D'Aprano wrote: > >  I'm not entirely sure what the use-case for swapcase is. > > Obviously it's for correcting things that were typed > in with tHE cAPS lOCK kEY oN bY mISTAKE. :-) > +1 QOTW !-) -- http://mail.python.org/mailman/listinfo/python-l

Re: palindrome iteration

2010-08-29 Thread bruno.desthuilli...@gmail.com
On 27 août, 20:05, Jussi Piitulainen > def palindromep(s): >     return ( s == "" or >              ( s[0] == s[-1] and >                palindromep(s[1:-1]) ) ) > I-can-write-lisp-in-any-language-p !-) -- http://mail.python.org/mailman/listinfo/python-list

Re: palindrome iteration

2010-08-29 Thread bruno.desthuilli...@gmail.com
On 27 août, 18:20, Mark Lawrence wrote: > On 27/08/2010 15:43, Bruno Desthuilliers wrote: > > > Dave Angel a écrit : > > (snip) > > >> or (untested) > >> def is_palindrom(s): > >> s = s.lower() > >> return s == s[::-1] > > > Right, go on, make me feel a bit more stupid :-/ > > Who's next ? > > It

Re: palindrome iteration

2010-08-29 Thread Matteo Landi
I thought they reached you. Here they are again: def palindrome(str, i=0, j=-1): try: if str[i] == str[j]: return palindrome(str, i + 1, j - 1) return False except IndexError: return True def palindrome(str, i=0, j=-1): try

Re: palindrome iteration

2010-08-29 Thread Arnaud Delobelle
Matteo Landi writes: > Well, I tried the also the solution posted above (recursive w/o > slicing and iterative), and I discovered they were the slowest.. > > is_palindrome_recursive 2.68151649808 > is_palindrome_slice 0.44510699381 > is_palindrome_list 1.93861944217 > is_palindrome_reversed 3.289

Re: palindrome iteration

2010-08-29 Thread Gregory Ewing
Steven D'Aprano wrote: I'm not entirely sure what the use-case for swapcase is. Obviously it's for correcting things that were typed in with tHE cAPS lOCK kEY oN bY mISTAKE. :-) -- Greg -- http://mail.python.org/mailman/listinfo/python-list

Re: palindrome iteration

2010-08-29 Thread Matteo Landi
Well, I tried the also the solution posted above (recursive w/o slicing and iterative), and I discovered they were the slowest.. is_palindrome_recursive 2.68151649808 is_palindrome_slice 0.44510699381 is_palindrome_list 1.93861944217 is_palindrome_reversed 3.28969831976 is_palindrome_recursive_no_

Re: palindrome iteration

2010-08-28 Thread Josh English
This whole conversation got interesting, so I thought I'd run some speed tests: The code: from timeit import Timer def is_palindrome_recursive(s): if len(s) <= 1: return True if s[0] != s[-1]: return False else: return is_palindrome(s[1:-1]) def is_palindrome_

Re: palindrome iteration

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 15:11:03 +0300, Jussi Piitulainen wrote: [...] > When I said that there could be such a method, I was merely objecting to > a statement, made in response to me, that there could not be such a > method because strings are immutable. You clearly agree with me that > that statemen

Re: palindrome iteration

2010-08-28 Thread Dave Angel
s. It would return the reversed string, like .swapcase() returns the swapcased string. Could be, but the main use case seems to be for palindrome testing ;-) Given that slicing and reversed() can do the same thing, the need is thin. The need is quite thin, but immutability of strin

Re: palindrome iteration

2010-08-28 Thread D'Arcy J.M. Cain
On Sat, 28 Aug 2010 09:48:47 +0100 Ian wrote: > > def palindromep(s): > > def reversed(s): > > return s[::-1] > > return s == reversed(s) > I like this. > > s[::-1] is obscure and non-obvious, especially to Python noobs. > > This makes it clear what is going on and why at a co

Re: palindrome iteration

2010-08-28 Thread Jon Clements
On Aug 28, 11:55 am, Steven D'Aprano wrote: > On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote: > > Terry Reedy writes: > >> On 8/27/2010 3:43 PM, Jussi Piitulainen wrote: > >> > Dave Angel writes: [snip] > Not everything needs to be a built-in method. There is already a standard > way

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Paul Rubin writes: > Ian writes: > > On 27/08/2010 21:51, Jussi Piitulainen wrote: > >> Meanwhile, I have decided to prefer this: > >> > >> def palindromep(s): > >> def reversed(s): > >> return s[::-1] > >> return s == reversed(s) > > I like this. > > s[::-1] is obscure and non-

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
e not - and I am quite aware that Python is not Lisp. My background is elsewhere, I was not paying particular attention to the name at all, and I just could not be bothered to look up what implications any of palindrome, palindromic, ispalindrome, is_palindrome, isPalindrome, has_palindrome_nature,

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
strings. It would return >>> > the reversed string, like .swapcase() returns the swapcased string. >>> >>> Could be, but the main use case seems to be for palindrome testing ;-) >>> Given that slicing and reversed() can do the same thing, the need is >>&

Re: palindrome iteration

2010-08-28 Thread Steven D'Aprano
ersed string, like .swapcase() returns the swapcased string. >> >> Could be, but the main use case seems to be for palindrome testing ;-) >> Given that slicing and reversed() can do the same thing, the need is >> thin. > > The need is quite thin, but immutability of strings

Re: palindrome iteration

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 09:48:47 +0100, Ian wrote: > On 27/08/2010 21:51, Jussi Piitulainen wrote: >> Meanwhile, I have decided to prefer this: >> >> def palindromep(s): >> def reversed(s): >> return s[::-1] >> return s == reversed(s) > I like this. It's silly, needlessly complicat

Re: palindrome iteration

2010-08-28 Thread Arnaud Delobelle
Paul Rubin writes: > Ian writes: >> On 27/08/2010 21:51, Jussi Piitulainen wrote: >>> Meanwhile, I have decided to prefer this: >>> >>> def palindromep(s): >>> def reversed(s): >>> return s[::-1] >>> return s == reversed(s) >> I like this. >> s[::-1] is obscure and non-obviou

Re: palindrome iteration

2010-08-28 Thread Paul Rubin
Ian writes: > On 27/08/2010 21:51, Jussi Piitulainen wrote: >> Meanwhile, I have decided to prefer this: >> >> def palindromep(s): >> def reversed(s): >> return s[::-1] >> return s == reversed(s) > I like this. > s[::-1] is obscure and non-obvious, especially to Python noobs.

Re: palindrome iteration

2010-08-28 Thread Ian
On 27/08/2010 21:51, Jussi Piitulainen wrote: Meanwhile, I have decided to prefer this: def palindromep(s): def reversed(s): return s[::-1] return s == reversed(s) I like this. s[::-1] is obscure and non-obvious, especially to Python noobs. This makes it clear what is goin

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
ems like a bit of overkill... Why would you want to define a > function in a function for something trivial like this? Just > > def palindrome(s): > return s[::-1] > > will do fine. I'm sure your version will do something just fine, but what that something is, I ca

Re: palindrome iteration

2010-08-27 Thread Jussi Piitulainen
main use case seems to be for palindrome testing ;-) > Given that slicing and reversed() can do the same thing, the need is thin. The need is quite thin, but immutability of strings is not an issue, just like there can be .swapcase() though strings are immutable. That is all I am saying above. -

Re: palindrome iteration

2010-08-27 Thread Richard Arts
versed would be better called .reversed(). >> >> Yes, agreed. >> >> Meanwhile, I have decided to prefer this: >> >> def palindromep(s): >>    def reversed(s): >>        return s[::-1] >>    return s == reversed(s) >> -- >> http://mail.p

Re: palindrome iteration

2010-08-27 Thread Richard Arts
   def reversed(s): >        return s[::-1] >    return s == reversed(s) > -- > http://mail.python.org/mailman/listinfo/python-list > That seems like a bit of overkill... Why would you want to define a function in a function for something trivial like this? Just def palindrome(s): return s[::-1] will do fine. Of course, you can stick the inner function in a library somewhere if you like. Regards, Richard -- http://mail.python.org/mailman/listinfo/python-list

Re: palindrome iteration

2010-08-27 Thread Terry Reedy
On 8/27/2010 3:43 PM, Jussi Piitulainen wrote: Dave Angel writes: There could easily be a .reverse() method on strings. It would return the reversed string, like .swapcase() returns the swapcased string. Could be, but the main use case seems to be for palindrome testing ;-) Given that

Re: palindrome iteration

2010-08-27 Thread Jussi Piitulainen
MRAB writes: > On 27/08/2010 20:43, Jussi Piitulainen wrote: >> Dave Angel writes: >>> Jussi Piitulainen wrote: Agreed. But is there any nicer way to spell .reverse than [::-1] in Python? There is .swapcase() but no .reverse(), right? >>> There can't be a .reverse() method on string,

Re: palindrome iteration

2010-08-27 Thread MRAB
On 27/08/2010 20:43, Jussi Piitulainen wrote: Dave Angel writes: Jussi Piitulainen wrote: Ian writes: Of course, the simpler way is to use the definition of a Palindrome as the same backwards and forwards. def isPalindrome(pal) return pal == pal.reverse Agreed. But is there any

Re: palindrome iteration

2010-08-27 Thread Jussi Piitulainen
Dave Angel writes: > Jussi Piitulainen wrote: >> Ian writes: >>> Of course, the simpler way is to use the definition of a >>> Palindrome as the same backwards and forwards. >>> >>> def isPalindrome(pal) >>> return pal == pal.reverse >

Re: palindrome iteration

2010-08-27 Thread D'Arcy J.M. Cain
On Fri, 27 Aug 2010 12:02:39 -0400 "D'Arcy J.M. Cain" wrote: > On Fri, 27 Aug 2010 11:49:42 -0400 > "D'Arcy J.M. Cain" wrote: > > is_palindrome = lambda x: x == x.lower()[::-1] > > Oops. Simple and wrong. > > is_palindrome = lambda x: x.lower() == x.lower()[::-1] slightly more efficient I thi

Re: palindrome iteration

2010-08-27 Thread Dave Angel
Jussi Piitulainen wrote: Ian writes: If you want to or must do it recursively. (Shown in pseudo code to make the logic clearer) def isPalindrome(pal) ''' test pal (a list) is a palindrome ''' if length of pal = 1 return True # all one

Re: palindrome iteration

2010-08-27 Thread Jussi Piitulainen
Ian writes: > If you want to or must do it recursively. > (Shown in pseudo code to make the logic clearer) > > def isPalindrome(pal) > ''' test pal (a list) is a palindrome ''' > if length of pal = 1 > return True # all

Re: palindrome iteration

2010-08-27 Thread MRAB
On 27/08/2010 18:28, Mark Lawrence wrote: On 27/08/2010 17:53, MRAB wrote: On 27/08/2010 17:20, Mark Lawrence wrote: On 27/08/2010 15:43, Bruno Desthuilliers wrote: Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel

Re: palindrome iteration

2010-08-27 Thread Terry Reedy
On 8/27/2010 4:53 AM, Baba wrote: level: beginner the following code looks ok to me but it doesn't work. I would like some hints as to where my reasoning / thought goes wrong def i_palindrome(pal): while len(pal)>1: if pal[0] == pal[-1]: pal=pal[1:-1] return True print i_palindrome(

Re: palindrome iteration

2010-08-27 Thread Mark Lawrence
On 27/08/2010 17:53, MRAB wrote: On 27/08/2010 17:20, Mark Lawrence wrote: On 27/08/2010 15:43, Bruno Desthuilliers wrote: Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel a bit more stupid :-/ Who's next ? It co

Re: palindrome iteration

2010-08-27 Thread MRAB
On 27/08/2010 17:20, Mark Lawrence wrote: On 27/08/2010 15:43, Bruno Desthuilliers wrote: Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel a bit more stupid :-/ Who's next ? It could be worse, try responding to is

Re: palindrome iteration

2010-08-27 Thread Ian
lindrome('annab') If you want to or must do it recursively. (Shown in pseudo code to make the logic clearer) def isPalindrome(pal) ''' test pal (a list) is a palindrome ''' if length of pal = 1 return True # all one letter strings are pal

Re: palindrome iteration

2010-08-27 Thread Mark Lawrence
On 27/08/2010 15:43, Bruno Desthuilliers wrote: Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel a bit more stupid :-/ Who's next ? It could be worse, try responding to issue 9702. :) Cheers. Mark Lawrence. -- h

Re: palindrome iteration

2010-08-27 Thread D'Arcy J.M. Cain
On Fri, 27 Aug 2010 11:49:42 -0400 "D'Arcy J.M. Cain" wrote: > is_palindrome = lambda x: x == x.lower()[::-1] Oops. Simple and wrong. is_palindrome = lambda x: x.lower() == x.lower()[::-1] -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/|

Re: palindrome iteration

2010-08-27 Thread D'Arcy J.M. Cain
On Fri, 27 Aug 2010 16:43:16 +0200 Bruno Desthuilliers wrote: > Dave Angel a écrit : > > def is_palindrom(s): > >s = s.lower() > >return s == s[::-1] > > > Right, go on, make me feel a bit more stupid :-/ > Who's next ? How about a one-liner? is_palindrome = lambda x: len(x)> 0 and x =

Re: palindrome iteration

2010-08-27 Thread Bruno Desthuilliers
Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel a bit more stupid :-/ Who's next ? -- http://mail.python.org/mailman/listinfo/python-list

Re: palindrome iteration

2010-08-27 Thread Bruno Desthuilliers
A') So you just have to extract the symetric halves, reverse one, and compare both (case insensitive compare while we're at it). Yes, this is a correct observation, but it is not necessary to compare the halves; Simply compare the complete string with its reverse. If they match, it is a pali

Re: palindrome iteration

2010-08-27 Thread Dave Angel
is > 1 - exiting this loop means all compared chars were identical hence it is a palindrome and i return True Your problem is that when first and last char are not equal, you don't exit the while loop. You need a "return False" somewhere here, ie: def is_palindrom(pal): while

Re: palindrome iteration

2010-08-27 Thread Matteo Landi
> Yes, this is a correct observation, but it is not necessary to compare > the halves; Simply compare the complete string with its reverse. If > they match, it is a palindrome. I've always used to implement the is_palindrome function as you suggest, i.e. comparing the original s

Re: palindrome iteration

2010-08-27 Thread Richard Arts
') > > So you just have to extract the symetric halves, reverse one, and compare > both (case insensitive compare while we're at it). Yes, this is a correct observation, but it is not necessary to compare the halves; Simply compare the complete string with its reverse. If th

Re: palindrome iteration

2010-08-27 Thread Bruno Desthuilliers
means all compared chars were identical hence it is a palindrome and i return True Your problem is that when first and last char are not equal, you don't exit the while loop. You need a "return False" somewhere here, ie: def is_palindrom(pal): while len(pal)>1: # NB : inve

Re: palindrome iteration

2010-08-27 Thread Peter Otten
ual continue > - create a new, shorter string starting at index 1 and ending at > second last index (up to but not including index-1 > -restart the while loop as long as length of string is > 1 > - exiting this loop means all compared chars were identical hence it > is a palindrome an

Re: palindrome iteration

2010-08-27 Thread Xavier Ho
One possible reason I can think of - "- exiting this loop means all compared chars were identical hence it is a palindrome and i return True" is probably incorrect reasoning. Think again. Also, you may consider posting your code in a way that preserves the whitespace characters. Cheer

palindrome iteration

2010-08-27 Thread Baba
ting this loop means all compared chars were identical hence it is a palindrome and i return True tnx Baba -- http://mail.python.org/mailman/listinfo/python-list

Re: palindrome function

2008-07-12 Thread Terry Reedy
Mensanator wrote: It hasn't. and here's why: IDLE 2.6b1 seq=['a','n','n','a'] seq.reversed() Traceback (most recent call last): File "", line 1, in seq.reversed() AttributeError: 'list' object has no attribute 'reversed' My apologies. reversed() is a builtin func, not a method, a

Re: palindrome function

2008-07-12 Thread Mensanator
; >> spam = ['a', 'n', 'n', 'a'] > >> eggs = spam[:] > >> if spam.reverse() == eggs: > >> � � print "Palindrome" > > > Your explanation is correct, but your example code compares None to > > ['a&#x

Re: palindrome function

2008-07-12 Thread Terry Reedy
eturn None. Hence, the comparison you are doing is between the original list and a None, which is False, naturally. Try this: spam = ['a', 'n', 'n', 'a'] eggs = spam[:] if spam.reverse() == eggs: print "Palindrome" Your explanation is correct, bu

Re: palindrome function

2008-07-12 Thread Denis Kasak
None. Hence, the comparison you are doing is between the original list and a None, which is False, naturally. Try this: spam = ['a', 'n', 'n', 'a'] eggs = spam[:] if spam.reverse() == eggs: print "Palindrome" Your explanation is correct, bu

Re: palindrome function

2008-07-11 Thread Peter Otten
None. Hence, the comparison you are > doing is between the original list and a None, which is False, naturally. > Try this: > > spam = ['a', 'n', 'n', 'a'] > eggs = spam[:] > if spam.reverse() == eggs: > print "Palindrome"

Re: palindrome function

2008-07-11 Thread Paul McGuire
On Jul 11, 6:20 pm, Mensanator <[EMAIL PROTECTED]> wrote: > > Try this: > > > spam = ['a', 'n', 'n', 'a'] > > eggs = spam[:] > > if spam.reverse() == eggs: > >     print "Palindrome" > >

Re: palindrome function

2008-07-11 Thread Hugh M
omparison you are doing > is between the original list and a None, which is False, naturally. > Try this: > > spam = ['a', 'n', 'n', 'a'] > eggs = spam[:] > if spam.reverse() == eggs: > print "Palindrome" > > Um, wouldn

Re: palindrome function

2008-07-11 Thread Mensanator
st which > called it. It does not return a /new/ list which is a reversed version > of the original, as you expected it to. Since it doesn't return anything > explicitly, Python makes it return None. Hence, the comparison you are > doing is between the original list and a None, which i

Re: palindrome function

2008-07-11 Thread bearophileHUGS
Denis Kasak: > spam = ['a', 'n', 'n', 'a'] > eggs = spam[:] > if spam.reverse() == eggs: > print "Palindrome" An alternative version: >>> txt = "anna" >>> txt == txt[::-1] True >>> txt = "

Re: palindrome function

2008-07-11 Thread kdt
st which > called it. It does not return a /new/ list which is a reversed version > of the original, as you expected it to. Since it doesn't return anything > explicitly, Python makes it return None. Hence, the comparison you are > doing is between the original list and a None, which

Re: palindrome function

2008-07-11 Thread Denis Kasak
nything explicitly, Python makes it return None. Hence, the comparison you are doing is between the original list and a None, which is False, naturally. Try this: spam = ['a', 'n', 'n', 'a'] eggs = spam[:] if spam.reverse() == eggs: print &quo

palindrome function

2008-07-11 Thread kdt
Hi all, Can someone please explain to me why the following evaluates as false? >>>list=['a','n','n','a'] >>>list==list.reverse() >>>False I'm stumped :s -- http://mail.python.org/mailman/listinfo/python-list

Amusement - rotational palindrome generator

2007-12-20 Thread Paul McGuire
Here is some semi-obfuscated Python, to generate rotational palindromes: from random import choice base = "sznuoxpqbdMWOINZXSH" rot = dict(zip(base,"szunoxdbqpWMOINZXSH")) for i in range(40): s1 = [choice(base) for j in range(choice((2,3,4)))] start = (1,2)[rot[s1[-1]]==s1[-1] and choice