๋ฐ์ํ
๋์ ํ์ด
- 1,2,3๋ฒ์ ๋ต๋ณ์๋ ๊ท์น์ ์ธ ๋ต๋ณ์ ์ฐ์ --> ๋ฏธ๋ฆฌ ๋ต๋ณ list๋ฅผ ๊ตฌ์ฑ
- 1,2,3๋ฒ์ ๋ต๋ณ์๋ ๊ฐ๊ฐ a,b,c์ ๋ณ์๋ก ์ ์ฅํ์ฌ ์ฌ์ฉํ ๊ฒ์
- input๋๋ answers์ ๊ธธ์ด๋ 10000๊น์ง ํ์ฅ๋ ์ ์์.
- 1๋ฒ ๋ต๋ณ์๊ฐ 5๊ฐ๋ง์ผ๋ก ์ด๋ฃจ์ด์ง ๊ท์น์ ํ์ฑํ๋ฏ๋ก, ๊ฐ์ฅ ์ ์ ๊ธธ์ด์ธ 1๋ฒ ๋ต๋ณ์๋ฅผ ๊ธฐ์ค์ผ๋ก len(answers)์ ๊ธธ์ด๋ฅผ ๋ง์ถฐ์ฃผ์์.
- for ๋ฐ๋ณต๋ฌธ์ ํ์ฉํด์ answers ์ ์ ๋ต๊ณผ ๋น๊ต
- ์ ๋ต๋ค์ max๊ฐ์ ๊ตฌํ๊ณ ๋์ ์๊ฐ ์กด์ฌํ ์ ์์ผ๋, for๋ฌธ์ ํ์ฉํ์ฌ max๊ฐ๊ณผ ๋์ผํ ๋ต๋ณ์๋ฅผ index๋ก ์ถ์ถ (abc๋ก ์ง์ ํ๊ธฐ ๋๋ฌธ์ ์ํ๋ ๋ต๋ณ์ ์ป์ผ๋ ค๋ฉด ์ธ๋ฑ์ค๋ก ์ถ์ถํด์ผํจ)
- ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ์ฌ return
def solution(answers):
a = [1, 2, 3, 4, 5]
b = [2,1,2,3,2,4,2,5]
c = [3,3,1,1,2,2,4,4,5,5]
i = 1
while len(a)*i < len(answers):
i+=1
a *= i
b *= i
c *= i
# ์ ์ ๊ณ์ฐ
a_score, b_score, c_score = 0,0,0
for j in range(0, len(answers)):
if answers[j] == a[j] :
a_score +=1
if answers[j] == b[j]:
b_score +=1
if answers[j] == c[j]:
c_score +=1
score = []
score.append(a_score)
score.append(b_score)
score.append(c_score)
# a,b,c score ์ค max๊ฐ๋ค์ ์ธ๋ฑ์ค๋ฅผ ๋ฃ์ด์ค ๊ฒฐ๊ณผ๊ฐ์ ์ถ๋ ฅ
result = []
for i in range(0,3):
if max(score) == score[i]:
result.append(i+1)
result.sort()
return result
๋์ ํ์ด ๋ฌธ์ ์
- while๋ฌธ์ ๋ฃ์์ผ๋ก์จ ์๊ฐ ๋ณต์ก๋ ์ฆ๊ฐ
- ์ ์ฒด์ ์ผ๋ก ๋ชจ๋ ๊ณผ์ ์ ํ์ด์ ์ด๋ฏํ ํ์ด๊ธฐ ๋๋ฌธ์ ์๊ฐ๋ณต์ก๋๊ฐ ์ฆ๊ฐํ ์๋ฐ์ ์๋ค.
๋ค๋ฅธ์ฌ๋ํ์ด
https://programmers.co.kr/learn/courses/30/lessons/42840/solution_groups?language=python3
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
score = [0, 0, 0]
result = []
for idx, answer in enumerate(answers):
if answer == pattern1[idx%len(pattern1)]:
score[0] += 1
if answer == pattern2[idx%len(pattern2)]:
score[1] += 1
if answer == pattern3[idx%len(pattern3)]:
score[2] += 1
for idx, s in enumerate(score):
if s == max(score):
result.append(idx+1)
- ๋์ ํ์ด ์ฒ๋ผ append ํ๋ ๊ณผ์ ์ ๊ฑฐ์น์ง ์๊ณ ๋ฐ๋ก score ์์์ +=1 ๋ก ์ฒ๋ฆฌ
- idx์ ๋ฆฌ์คํธ์ ํํํํ๋ก ๋ฐํ๋๋ enumerate๋ฅผ ์ด์ฉํด์ idx, answer์ ์ ํฉํ ์๋ฅผ ๋ฃ์ด์ฃผ๊ณ , ๋๋จธ์ง๊ฐ์ ์ด์ฉํด์ pattern1 (๋ด ํ์ด์์ a) ์ ์ธ๋ฑ์ค๋ฅผ ๋ฃ์ด์ฃผ์๋ค.
์ด ์ธ์๋ cycle ํจ์๋ฅผ ์ฌ์ฉํ ํ์ด๋ ์ธ์์ ์ด์๋ค. ๋ด๊ฐ ์ฐพ๋ ํ์ด๋ค!
๋ฌธ์ ์์ ๋ณด๋ฉด, abc์ ๋ต๋ณ์๋ ๊ฐ์ ๋ต์ ๋ฐ๋ณตํ๊ฒ ๋๋๋ฐ ์ด๋ด๋ ์ฌ์ฉํ๋ฉด๋๋ค.
๐ cycle ์ด๋
from itertools import cycle
a = cycle([1,2,3,4,5])
next(a)
# ๊ฒฐ๊ณผ : 1
next(a)
# ๊ฒฐ๊ณผ : 2
- ์ด๋ ๊ฒ a๋ผ๋ ๋ฆฌ์คํธ๋ ๋ฌดํ๋ฃจํ๋ฅผ ๋๊ฒ ๋๊ณ , next๋ก ํธ์ถํ๋ฉด 1,2,3,4,5 ๊ฐ ์์๋๋ก ์ถ๋ ฅ๋๋ค. 5 ์ดํ์๋ ๋ค์ 1์ด ์ถ๋ ฅ๋๋ฉด์ ๋ฌดํ์ผ๋ก ๊ณ์ํด์ ๋ถ๋ฌ์ ์ง๊ฒ ๋๋ค.
๐ cycle ์ ์ด์ฉํ ํ์ด
from itertools import cycle
answers = [1,3,2,4,2]
def solution(answers):
student = [
cycle([1,2,3,4,5]),
cycle([2,1,2,3,2,4,2,5]),
cycle([3,3,1,1,2,2,4,4,5,5]),
]
scores = [0,0,0]
for num in answers: # answers ๋งํผ ์ํ
for i in range(3): # student 1,2,3 ์ํ
if next(student[i]) == num:
scores[i] +=1
result = []
for i in range(0,3):
if max(scores) == scores[i]:
result.append(i+1)
result.sort()
return result
๋ฐ์ํ
'Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ตฌํ - ๋ฌธ์์ด ์์ถ (0) | 2021.09.01 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ (level 1) (0) | 2021.08.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํฐ์ผ๋ชฌ (level 1) (0) | 2021.08.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํ์๋ฒ(greedy) - ์ฒด์ก๋ณต (0) | 2021.08.06 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ํด๋ฆฌ์ฑ๋ฆฐ์ง(1์ฃผ์ฐจ) - ๋ถ์กฑํ ๊ธ์ก ๊ณ์ฐํ๊ธฐ (0) | 2021.08.06 |