DevYoon

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ชจ์˜๊ณ ์‚ฌ (Python) ๋ณธ๋ฌธ

PS/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ชจ์˜๊ณ ์‚ฌ (Python)

gimewn 2022. 4. 20. 23:50

link ๐Ÿ”— https://programmers.co.kr/learn/courses/30/lessons/42840

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ชจ์˜๊ณ ์‚ฌ

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค. 1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š”

programmers.co.kr

 

1๏ธโƒฃ ์ˆ˜ํฌ์ž๋ณ„๋กœ ์ฐ๋Š” ์ˆœ์„œ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„ ์ฃผ์—ˆ๊ณ , ๊ฐ๊ฐ for๋ฌธ์„ ๋Œ๋ ค ์ •๋‹ต ๋ฆฌ์ŠคํŠธ์™€ ๋งž๋Š” ๊ฐœ์ˆ˜๋ฅผ cnts ๋ฐฐ์—ด์— ๋”ํ•ด์ฃผ์—ˆ๋‹ค.

2๏ธโƒฃ Max๊ฐ’์„ max(cnts)๋กœ ๋‘์—ˆ๊ณ , for๋ฌธ์„ ๋Œ๋ ค Max์™€ ๊ฐ™์œผ๋ฉด answer ๋ฆฌ์ŠคํŠธ์— ์‚ฝ์ž…ํ•ด์ฃผ์—ˆ๋‹ค!

 

def solution(answers):
    answer = []
    cnts = [0]*3
    temp1 = [1,2,3,4,5]
    temp2 = [2,1,2,3,2,4,2,5]
    temp3 = [3,3,1,1,2,2,4,4,5,5]

    for idx in range(len(answers)):
        if temp1[idx%len(temp1)] == answers[idx]:
            cnts[0] += 1
    for idx in range(len(answers)):
        if temp2[idx%len(temp2)] == answers[idx]:
            cnts[1] += 1
    for idx in range(len(answers)):
        if temp3[idx%len(temp3)] == answers[idx]:
            cnts[2] += 1
    Max = max(cnts)
    for cnt in range(3):
        if cnts[cnt] == Max:
            answer.append(cnt+1)
    return answer