나는 이렇게 학습한다/Algorithm & SQL

String ends with?

daco2020 2022. 4. 21. 23:09
반응형

Description:

Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).

Examples:

solution('abc', 'bc') # returns true
solution('abc', 'd') # returns false

 

Solution:

1. Return True if the last digit of 'string' is the same as 'ending'.
2. If 'ending' is an empty string, return True.
3. If not, return False.

 

def solution(string, ending):
    return string[-len(ending):] == ending or not ending

A slice was compared whether the last digit of 'string' matches 'ending'.
If not, I added code to check if 'ending' is empty.

 

 

Using built-in functions makes it easier to solve.

def solution(string, ending):
    return string.endswith(ending)

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Shortest Word  (0) 2022.04.23
Vowel Count  (0) 2022.04.22
Remove the minimum  (0) 2022.04.20
Sum of two lowest positive integers  (0) 2022.04.19
Square Every Digit  (0) 2022.04.18