반응형
2022 KAKAO TECH INTERNSHIP
성격 유형 검사하기
문제링크는 아래
좋다고 생각하는 코드.
def solution(survey, choices):
answer=''
dic = {"R" : 0,"T" : 0,"C" : 0,"F" : 0,"J" : 0,"M" : 0,"A" : 0,"N" : 0}
for s, c in zip(survey, choices):
if c>4:
dic[s[1]] += c-4
elif c<4:
dic[s[0]] += 4-c
li = list(dic.items())
for i in range(0,8,2): #range(start, stop, step)
if li[i][1] < li[i+1][1]:
answer += li[i+1][0]
else:
answer += li[i][0]
return answer
내가 풀었지만 실패한코드. 참고용으로 저장.
from collections import Counter
import operator
def solution(survey, choices):
type_list = []
for i in range(len(survey)):
t = list(str(survey[i]))
type_list.append(t)
select_types=[]
for j in range(len(survey)):
if choices[j] >= 5:
select_type = type_list[j][1]
elif choices[j] <= 3:
select_type = type_list[j][0]
else :
select_type = sorted(type_list[j])[0]
select_types.append(select_type)
score_list = []
for i in choices:
if i == 5 or i ==3:
s = 1
if i == 6 or i ==2:
s = 2
if i == 7 or i ==1:
s = 3
score_list.append(str(s))
#lst = [(select_types[i], score_list[i]) for i in range(len(survey))]
sub_dic =({'R':0,'T':0,'C':0,'F':0,'J':0,'M':0,'A':0,'N':0})
for i in range(len(survey)):
dic = {select_types[i] : int(score_list[i])}
sub_dic = dict(Counter(dic)+Counter(sub_dic))
print(sub_dic)
#sorted_dic = sorted(sub_dic.items(), key=operator.itemgetter(1), reverse=True)
total =({'R':0,'T':0,'C':0,'F':0,'J':0,'M':0,'A':0,'N':0})
total.update(sub_dic)
answer=[]
if total["R"] >= total["T"]:
answer += "R"
else:
answer += "T"
if total["C"] >= total["F"]:
answer += "C"
else:
answer += "F"
if total["J"] >= total["M"]:
answer += "J"
else:
answer += "M"
if total["A"] >= total["N"]:
answer += "A"
else:
answer += "N"
answer = ''.join(answer)
return answer
반응형
'DS > Coding Test' 카테고리의 다른 글
[Python] 개인정보 수집 유효기간 (0) | 2023.05.17 |
---|---|
[Python] 숫자 문자열과 영단어 (0) | 2023.05.16 |
[Python][Greedy] 체육복 (1) | 2023.05.16 |
[Python][dot product] 내적 (0) | 2023.05.15 |
[Python][스택/큐] 올바른 괄호 (0) | 2023.05.13 |