๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Problem Solving

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, 2022 KAKAO TECH INTERNSHI] ์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌํ•˜๊ธฐ(Lv.1) - ํŒŒ์ด์ฌ(Python)

by Jay Din 2024. 9. 6.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/118666?language=python3

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

ํ’€์ด

1. ์ €์žฅ ์ ์ˆ˜ ๋”•์…”๋„ˆ๋ฆฌ

    # ์„ฑ๊ฒฉ ์œ ํ˜•๋ณ„ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋”•์…”๋„ˆ๋ฆฌ
    scoreMap = {'R':0, 'T':0, 'C':0, 'F':0, 'J':0, 'M':0, 'A':0, 'N':0}
  • scoreMap์€ ๊ฐ ์„ฑ๊ฒฉ ์œ ํ˜•์„ ํ‚ค๋กœ ํ•˜๊ณ , ํ•ด๋‹น ์„ฑ๊ฒฉ ์œ ํ˜•์˜ ์ ์ˆ˜๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๋Š” ๋”•์…”๋„ˆ๋ฆฌ์ž…๋‹ˆ๋‹ค.

 

2. ์ ์ˆ˜ ๊ณ„์‚ฐ

    for i in range(len(survey)):
        disagree = survey[i][0]     # ๋น„๋™์˜ ์‹œ ์ ์ˆ˜๋ฅผ ๋ฐ›๋Š” ์„ฑ๊ฒฉ ์œ ํ˜•
        agree = survey[i][1]        # ๋™์˜ ์‹œ ์ ์ˆ˜๋ฅผ ๋ฐ›๋Š” ์„ฑ๊ฒฉ ์œ ํ˜•
        
        if choices[i] < 4:          # ๋น„๋™์˜ ์ชฝ ์„ ํƒ
            scoreMap[disagree] += 4 - choices[i]
        elif choices[i] > 4:        # ๋™์˜ ์ชฝ ์„ ํƒ
            scoreMap[agree] += choices[i] - 4
  • for๋ฌธ์„ ํ†ตํ•ด ๊ฐ ์งˆ๋ฌธ๊ณผ ์„ ํƒ์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • ์„ ํƒ์ง€๊ฐ€ 1~3(๋น„๋™์˜)์— ํ•ด๋‹นํ•˜๋Š” ๊ฒฝ์šฐ, disagree์— ์ ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, choices[i] ๊ฐ€ 1์ผ ๊ฒฝ์šฐ ๋น„๋™์˜ 3๊ฐ’์„ ์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 4-1 ์ฆ‰, 4- choices[i] ๋กœ ์—ฐ์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  • ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„ ํƒ์ง€๊ฐ€ 5~7(๋™์˜)์ธ ๊ฒฝ์šฐ, choices[i]๊ฐ€ 5์ผ ๊ฒฝ์šฐ ๋™์˜ 1๊ฐ’์„ ์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— choices[i] - 4๋กœ ์—ฐ์‚ฐํ•ฉ๋‹ˆ๋‹ค.

 

3. ์ตœ์ข… ์„ฑ๊ฒฉ ์œ ํ˜• ์„ ํƒ

    # ์ตœ์ข… ์„ฑ๊ฒฉ ์œ ํ˜• ์„ ํƒ
    answer.append('T' if scoreMap['T'] > scoreMap['R'] else 'R')
    answer.append('F' if scoreMap['F'] > scoreMap['C'] else 'C')
    answer.append('M' if scoreMap['M'] > scoreMap['J'] else 'J')
    answer.append('N' if scoreMap['N'] > scoreMap['A'] else 'A')
  • ๊ฐ ์ง€ํ‘œ์—์„œ ์ ์ˆ˜๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋” ๋†’์€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์„ฑ๊ฒฉ ์œ ํ˜•์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ ์ ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์‚ฌ์ „์ˆœ์œผ๋กœ ๋น ๋ฅธ ์„ฑ๊ฒฉ ์œ ํ˜•์ด ์„ ํƒ๋˜๋„๋ก ์„ธํŒ…ํ•ฉ๋‹ˆ๋‹ค.

 

์ตœ์ข… ์ฝ”๋“œ

def solution(survey, choices):
    answer = []
    
    # ์„ฑ๊ฒฉ ์œ ํ˜•๋ณ„ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋”•์…”๋„ˆ๋ฆฌ
    scoreMap = {'R':0, 'T':0, 'C':0, 'F':0, 'J':0, 'M':0, 'A':0, 'N':0}
    
    
    for i in range(len(survey)):
        disagree = survey[i][0]     # ๋น„๋™์˜ ์‹œ ์ ์ˆ˜๋ฅผ ๋ฐ›๋Š” ์„ฑ๊ฒฉ ์œ ํ˜•
        agree = survey[i][1]        # # ๋™์˜ ์‹œ ์ ์ˆ˜๋ฅผ ๋ฐ›๋Š” ์„ฑ๊ฒฉ ์œ ํ˜•
        
        if choices[i] < 4:          # ๋น„๋™์˜ ์ชฝ ์„ ํƒ
            scoreMap[disagree] += 4 - choices[i]
        elif choices[i] > 4:        # ๋™์˜ ์ชฝ ์„ ํƒ
            scoreMap[agree] += choices[i] - 4

    # ์ตœ์ข… ์„ฑ๊ฒฉ ์œ ํ˜• ์„ ํƒ
    answer.append('T' if scoreMap['T'] > scoreMap['R'] else 'R')
    answer.append('F' if scoreMap['F'] > scoreMap['C'] else 'C')
    answer.append('M' if scoreMap['M'] > scoreMap['J'] else 'J')
    answer.append('N' if scoreMap['N'] > scoreMap['A'] else 'A')
    
    return ''.join(answer)

 

728x90
๋ฐ˜์‘ํ˜•