Python Tutorial Question: ends with function

I have a python related question that I could not get through. The question is:

Ends on
For two lines, return True if one of the lines appears at the very end of the other line, ignoring the upper / lower case difference (in other words, the calculation should not be case sensitive).

My decision:

def end_other(a,b): s1=a.lower() s2=b.lower() if len(s1)>len(s2): if s1[-len(s2)]==s2[-len(s2)]: return True if len(s1)<len(s2): if s1[-len(s1)]==s2[-len(s1)]: return True if len(s1)==len(s2): if s1[-len(s1)]==s2[-len(s2)]: return True else: return False 

But the "Error Message" window shows: all public tests passed, but some private tests failed. You need to summarize your decision.

What is the problem with my solution? Or is there something I missed?

+4
source share
6 answers

How about using the built-in python str.endswith() method?

 def end_other(a, b): a_lower = a.lower() b_lower = b.lower() return a_lower.endswith(b_lower) or b_lower.endswith(a_lower) 
+16
source

I think this is exactly what you tried to do:

 def end_other ( a, b ): a = a.lower() b = b.lower() if a == b: return True elif len( a ) > len( b ): return b == a[-len( b ):] else: return a == b[-len( a ):] 

You had a few errors in your decision:

  • s1[-len(s2)] or generally s1[x] just gets one character. Therefore, if anything, you are simply comparing one character, but not the actual sequence of characters. You join a sequence from a string using s[x:y] (see the manual).
  • s2[-len(s2)] , even with a colon, as explained above, doesn't make much sense. You get access to s2 using your own length. But since s2 already a smaller string, you can compare it as a whole.
  • When one of your external ifs matches the situation, there is no other external situation. For example, if a longer than b , then there is no way that a shorter or the same length as b later. Therefore, you should make your support for the if structure. Instead of if ... if ... if ... else make a chain: if ... elif ... elif ... else . Then everyone automatically knows that only one of these cases can be applied.
  • When you do this, you can also leave the last if . Because when neither x < y nor y < x is true, then x is y .
  • You also need to change the way you return from the function. At the moment, you have a conditional output in each of your external if cases. Now, if the first case of if is applied (i.e. s1 longer than s2 ), and the internal if is not applied ( s1 does not end with s2 ), then you already know that the function should return False. Because there is no way that any other condition later in the code says otherwise, so you should immediately return False (especially when there is other code that can be executed later). And when you have if x: return True else: return False , you can just return x .
  • s1[-len(s1)]==s2[-len(s2)] : As already mentioned, you will have a problem when s1 and s2 are empty lines. len( "" ) = 0 , and if both lines are empty, then both lengths are identical. And then your code tries to use a range of indices on an empty string that will always fail (since there is not a single character). Instead, when you already know that the two lines are the same size, just compare them as a whole. It also helps to avoid blank lines.

In any case, if you did not need to implement it yourself, you really should use str.endswith .

+7
source

Comparison using an empty string will result in blurring. Use .endswith() .

+2
source
 def ends_with(str1, str2): str1 = str1.lower() str2 = str2.lower() return str1.endswith(str2) or str2.endswith(str1) 
+1
source

The problem with your code is comparing only one character from each line. You might want to use slicing notation, but you left the colon out of the brackets. For example, your code will be valid for "--- ba" and "bc". Then you do not return False when len (s1)! = Len (s2); however, Python functions always return something, and by default None, so the error may go unnoticed (since None evaluates as false) for some time.

 def either_endswith_lower(a, b): n = min(len(a), len(b)) return a[-n:].lower() == b[-n:].lower() 

This is the tutorial that you are following, but I would prefer to drop the subscript in the function and ask users to call func (a.lower (), b.lower ()) if that is what they want.

0
source

Indeed, ThiefMaster's answer is the most elegant.

Now just fix your code, taking into account the poke notes:

 def end_other(a,b): s1=a.lower() s2=b.lower() if len(s1)>len(s2): if s1[-len(s2):]==s2: return True elif len(s1)<len(s2): if s1==s2[-len(s1):]: return True else: return s1 == s2; return False 

I just used simple rules to simplify it:

  • use if / elif / else block instead of 3 if , as these three cases are mutually exclusive
  • s[-len(s):] equivalent to s
0
source

Source: https://habr.com/ru/post/1336755/


All Articles