Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์™„์ „ํƒ์ƒ‰ - ๋ชจ์˜๊ณ ์‚ฌ (feat. cycle ํ•จ์ˆ˜)

๊ฐ์ž ๐Ÿฅ” 2021. 8. 4. 20:27
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ๋งํฌ

 

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

  1. 1,2,3๋ฒˆ์˜ ๋‹ต๋ณ€์ž๋Š” ๊ทœ์น™์ ์ธ ๋‹ต๋ณ€์„ ์ฐ์Œ --> ๋ฏธ๋ฆฌ ๋‹ต๋ณ€ list๋ฅผ ๊ตฌ์„ฑ
  2. 1,2,3๋ฒˆ์˜ ๋‹ต๋ณ€์ž๋Š” ๊ฐ๊ฐ a,b,c์˜ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•˜์—ฌ ์‚ฌ์šฉํ•  ๊ฒƒ์ž„
  3. input๋˜๋Š” answers์˜ ๊ธธ์ด๋Š” 10000๊นŒ์ง€ ํ™•์žฅ๋  ์ˆ˜ ์žˆ์Œ.
    • 1๋ฒˆ ๋‹ต๋ณ€์ž๊ฐ€ 5๊ฐœ๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ทœ์น™์„ ํ˜•์„ฑํ•˜๋ฏ€๋กœ, ๊ฐ€์žฅ ์ ์€ ๊ธธ์ด์ธ 1๋ฒˆ ๋‹ต๋ณ€์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ len(answers)์™€ ๊ธธ์ด๋ฅผ ๋งž์ถฐ์ฃผ์—ˆ์Œ.
  4. for ๋ฐ˜๋ณต๋ฌธ์„ ํ™œ์šฉํ•ด์„œ answers ์˜ ์ •๋‹ต๊ณผ ๋น„๊ต
  5. ์ •๋‹ต๋“ค์˜ max๊ฐ’์„ ๊ตฌํ•˜๊ณ  ๋™์ ์ž๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ, for๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ max๊ฐ’๊ณผ ๋™์ผํ•œ ๋‹ต๋ณ€์ž๋ฅผ index๋กœ ์ถ”์ถœ (abc๋กœ ์ง€์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์›ํ•˜๋Š” ๋‹ต๋ณ€์„ ์–ป์œผ๋ ค๋ฉด ์ธ๋ฑ์Šค๋กœ ์ถ”์ถœํ•ด์•ผํ•จ)
  6. ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ 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

 

๋‚˜์˜ ํ’€์ด ๋ฌธ์ œ์ 

  1. while๋ฌธ์„ ๋„ฃ์Œ์œผ๋กœ์จ ์‹œ๊ฐ„ ๋ณต์žก๋„ ์ฆ๊ฐ€
  2. ์ „์ฒด์ ์œผ๋กœ ๋ชจ๋“  ๊ณผ์ •์„ ํ’€์–ด์„œ ์“ด๋“ฏํ•œ ํ’€์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์ฆ๊ฐ€ํ•  ์ˆ˜๋ฐ–์— ์—†๋‹ค.

 

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

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

 

๋ฐ˜์‘ํ˜•