어떤 문자열에 대해서 접미사는 특정 인덱스부터 시작하는 문자열을 의미합니다. 예를 들어, "banana"의 모든 접미사는 "banana", "anana", "nana", "ana", "na", "a"입니다.
문자열 my_string과 is_suffix가 주어질 때, is_suffix가 my_string의 접미사라면 1을, 아니면 0을 return 하는 solution 함수를 작성해 주세요.
code
def solution(my_string, is_suffix):
return(int(my_string.endswith(is_suffix)))
- return int(my_string.endswith(is_suffix))
- endswith의 결과값은 True or False로 나옵니다.
- int(True)는 1, int(Flase)는 0이기 때문에 int(...)로 리턴하면 원하는 0 또는 1의 값을 얻을 수 있습니다.
https://github.com/seonmin5/codingtest_Python
GitHub - seonmin5/codingtest_Python
Contribute to seonmin5/codingtest_Python development by creating an account on GitHub.
github.com
반응형
'Python' 카테고리의 다른 글
[Python/level 0] 가위 바위 보 - 120839, dictionary = {key:value} (0) | 2024.08.21 |
---|---|
[Python/level 1] 하샤드 수 - 12947, 리스트 컴프리헨션, sum, x%...==0 (0) | 2024.08.21 |
[Python/level 0] 접두사인지 확인하기 - 181906, startswith, int (1) | 2024.08.21 |
[Python/level 0] 홀수 vs 짝수 - 181887, sum, max (0) | 2024.08.21 |
[Python/level 0] 꼬리 문자열 - 181841, range(k, n+1, k) (0) | 2024.08.21 |