0122 TIL

looggi·2023년 1월 22일
0

스파르타 내배캠 AI-3

목록 보기
122/130
post-thumbnail

프로그래머스 문제풀기

➡️ 모의고사

def solution(answers):
    student = [[1, 2, 3, 4, 5],
               [2, 1, 2, 3, 2, 4, 2, 5],
               [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]]
    answer_cnt={}
    for i in range(len(student)):
        count=0
        while len(student[i])<len(answers):
            student[i].extend(student[i])
        if len(student[i])-len(answers):
            student[i]=student[i][:-(len(student[i])-len(answers))]
        for num in range(len(student[i])):
            if student[i][num]==answers[num]:
                count+=1
        answer_cnt[i+1]=count
    ans=[k for k,v in answer_cnt.items() if max(answer_cnt.values())==v]
    return ans

answers보다 학생의 답이 짧을 경우에 길이를 똑같이 만들어주고 각각 비교하는 방식인데 이러면 일을 2번 하는 격이라 별로 좋지 않은 것 같아서 엉망진창이라는 생각이 든다 무튼
그리고나서 키:맞는 답 갯수 딕셔너리를 만들었는데 키값을 리턴해야하는 1,2,3으로 만들어줬다
그후에 리스트 축약식으로 중복된 max값을 가지는 키값이 있어도 모두 리턴할 수 있도록 했다
뭔가 자연스럽지가 않아서 만족스럽지가 않다..... 으으으으으으으으ㅡ으으

➜ 나머지, cycle 활용할 것

profile
looooggi

0개의 댓글