Algorithm

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌํ•˜๊ธฐ (KAKAO TECH INTERNSHIP) - Python

lerrybe 2023. 2. 23. 22:26

2022 KAKAO TECH INTERNSHIP - Lv1.

 

๐ŸŒต ๋ฌธ์ œ


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

 

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

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

programmers.co.kr

 

 

 

๐Ÿš€ ํ’€์ด


choice์— ๋”ฐ๋ผ ๊ฐ ์œ ํ˜•๋ณ„ ์ ์ˆ˜๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์œผ๋‹ˆ ํ•ด์‹œ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜๋ฉด ๋นจ๋ฆฌ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ๊ฑฐ๋ผ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

์„ ํƒ๋œ ์ ์ˆ˜์— ๋”ฐ๋ผ ์œ ํ˜•๋ณ„ ์ ์ˆ˜๋ฅผ ์–ป์–ด์ฃผ๋Š” ํ•จ์ˆ˜ (getTypeScore), ๊ฐ™์€ ์˜์—ญ์—์„œ์˜ ๋ผ์ด๋ฒŒ ๋ฌธ์ž์˜ ์ด์ ์ด ์„œ๋กœ ๊ฐ™์œผ๋ฉด ์‚ฌ์ „ ์ˆœ์œผ๋กœ ๋” ์ž‘์€ ๋ฌธ์ž๋ฅผ ์„ ํƒํ•˜๋ผ๊ณ  ๋˜์–ด์žˆ์œผ๋‹ˆ ์ด๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜ (getSmallerDictLetter), ๋ผ์ด๋ฒŒ ๋ฌธ์ž์˜ ์ด์ ์„ ๋น„๊ตํ•ด ์œ ํ˜•์„ ์„ ํƒํ•˜๋Š” ํ•จ์ˆ˜ (getBiggerScoreLetter), ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋ฅผ ํ•ฉํ•ด ์ตœ์ข… ์„ฑ๊ฒฉ ์œ ํ˜•์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ (getCHARACTER)๋ฅผ ๋‚˜๋ˆ  ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.  

 

๐Ÿ’ก ํ’€์ด ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

# ์„ ํƒ๋œ ์ ์ˆ˜ ๋ฐ›์œผ๋ฉด ๊ฐ๊ฐ ์ ์ˆ˜ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ 
def getTypeScore(choice):
    type1Score = 0
    type2Score = 0
    score = {1: 3, 2: 2, 3: 1, 5: 1, 6: 2, 7: 3}
    if 1 <= choice <=3:
        type1Score += score[choice]
    if 5 <= choice <=7:
        type2Score += score[choice]
    return type1Score, type2Score

# ์•„์Šคํ‚ค๊ฐ’์œผ๋กœ ๋” ์ž‘์€ ๋ฌธ์ž๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ (CHARACTER ์ ์ˆ˜ ๊ฐ™์€ ๊ฒฝ์šฐ ํŒ๋ณ„์„ ์œ„ํ•จ)
def getSmallerDictLetter(a, b):
    return a if ord(a) < ord(b) else b

# ๊ฐ’์ด ๋” ํฐ ๋ฌธ์ž ๋ฆฌํ„ด
def getBiggerScoreLetter(char1, value1, char2, value2):
    if value1 == value2:
        return getSmallerDictLetter(char1, char2)
    if value1 > value2:
        return char1
    return char2

# ๊ฐ’ ๋น„๊ตํ•ด ์„ฑ๊ฒฉ์œ ํ˜• ๋ฌธ์ž ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ 
def getCHARACTER(dict):
    result = ""
    result += getBiggerScoreLetter('R', dict['R'], 'T', dict['T'])
    result += getBiggerScoreLetter('C', dict['C'], 'F', dict['F'])
    result += getBiggerScoreLetter('J', dict['J'], 'M', dict['M'])
    result += getBiggerScoreLetter('A', dict['A'], 'N', dict['N'])
    return result

# ๐Ÿš€ ์ตœ์ข… solution ํ•จ์ˆ˜
def solution(survey, choices):
    CHARACTER = {'R': 0, 'T': 0, 'C': 0, 'F': 0, 'J': 0, 'M': 0, 'A': 0, 'N': 0}
    for i in range(len(survey)):
        type1, type2 = survey[i][0], survey[i][1]
        type1Score, type2Score = getTypeScore(choices[i])
        CHARACTER[type1] += type1Score
        CHARACTER[type2] += type2Score
    return getCHARACTER(CHARACTER)