어떤 문자열에 대해서 접두사는 특정 인덱스까지의 문자열을 의미합니다. 예를 들어, "banana"의 모든 접두사는 "b", "ba", "ban", "bana", "banan", "banana"입니다.
문자열 my_string과 is_prefix가 주어질 때, is_prefix가 my_string의 접두사라면 1을, 아니면 0을 return 하는 solution 함수를 작성해 주세요.
code - 1차
def solution(my_string, is_prefix):
if my_string.startswith(is_prefix) == True:
return 1
else:
return 0
- if my_string.startswith(is_prefix) == True:
- 만약 my_string의 접두사가 is_prefix가 맞다면 1을, 아니라면 0을 리턴합니다.
code - 2차
def solution(my_string, is_prefix):
return int(my_string.startswith(is_prefix))
- int(my_string.startswith(is_prefix))
- startswith로 확인한 값은 True, False로 결과값을 return합니다.
- True, False 값을 int로 변환하여 바로 return 하면 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 1] 하샤드 수 - 12947, 리스트 컴프리헨션, sum, x%...==0 (0) | 2024.08.21 |
|---|---|
| [Python/level 0] 접미사인지 확인하기 - 181908, endswith, int (0) | 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 |
| [Python/level 0] 꼬리 문자열 - 181841, continue (0) | 2024.08.21 |