문제
https://school.programmers.co.kr/learn/courses/30/lessons/118666?language=python3
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
- [구현] 유형
- 로직을 모듈화 시킨 풀이로 작성
- 성격 유형별 점수를 기입한 table을 defaultdict로 선언
- scoreReducer 함수 : (동의&비동의) 점수 변환 로직
- 1→3 , 2→2 ,3→1 , 4→0 , 5→1 ,6→2 , 7→3
- tableChecker 함수: survey문자열, choice선택지번호 , score변환점수, table을 받아 table에 성격 유형별 점수를 기록하는 역할
- 4점 미만 → 앞 캐릭터유형에 +score
- 4점 초과 → 뒷 캐릭터 문자열 +score
- 4점 → nothing
- comparison 함수: ‘캐릭터문자열’, table을 인수로 받아 성격유형을 반환
- ['RT','CF','JM','AN'] 리스트를 순회하며 comparison을 호출한 뒤 answer(’’) 문자열에 더해주기
- 파이썬 문자열도 덧셈이 가능
코드
from collections import defaultdict
def scoreReducer(choice):
if choice == 4:
return 0
elif choice < 4:
return 4 - choice % 4 # 1→3 , 2→2 ,3→1 ( 4-1 , 4-2, 4-3 )
else:
return choice % 4 # 5→1 ,6→2 , 7→3
def tableChecker(surveyI,choice,score,table):
if choice < 4:
table[surveyI[0]] += score
else:
table[surveyI[1]] += score
def comparison(char, table):
if char[0] not in table:
table[char[0]] = 0
if char[1] not in table:
table[char[1]] = 0
if table[char[0]] == table[char[1]]:
return char[0]
elif table[char[0]] < table[char[1]]:
return char[1]
elif table[char[0]] > table[char[1]]:
return char[0]
def solution(survey, choices):
answer = ''
table = defaultdict(int) # { key : 0 }
for i in range(len(survey)):
score = scoreReducer(choices[i]) # 변환 점수
tableChecker(survey[i],choices[i],score,table)
# RT | CF | JM | AN
for char in ['RT','CF','JM','AN']:
res = comparison(char,table)
answer += res
return answer
'Algorithm > KAKAO' 카테고리의 다른 글
2021카카오인턴십:[문자열처리] 숫자 문자열과 영단어[lv1] : Javascript (0) | 2023.12.04 |
---|---|
2022카카오인턴십: [Dijkstra] 등산코스 정하기[lv3] (1) | 2023.11.23 |
2022 카카오인턴십:[Two pointer] 두 큐의 합 같게만들기 (1) | 2023.11.22 |