1

Looking to put together a simple python function that checks the following

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").

end_other('Hiabc', 'abc') → True
end_other('AbC', 'HiaBc') → True
end_other('abc', 'abXabc') → True
0

6 Answers 6

4

Try with

def end_other(s1, s2):
    s1 = s1.lower()
    s2 = s2.lower()
    return s1.endswith(s2) or s2.endswith(s1)
Sign up to request clarification or add additional context in comments.

5 Comments

thanks. on the right track now . I understand that people might think i am just looking for the answer but im really not. im new to this and there is no point in spending all day staring at the screen when you guys can help me learn the same thing in a few minutes. i will then use what you have given me for future work.
the function above still returns true even if i add a string that should return false ? i have added print end_other ('nononono,nonon') and i still get true
ahem.. check your quotes. I think you want end_other('nononono','nonon'), which returns False, as expected
:-) Thanks a mill. Learned loads from this
you are welcome. Check out @Pruthvi Raj and AChampion's answers. You can learn a lot from those as well.
1

You can use regex

def end_other(s1,s2):
    return bool(re.search(s1+'$',s2,re.I)) or bool(re.search(s2+'$',s1,re.I))

Comments

1

Or if you need to "create" your own function:

def check(str1, str2):
    str1 = str1.lower()
    str2 = str2.lower()
    check = True

    # If string 1 is bigger then string 2:
    if( len(str1) > len(str2) ):
        # For each character of string 2
        for i in range(len(str2)):
            # Compare the character i of string 2 with the character at the end of string 1, keeping the order
            if str2[i] != str1[-(len(str2)-i)]:
            check = False

    # If string 2 is bigger then string 1:
    else:
        # For each character of string 1
        for i in range(len(str1)):
            # Compare the character i of string 1 with the character at the end of string 2, keeping the order
            if str1[i] != str2[-(len(str1)-i)]:
                check = False
    return check

So, basically, if string1 = "ABCD" and string2 = "CD", it will check character 0 of string2 with 2 of string1 and 1 of string2 with 3 of string1

5 Comments

netiquette contemplates citing previous answers, if repeated in yours
Just edited it, I started writing the answer before any other so haven't seen it @Pynchia
can you explan this code please ? I dont understand the if statements and the syntax there
this also gives true for everything , even if it should return false
Commented and explained @johndoe12345. Please show me a case where it returns true and should return false..
1

Here's another approach using zip, which avoids having to do multiple passes of endswith(), re.search() or slicing. It iterates the 2 strings in reverse and returns True if all the letters are equal up to the exhaustion of one of the strings:

def end_other(s1, s2):
    return all(a == b for a, b in zip(reversed(s1.lower()), reversed(s2.lower())))

In Python2 you can use itertools.izip() for a marginal space improvement:

>>> end_other('Hiabc', 'abc')
True
>>> end_other('AbC', 'HiaBc')
True
>>> end_other('abc', 'abXabc')
True
>>> end_other('cbc', 'abXabc')
False

Comments

0

You could use slicing.

>>> s1 = "Hiabc"
>>> s2 = "abc"
>>> l = len(s2)
>>> s2 == s1[-l:]
True
>>> s2 = "aBc"
>>> s2 == s1[-l:]
False

You can read more about string and slicing here.

Slicing means to get part of the string based on passed index. General construction look like this: string[starting_index:ending_index].

Let's take a look at this:

string1 = "doggy" string1[0:1] -> "d" string1[0:2] -> "do" string1[0:] = string[:] = "doggy" (from zero to end)

You can also pass index lesser than 0:

string1[-1:] (from -1 to end) -> "y" So in the code above calling s1[-l:] means starting position is the length of s2 sentece. In this case s1[-l:] = 'abc' = s1[-3:]. Because lenght of s2 is 3.

2 Comments

the slicing method above implies that s1 will always be Hiabc, what if it wasnt ? str.endswith sounds better ? how can i make str1 the value of the first string and str2 the value of the second one, ? i cant hard code these cos they are different in each line .
@johndoe12345 My answer shows you path you should follow. You should go ask google how to define functions in python and try to solve rest of the problem by yourself.
0

You can use OOP if you want, subclassing str and writing an appropriate method:

>>> class String(str):
...     def isend(self, other):
...             self = self.lower()
...             other = other.lower()
...             return self.endswith(other) or other.endswith(self)
...
>>> a = String('HiAbC')
>>> b = String('Bc')
>>> a.isend(b)
True
>>> b.isend(a)
True
>>> c = String('Hello')
>>> d = String('el')
>>> c.isend(d)
False
>>> d.isend(c)
False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.