简体   繁体   中英

How to check if one string ends with another, or the other way around?

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

Examples / Tests:

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

My Code:

def end_other(s1, s2):
    
    s1 = s1.upper()
    s2 = s2.upper()
    
    if s1[2:6] == s2:
        return True
    elif s2[2:6] == s1:
        return True
    elif s2 == s1:
        return True    
    else:
        return False

What I expect is wrong.

(NB: this is a code practice from CodingBat

Not the greatest title ever... Anyways Hello! I am currently answering this question but I am not meeting all the exceptions D: Any help would be amazing! (Sorry again for the crappy title).

Question:


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

Examples / Tests:


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

Test Results:


Call                           Expected     Received    Correct
end_other('Hiabc', 'abc')          True     True    true
end_other('AbC', 'HiaBc')          True     True    true
end_other('abc', 'abXabc')         True     False   false #Cond. Not being met
end_other('Hiabcx', 'bc')          False        False   true
end_other('abc', 'abc')        True     True    true
end_other('xyz', '12xyz')          True     True    true
end_other('yz', '12xz')        False        False   true
end_other('ab', 'ab12')        False        False   true

(Sorry for failed Formatting)

My Code:


def end_other(s1,s2):

    s1 = s1.upper()
    s2 = s2.upper()

    if s1[2:6] == s2:
        return True
    elif s2[2:6] == s1:
        return True
    elif s2 == s1:
        return True    
    else:
        return False

What I expect is wrong:


The website I am using is not really known for it's solid answers... but I know it's quite possible there is a flaw in my code. So If you have any suggestions please let me know!

Not the greatest title ever... Anyways Hello! I am currently answering this question but I am not meeting all the exceptions D: Any help would be amazing! (Sorry again for the crappy title).

Question:


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

Examples / Tests:


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

Test Results:


Call                           Expected     Received    Correct
end_other('Hiabc', 'abc')          True     True    true
end_other('AbC', 'HiaBc')          True     True    true
end_other('abc', 'abXabc')         True     False   false #Cond. Not being met
end_other('Hiabcx', 'bc')          False        False   true
end_other('abc', 'abc')        True     True    true
end_other('xyz', '12xyz')          True     True    true
end_other('yz', '12xz')        False        False   true
end_other('ab', 'ab12')        False        False   true

(Sorry for failed Formatting)

My Code:


def end_other(s1,s2):

    s1 = s1.upper()
    s2 = s2.upper()

    if s1[2:6] == s2:
        return True
    elif s2[2:6] == s1:
        return True
    elif s2 == s1:
        return True    
    else:
        return False

What I expect is wrong:


The website I am using is not really known for it's solid answers... but I know it's quite possible there is a flaw in my code. So If you have any suggestions please let me know!

Not the greatest title ever... Anyways Hello! I am currently answering this question but I am not meeting all the exceptions D: Any help would be amazing! (Sorry again for the crappy title).

Question:


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

Examples / Tests:


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

Test Results:


Call                           Expected     Received    Correct
end_other('Hiabc', 'abc')          True     True    true
end_other('AbC', 'HiaBc')          True     True    true
end_other('abc', 'abXabc')         True     False   false #Cond. Not being met
end_other('Hiabcx', 'bc')          False        False   true
end_other('abc', 'abc')        True     True    true
end_other('xyz', '12xyz')          True     True    true
end_other('yz', '12xz')        False        False   true
end_other('ab', 'ab12')        False        False   true

(Sorry for failed Formatting)

My Code:


def end_other(s1,s2):

    s1 = s1.upper()
    s2 = s2.upper()

    if s1[2:6] == s2:
        return True
    elif s2[2:6] == s1:
        return True
    elif s2 == s1:
        return True    
    else:
        return False

What I expect is wrong:


The website I am using is not really known for it's solid answers... but I know it's quite possible there is a flaw in my code. So If you have any suggestions please let me know!

Not the greatest title ever... Anyways Hello! I am currently answering this question but I am not meeting all the exceptions D: Any help would be amazing! (Sorry again for the crappy title).

Question:


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

Examples / Tests:


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

Test Results:


Call                           Expected     Received    Correct
end_other('Hiabc', 'abc')          True     True    true
end_other('AbC', 'HiaBc')          True     True    true
end_other('abc', 'abXabc')         True     False   false #Cond. Not being met
end_other('Hiabcx', 'bc')          False        False   true
end_other('abc', 'abc')        True     True    true
end_other('xyz', '12xyz')          True     True    true
end_other('yz', '12xz')        False        False   true
end_other('ab', 'ab12')        False        False   true

(Sorry for failed Formatting)

My Code:


def end_other(s1,s2):

    s1 = s1.upper()
    s2 = s2.upper()

    if s1[2:6] == s2:
        return True
    elif s2[2:6] == s1:
        return True
    elif s2 == s1:
        return True    
    else:
        return False

What I expect is wrong:


The website I am using is not really known for it's solid answers... but I know it's quite possible there is a flaw in my code. So If you have any suggestions please let me know!

def end_other(a, b):
 return (a.lower()[-len(b):]==b.lower()) or (b.lower()[-len(a):]==a.lower())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM