๋ฌธ์์ด ๋ด p์ y์ ๊ฐ์
https://school.programmers.co.kr/learn/courses/30/lessons/12916
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
ํ์ด 1
def solution(s):
# ๋ฌธ์์ด์ ๋ชจ๋ ์๋ฌธ์๋ก ๋ณํ
s = s.lower()
# 'p'์ 'y'์ ๊ฐ์๋ฅผ ์ผ๋ค
count_p = s.count('p')
count_y = s.count('y')
# 'p'์ 'y'์ ๊ฐ์๊ฐ ๊ฐ์์ง ํ์ธ
return count_p == count_y
# ์์ ํ
์คํธ
print(solution("pPoooyY")) # ์ถ๋ ฅ: True
print(solution("Pyy")) # ์ถ๋ ฅ: False
1. ๋ฌธ์์ด ๋ณํ: s.lower()๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด s๋ฅผ ์๋ฌธ์๋ก ๋ณํํฉ๋๋ค.
2. ๊ฐ์ ์ธ๊ธฐ: s.count('p')์ s.count('y')๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๊ฐ 'p'์ 'y'์ ๊ฐ์๋ฅผ ์ ๋๋ค.
3. ๋น๊ต ๋ฐ ๋ฐํ: count_p == count_y๊ฐ ์ฐธ์ด๋ฉด True, ๊ฑฐ์ง์ด๋ฉด False๋ฅผ ๋ฐํํฉ๋๋ค. ์ด๋ ํ์ด์ฌ์์ ๋น๊ต ์ฐ์ฐ์๊ฐ ๋ถ์ธ๋ฆฐ ๊ฐ์ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํฉ๋๋ค.
ํ์ด2
def solution(s):
s = s.lower()
count_p = s.count('p')
count_y = s.count('y')
if count_p == count_y:
return True
else:
return False
'์๊ณ ๋ฆฌ์ฆ ๐ก > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ํธ๋ํฐ ๋ฒํธ ๊ฐ๋ฆฌ๊ธฐ python (0) | 2024.06.12 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ํฐ์ผ๋ชฌ python (0) | 2024.06.11 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ ์ผ ์์ ์ ์ ๊ฑฐํ๊ธฐ python (0) | 2024.06.11 |
ํ๊ท ์ผ์ผ ๋์ฌ ์๊ธ ๊ตฌํ๊ธฐ (0) | 2023.11.19 |
์กฐ๊ฑด์ ๋ง๋ ๋์์ ์ ์ ๋ฆฌ์คํธ ์ถ๋ ฅํ๊ธฐ/ inner JOIN (0) | 2023.11.03 |