palindrome function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kdt

    palindrome function

    Hi all,

    Can someone please explain to me why the following evaluates as false?
    >>>list=['a','n','n','a']
    >>>list==list.r everse()
    >>>False
    I'm stumped :s
  • Denis Kasak

    #2
    Re: palindrome function

    On Sat, Jul 12, 2008 at 12:22 AM, kdt <dorjetarap@goo glemail.comwrot e:
    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
    Read the documentation on list.reverse().

    Basically, it reverses the list in place, so it modifies the list 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 is False, naturally.
    Try this:

    spam = ['a', 'n', 'n', 'a']
    eggs = spam[:]
    if spam.reverse() == eggs:
    print "Palindrome "

    Also, 'list' is a really bad name for a list, since this is the name of
    the builtin type object for the list type.

    --
    Denis Kasak

    Comment

    • kdt

      #3
      Re: palindrome function

      On Jul 11, 11:34 pm, Denis Kasak <denis.kasak271 8281...@gmail.c om>
      wrote:
      On Sat, Jul 12, 2008 at 12:22 AM, kdt <dorjeta...@goo glemail.comwrot e:
      >
       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
      >
      Read the documentation on list.reverse().
      >
      Basically, it reverses the list in place, so it modifies the list 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 is False, naturally.
      Try this:
      >
      spam = ['a', 'n', 'n', 'a']
      eggs = spam[:]
      if spam.reverse() == eggs:
          print "Palindrome "
      >
      Also, 'list' is a really bad name for a list, since this is the name of
      the builtin type object for the list type.
      >
      --
      Denis Kasak
      thanks for the explanation :D

      Comment

      • bearophileHUGS@lycos.com

        #4
        Re: palindrome function

        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 = "annabella"
        >>txt == txt[::-1]
        False

        Bye,
        bearophile

        Comment

        • Mensanator

          #5
          Re: palindrome function

          On Jul 11, 5:34 pm, Denis Kasak <denis.kasak271 8281...@gmail.c om>
          wrote:
          On Sat, Jul 12, 2008 at 12:22 AM, kdt <dorjeta...@goo glemail.comwrot e:
          >
           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
          >
          Read the documentation on list.reverse().
          >
          Basically, it reverses the list in place, so it modifies the list 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 is False, naturally.
          Try this:
          >
          spam = ['a', 'n', 'n', 'a']
          eggs = spam[:]
          if spam.reverse() == eggs:
              print "Palindrome "
          You could also do
          >>spam = ['a','n','n','a']
          >>if spam == [i for i in reversed(spam)]:
          print "Palindrome "

          >
          Also, 'list' is a really bad name for a list, since this is the name of
          the builtin type object for the list type.
          >
          --
          Denis Kasak

          Comment

          • Paul McGuire

            #6
            Re: palindrome function

            On Jul 11, 6:20 pm, Mensanator <mensana...@aol .comwrote:
            Try this:
            >
            spam = ['a', 'n', 'n', 'a']
            eggs = spam[:]
            if spam.reverse() == eggs:
                print "Palindrome "
            >
            You could also do
            >
            >spam = ['a','n','n','a']
            >if spam == [i for i in reversed(spam)]:
            >
                    print "Palindrome "
            >
            Or instead of this:

            [ i for i in generator_expre ssion]

            just use the list constructor:

            list(generator_ expression)

            -- Paul

            Comment

            • Peter Otten

              #7
              Re: palindrome function

              Denis Kasak wrote:
              Basically, it reverses the list in place, so it modifies the list 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 is False, naturally.
              Try this:
              >
              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', 'n', 'n', 'a'] and therefore won't print "Palindrome ", either.

              Peter

              Comment

              • Denis Kasak

                #8
                Re: palindrome function

                Peter Otten wrote:
                Denis Kasak wrote:
                >
                >Basically, it reverses the list in place, so it modifies the list 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 is False, naturally.
                >Try this:
                >>
                >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', 'n', 'n', 'a'] and therefore won't print "Palindrome ", either.
                Of course. Thank you for the correction. I guess you know your caffeine
                has started to wear off when you start making the same mistakes you were
                trying to fix. :-)

                --
                Denis Kasak

                Comment

                • Terry Reedy

                  #9
                  Re: palindrome function



                  Peter Otten wrote:
                  Denis Kasak wrote:
                  >
                  >Basically, it reverses the list in place, so it modifies the list 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 is False, naturally.
                  >Try this:
                  >>
                  >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', 'n', 'n', 'a'] and therefore won't print "Palindrome ", either.
                  I don't know if this was posted yet, but 'seq.reversed() == seq' is the
                  simple way to test for 'palindomeness' .

                  Comment

                  • Mensanator

                    #10
                    Re: palindrome function

                    On Jul 12, 2:18�pm, Terry Reedy <tjre...@udel.e duwrote:
                    Peter Otten wrote:
                    Denis Kasak wrote:
                    >
                    Basically, it reverses the list in place, so it modifies the list 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 is False, naturally.
                    Try this:
                    >
                    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', 'n', 'n', 'a'] and therefore won't print "Palindrome ", either.
                    >
                    I don't know if this was posted yet,
                    It hasn't. and here's why:

                    IDLE 2.6b1
                    >>seq=['a','n','n','a']
                    >>seq.reversed( )
                    Traceback (most recent call last):
                    File "<pyshell#3 >", line 1, in <module>
                    seq.reversed()
                    AttributeError: 'list' object has no attribute 'reversed'

                    but 'seq.reversed() == seq' is the
                    simple way to test for 'palindomeness' .
                    Not in 2.x.

                    Comment

                    • Terry Reedy

                      #11
                      Re: palindrome function



                      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 "<pyshell#3 >", line 1, in <module>
                      seq.reversed()
                      AttributeError: 'list' object has no attribute 'reversed'
                      My apologies. reversed() is a builtin func, not a method, and it
                      produces an iterator, not a seq. SO, corrected,
                      >>for s in ((1,2,3,2,1), [1,2,3,2,1]):
                      .... type(s)(reverse d(s)) == s
                      ....
                      True
                      True
                      >>s = '12121'
                      >>''.join(rever sed(s)) == s
                      True

                      Comment

                      Working...