#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Python code for a string program


    HI!
    could anyone help me out with the code for below question:

    Given two strings, return True if either of the strings appears at the very end of the other string, ignoring upper/lower case differences (in other words, the computation should not be "case sensitive"). Note: s.lower() returns the lowercase version of a string.

    end_other('Hiabc', 'abc') → True
    end_other('AbC', 'HiaBc') → True
    end_other('abc', 'abXabc') → True
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,840
    Rep Power
    480
    Code:
    >>> import pprint
    >>> pprint . pprint ( dir ( '' ) )
    ['__add__',
     '__class__',
     ...
     'endswith',
     ...
     'zfill']
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    475
    Rep Power
    33
    You can either reverse the letters in the words and check the beginning or convert to lists and start at -1 comparing letters.
  6. #4
  7. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Code:
    def end_other(x, y):
        return x[::-1][0:len(y)] == y[::-1]
    case-insensitive:
    Code:
    def end_other(x, y):
        return (x[::-1][0:len(y)]).lower() == (y[::-1]).lower()
    Last edited by AndrewSW; December 9th, 2012 at 03:04 PM.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,840
    Rep Power
    480
    You don't seem to get it.

    endswith is a string method.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Originally Posted by b49P23TIvg
    You don't seem to get it.

    endswith is a string method.
    I suppose if this is homework they might not be expected to use a function, other than lower().. maybe len(). Although, my method is a bit cheeky
  12. #7
  13. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Originally Posted by dwblas
    You can either reverse the letters in the words and check the beginning or convert to lists and start at -1 comparing letters.
    Converting to lists is unnecessary as a string is already an iterable.

IMN logo majestic logo threadwatch logo seochat tools logo