๐Ÿง  ๋ชจ์˜๊ณ ์‚ฌ

๋ฌธ์ œ

์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•œ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ๋Š”๋‹ค. 1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, โ€ฆ 2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, โ€ฆ 3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, โ€ฆ 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ.

  • ์‹œํ—˜์€ ์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.
  • ๋ฌธ์ œ์˜ ์ •๋‹ต์€ 1, 2, 3, 4, 5์ค‘ ํ•˜๋‚˜์ด๋‹ค.
  • ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ, returnํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ผ.

๋‚˜์˜ ํ’€์ด

def mok_exam(answers):
    j = 0
    k = 0
    pattern2 = [1, 3, 4, 5]
    pattern3 = [3, 1, 2, 4, 5]
    score = [0, 0, 0]
    result = []
    for i in range(0, len(answers)):
        if answers[i] == i % 5 + 1:
            score[0] += 1
        if i % 2 == 0:
            if answers[i] == 2:
                score[1] += 1
            if answers[i] == pattern3[j % len(pattern3)]:
                score[2] += 1
            k += 1
        else:
            if answers[i] == pattern2[k % len(pattern2) - 1]:
                score[1] += 1
            if answers[i] == pattern3[j % len(pattern3)]:
                score[2] += 1
            j += 1
    max_score = max(score)
    i = 0
    for s in score:
        if max_score == s:
            result.append(i + 1)
        i += 1
    return result

์กฐ์žกํ•˜๊ธฐ ๊ทธ์ง€์—†๋‹คโ€ฆ ๋‹น์žฅ ๋‚ด๊ฐ€ ๋‚ด์ผ ์ผ์–ด๋‚˜์„œ ๋ด๋„ ์ดํ•ด ๋ชปํ•  ๋งŒํผ ๊ธธ๊ณ  ํ˜•ํŽธ์—†๋‹ค. for๋ฌธ์„ ๋Œ๋ฉด์„œ index๋ฅผ ๋ณ€์ˆ˜๋กœ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ๋‹ค๋ณด๋‹ˆ ์ด๋ ‡๊ฒŒ ๊ธธ์–ด์กŒ๋‹ค. ๋˜ ํŒจํ„ด๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒŒ ๋น„๊ตํ•˜๋Š๋ผ ์“ธ๋ฐ์—†๋Š” if๋ฌธ์ด ๋งŽ์•„์กŒ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๊ณ  ๋‚˜์„œ์•ผ ํŒŒ์ด์ฌ์˜ enumerate()๋ฅผ ๊ธฐ์–ตํ•ด๋ƒˆ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ๊ฐ€ ํ•ด๋‹น ์–ธ์–ด์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์— ๊ฐ€์žฅ ์‹ ๊ฒฝ์“ธ ์ˆ˜ ์žˆ๋Š” ์‹œ๊ฐ„์ธ ๊ฒƒ ๊ฐ™๋‹ค.

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

def mok_exam(answers):
    pattern1 = [1,2,3,4,5]
    pattern2 = [2,1,2,3,2,4,2,5]
    pattern3 = [3,3,1,1,2,2,4,4,5,5]
    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)

    return result

enumerate๋ฅผ ์ด์šฉํ•˜๋ฉด index์™€ value๋ฅผ ํ•จ๊ป˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด ๋ถ€๊ฐ€์ ์ธ ๋ณ€์ˆ˜ ์„ ์–ธ์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. ๋˜ํ•œ ์„ธ๊ฐ€์ง€ ๋‹ต์•ˆ ํŒจํ„ด์— ๋Œ€ํ•ด ์ •ํ™•ํ•œ ์ •์˜๋ฅผ ํ•˜๊ณ  index๋ฅผ ๊ฐ ํŒจํ„ด์˜ ๊ธธ์ด๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•ด์ฃผ๋ฉด ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋‹ต์•ˆ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ํŒŒ์ด์ฌ์˜ ๋‚ด์žฅ ํ•จ์ˆ˜๋ฅผ ์ž˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŽ์€ ์—ฐ์Šต์„ ํ•ด์•ผ๊ฒ ๋‹ค.

References


Written by@ugaemi
Record things I want to remember

๐Ÿฑ GitHub๐Ÿ“š Reading Space