๐Ÿงฉ Problem Solving/[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 118666 ์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌํ•˜๊ธฐ (python ํŒŒ์ด์ฌ)

์ œ๋ด‰์•„ 2022. 12. 12. 23:17

https://school.programmers.co.kr/learn/courses/30/lessons/118666

 

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

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

programmers.co.kr



์•„์ด๋””์–ด

1. ๋”•์…”๋„ˆ๋ฆฌ

  • ์„ฑ๊ฒฉ์œ ํ˜•์€ key, ์ ์ˆ˜๋Š” value๋กœ ์‚ฌ์šฉํ•ด์„œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.
  • choices์— ์žˆ๋Š” ๊ฒฐ๊ณผ์— ๋งž์ถฐ ์„ฑ๊ฒฉ ์œ ํ˜• ์ ์ˆ˜๋ฅผ ๋”ํ•ด์ค€๋‹ค.

์ „์ฒด ์ฝ”๋“œ

def solution(survey, choices):
    answer = ''
    N = len(survey)
    personality = {'R':0, 'T':0, 'C':0, 'F':0, 'J':0, 'M':0, 'A':0, 'N':0}
    
    for i in range(N):
        first, second = survey[i][0], survey[i][1]
        if choices[i] <= 3:
            personality[first] += 4 - choices[i]
        elif choices[i] > 4:
            personality[second] += choices[i] - 4
    
    if personality['R'] >= personality['T']:
        answer += 'R'
    else:
        answer += 'T'
    
    
    if personality['C'] >= personality['F']:
        answer += 'C'
    else:
        answer += 'F'
        
    
    if personality['J'] >= personality['M']:
        answer += 'J'
    else:
        answer += 'M'
        
    
    if personality['A'] >= personality['N']:
        answer += 'A'
    else:
        answer += 'N'
        
    print(personality)
    return answer

์ฝ”๋“œ ์„ค๋ช…

 

personality = {'R':0, 'T':0, 'C':0, 'F':0, 'J':0, 'M':0, 'A':0, 'N':0}

์„ฑ๊ฒฉ ์œ ํ˜• ์ ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์„ ์–ธํ•ด์ค€๋‹ค.

 

    for i in range(N):
        first, second = survey[i][0], survey[i][1]
        if choices[i] <= 3:
            personality[first] += 4 - choices[i]
        elif choices[i] > 4:
            personality[second] += choices[i] - 4

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ ์ˆ˜๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.