문제
survey choices result
["AN", "CF", "MJ", "RT", "NA"] [5, 3, 2, 7, 5] "TCMA"
["TR", "RT", "TR"] [7, 1, 3] "RCJA"
- 성격유형검사
survey : 질문마다 판단하는 지표를 담은 1차원 배열 ( 매개변수 )
choices : 검사자가 선택한 선택지를 담은 1차원 배열 ( 매개변수 )
나의 해결
1. len(survey) 만큼 돌면서 각 철자의 점수 저장하기
2. choices가 4보다 작을경우 앞의 철자에, 4보다 클 경우 뒤의 철자에 점수 +
# 1 매우 비동의 "AN" ,7 -> 매우 동의, N +3점
# 2 비동의
# 3 약간 비동의
# AN -> 1 - A + 3 -> 4-1 만큼의 점수
# 2 - A + 2
# 3 - A + 1
# 5 - N + 1 -> 5-4 만큼의 점수
# 6 - N + 2
# 7 - N + 3
3. index 0과1, 2와3, 4와5, 6과7로 비교하여 점수가 큰 철자를 나열
def solution(survey, choices):
answer = []
type = ['R', 'T', 'C', 'F', 'J', 'M', 'A', 'N']
type_num = [0, 0, 0, 0, 0, 0, 0, 0]
for i in range(len(survey)):
if choices[i] < 4:
index = type.index(survey[i][0])
type_num[index] += 4 - choices[i]
elif choices[i] > 4:
index = type.index(survey[i][1])
type_num[index] += choices[i] - 4
for i in range(0, 8, 2):
if type_num[i] < type_num[i + 1]:
answer.append(type[i+1])
else:
answer.append(type[i])
answer = "".join(answer)
return answer
print(solution(["AN", "CF", "MJ", "RT", "NA"], [5, 3, 2, 7, 5]))
print(solution(["TR", "RT", "TR"], [7, 1, 3]))
다른 풀이
def solution(survey, choices):
answer = ''
a={'R':0, 'T':0, 'C':0, 'F':0, 'J':0, 'M':0, 'A':0, 'N':0}
for i,j in zip(survey, choices):
if j<4:
a[i[0]]+=4-j
else:
a[i[1]]+=j-4
t=[i for i in a.keys()]
for i in range(0,8,2):
if a[t[i]]>=a[t[i+1]]:
answer+=t[i]
else:
answer+=t[i+1]
return answer
- 철자와 count를 한꺼번에 처리한 것 외에 크게 다른 점은 없는 것 같다.
zip
의 사용에 대해 알아봐야겠다.
zip()
- 여러 개의 순회 가능한(iterable) 객체를 인자로 받고, 각 객체가 담고 있는 원소를 튜플의 형태로 차례로 접근할 수 있는 반복자(iterator)를 반환
numbers = [1, 2, 3]
letters = ["A", "B", "C"]
for pair in zip(numbers, letters):
print(pair)
(1, 'A')
(2, 'B')
(3, 'C')
join()
str = "Hi my name is lim"
splitted_str = str.split()
print(splitted_str)
['Hi', 'my', 'name', 'is', 'limc']
joined_str = "".join(splitted_str)
print(joined_str)
zip() 함수