대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.
예를 들어 s가 "pPoooyY"면 true를 return하고 "Pyy"라면 false를 return합니다.
code - 1차
- for문을 돌려서 i가 "p" 혹은 "y"와 같다면 countP, countY의 값을 +1씩 더해진다.
- countP와 countY의 수가 동일하면 True / 동일하지 않다면 False가 출력된다.
def solution(s):
countP = 0
countY = 0
lowerS = s.lower()
for i in lowerS:
if i == "p":
countP += 1
elif i == "y":
countY += 1
if countP == countY:
return True
else:
return False
code - 2차
- count() 함수를 사용하여 다시 풀어보았다.
def solution(s):
lowerS = s.lower()
if lowerS.count("p") == lowerS.count("y"):
return True
else:
return False
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] 자릿수 더하기 - 12931, map, sum (0) | 2024.08.17 |
---|---|
[Python/level 0] 주사위의 개수 - 120845, list (0) | 2024.08.07 |
[Python/level 0] 문자열 정수의 합 - 181849, sum, 리스트 컴프리헨션 (0) | 2024.08.03 |
[Python/level 0] n의 배수 고르기 - 120905, 리스트 컴프리헨션 (0) | 2024.08.03 |
[Python/level 0] 암호 해독 - 120892, Slicing (0) | 2024.08.03 |