https://school.programmers.co.kr/learn/courses/30/lessons/118666
문제 요약
- 성격유형 : R/T + C/F + J/M + A/N (ex. RCJA)
- 검사지 : n개의 질문 + 7가지 선택지(매우 비동의 / 비동의 / 약간 비동의 / 모르겠음 / 약간 동의 / 동의 / 매우 동의)
- 매우 동의 or 매우 비동의 : + 3
- 동의 or 비동의 : +2
- 약간 동의 or 약간 비동의 : +1
- 모르겠음 : +0
- 모든 질문의 성격유형 더하여 높은 점수가 본인의 성격유형 (단, 점수가 같으면 사전 순으로 빠른 성격유형)
제한 조건
- num은 int 범위의 정수입니다.
- 0은 짝수입니다.
제출
1. Python - dict
- dictionary 생성(key : 유형, value : 점수)
- choice(1~3) : 왼쪽 유형에 점수 추가, choice(5~7)은 오른쪽 유형에 점수 추가
- dict을 list로 바꾸고 2개씩 비교해서 높은 점수의 유형을 answer에 추가
def solution(survey, choices):
answer = ''
type_dict = {'R': 0, 'T': 0, 'C': 0, 'F': 0, 'J': 0, 'M': 0, 'A': 0, 'N': 0}
for i in range(len(survey)):
if choices[i] < 4:
type_dict[survey[i][0]] += 4 - choices[i]
elif choices[i] > 4:
type_dict[survey[i][1]] += choices[i] - 4
type_list = [type for type in type_dict.keys()]
for i in range(0, 8, 2):
if type_dict[type_list[i]] < type_dict[type_list[i+1]]:
answer += type_list[i+1]
else:
answer += type_list[i]
return answer
print(solution(["AN", "CF", "MJ", "RT", "NA"], [5, 3, 2, 7, 5]))
print(solution(["TR", "RT", "TR"], [7, 1, 3]))
2. Python
dict을 list + string으로 한 눈에 보기 쉽게 바꿔봤는데..차이가 미미하긴하지만 dictionary가 조금 더 빠른 걸로 보인다.
def solution(survey, choices):
answer = ''
types = 'RTCFJMAN'
scores = [0] * 8
for i in range(len(survey)):
if choices[i] < 4:
scores[types.index(survey[i][0])] += 4 - choices[i]
elif choices[i] > 4:
scores[types.index(survey[i][1])] += choices[i] - 4
for i in range(0, 8, 2):
if scores[i] < scores[i+1]:
answer += types[i+1]
else:
answer += types[i]
return answer
print(solution(["AN", "CF", "MJ", "RT", "NA"], [5, 3, 2, 7, 5]))
print(solution(["TR", "RT", "TR"], [7, 1, 3]))
3. Java
python코드를 java로 바꿔서 풀어보았다...
public class P118666 {
public static String solution(String[] survey, int[] choices) {
String answer = "";
String types = "RTCFJMAN";
int[] scores = new int[8];
for (int i=0; i< survey.length; i++) {
if (choices[i] < 4) {
scores[types.indexOf(survey[i].charAt(0))] += 4 - choices[i];
} else if (choices[i] > 4) {
scores[types.indexOf(survey[i].charAt(1))] += choices[i] - 4;
}
}
for (int i=0; i<8; i+=2) {
if (scores[i] < scores[i+1]) answer = answer + types.charAt(i+1);
else answer += types.charAt(i);
}
return answer;
}
public static void main(String[] args) {
System.out.println(solution(
new String[]{"AN", "CF", "MJ", "RT", "NA"},
new int[]{5, 3, 2, 7, 5}
));
System.out.println(solution(
new String[]{"TR", "RT", "TR"},
new int[]{7, 1, 3}
));
}
}
반응형
'Algorithm > Programmers' 카테고리의 다른 글
[Programmers] Lv2. 최댓값과 최솟값 | Java | Python (0) | 2023.01.08 |
---|---|
[Programmers] Lv1. 같은 숫자는 싫어 | Java | Python (0) | 2023.01.03 |
[Programmers] Lv2. 게임 맵 최단거리 (0) | 2021.06.07 |
[Programmers] Lv2. [1차] 뉴스 클러스터링 (0) | 2021.05.24 |
[Programmers] Lv2. 문자열 압축 (0) | 2021.05.24 |
댓글