알파벳으로 이루어진 문자열 myString과 pat이 주어집니다. myString의 연속된 부분 문자열 중 pat이 존재하면 1을 그렇지 않으면 0을 return 하는 solution 함수를 완성해 주세요.
단, 알파벳 대문자와 소문자는 구분하지 않습니다.
code - 1차
def solution(myString, pat):
lowerString = myString.lower()
lowerPat = pat.lower()
if lowerPat in lowerString:
return 1
else:
return 0
- lower~
- 문제에서 알파벳 대문자와 소문자를 구분하지 않는다고 했기에, 모두 소문자로 변환합니다.
- lowerString, lowerPat
- if문
- lowerPat이 lowerString 문자열에 있다면 return 1
- 없다면 return 0
code - 2차
def solution(myString, pat):
# True 1, False 0 반환
return int(pat.lower() in myString.lower())
- in 연산 → int
- in 연산 시 결과값이 True, False로 나옵니다. 이를 int로 변환하면 1, 0 값을 return 할 수 있습니다.
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] 조건에 맞게 수열 변환하기 3 - 181835, 리스트 컴프리헨션 (0) | 2024.08.21 |
---|---|
[Python/Silver II] DFS와 BFS - 1260, 스택, 재귀함수, deque, queue (0) | 2024.08.21 |
[Python] append() vs. extend() 공통점과 차이점 (0) | 2024.08.17 |
[Python/level 0] 제곱수 판별하기 - 120909, isqrt(), is_integer() (1) | 2024.08.17 |
[Python/level 0] A 강조하기 - 181874, lower(), replace() (0) | 2024.08.17 |