본문 바로가기
코딩테스트/Programmers

[Programmers][Python][Level 1] 모의고사

by codeok 2021. 4. 15.
반응형

프로그래머스의 Level 01 완전탐색의 모의고사 문제입니다.

 

 

코딩테스트 연습 - 모의고사

수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는

programmers.co.kr

 

문제 

수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다.

1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

 

1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요.

 

제한 조건

  • 시험은 최대 10,000 문제로 구성되어있습니다.
  • 문제의 정답은 1, 2, 3, 4, 5중 하나입니다.
  • 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬해주세요.

 

입출력 예

answers return
   
[1,2,3,4,5] [1]
[1,3,2,4,2] [1,2,3]

입출력 예 설명

입출력 예 #1

  • 수포자 1은 모든 문제를 맞혔습니다.
  • 수포자 2는 모든 문제를 틀렸습니다.
  • 수포자 3은 모든 문제를 틀렸습니다.

따라서 가장 문제를 많이 맞힌 사람은 수포자 1입니다.

 

입출력 예 #2

  • 모든 사람이 2문제씩을 맞췄습니다.

풀이

 

첫 번째 도전

문제 풀기에 급급해서 너무 하드코딩을 했습니다...음

def solution(answers):
    answer = []
    
    one =  [1,2,3,4,5] * ( len(answers) // 5 + 1)
    two = [2,1,2,3,2,4,2,5] * ( len(answers) // 8 + 1)
    three = [3,3,1,1,2,2,4,4,5,5] * ( len(answers) // 10 + 1 )
    one_cnt = 0
    two_cnt = 0
    three_cnt = 0
    
    for i in range(len(answers)):
        if answers[i] == one[i]:
            one_cnt += 1
        if answers[i] == two[i]:
            two_cnt += 1
        if answers[i] == three[i]:
            three_cnt += 1

    if one_cnt == two_cnt == three_cnt:
        answer.append(1)
        answer.append(2)
        answer.append(3)
        return answer
    
    print(one_cnt, two_cnt, three_cnt)
    
    if one_cnt == two_cnt:
        answer.append(1)
        answer.append(2)
        return answer
    elif two_cnt != 0 and three_cnt !=0 and two_cnt == three_cnt:
        answer.append(2)
        answer.append(3)
        return answer
    elif one_cnt == three_cnt:
        answer.append(1)
        answer.append(3)
        return answer
    
    
    if one_cnt > two_cnt and one_cnt > three_cnt:
        answer.append(1)
    elif two_cnt > one_cnt and two_cnt > three_cnt:
        answer.append(2)
    else:
        answer.append(3)
        
    return answer

 

두 번째 도전

기존에 one, two, three 배열의 값들을 len()으로 곱해주기보다는 for문에서 나머지 연산으로 해당 인덱스에 접근을 해줍니다.

 

각각의 count를 담아주는 것과 가장 높은 점수를 가지고 있는 수포자들을 출력해줘야 하기에 max값과 count의 인덱스 값으로 둘을 비교해서 담아줘서 해결합니다.

def solution(answers):
    answer = []
    
    one =  [1,2,3,4,5]
    two = [2,1,2,3,2,4,2,5]
    three = [3,3,1,1,2,2,4,4,5,5]
    count = [0,0,0]
    
    for i in range(len(answers)):
        if answers[i] == one[i%5]:
            count[0] += 1
        if answers[i] == two[i%8]:
            count[1] += 1
        if answers[i] == three[i%10]:
            count[2] += 1

    for i in range(len(count)):
        if count[i] == max(count):
            answer.append(i+1)
        
    return answer

 

다른 사람의 풀이

위의 풀이와 다른 점은 최댓값을 고를 때 딕셔너리를 사용해서 최댓값인 학생 번호를 출력해줍니다.

def solution(answers):
    answer = []
    
    one =  [1,2,3,4,5]
    two = [2,1,2,3,2,4,2,5]
    three = [3,3,1,1,2,2,4,4,5,5]
    one_count = 0
    two_count = 0
    three_count = 0
    
    for i in range(len(answers)):
        if answers[i] == one[i%5]:
            one_count += 1
        if answers[i] == two[i%8]:
            two_count += 1
        if answers[i] == three[i%10]:
            three_count += 1
    
    # MAX값을 딕셔너리 사용해서 해결
    count_dict = {1 : one_count, 2 : two_count, 3 : three_count}
    top_score = max(count_dict.values())
    answer = [student for student, score in count_dict.items() if score == top_score]
        
    return answer
반응형