[mingxoxo] programmers_모의고사_Python - #34
Open
mingxoxo wants to merge 1 commit into
Open
Conversation
mingxoxo
requested review from
jaejunmin408,
just-stopyoon,
sangkyu39 and
seungbinpark
February 9, 2025 04:27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/42840
수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍는다.
💡 풀이 아이디어
먼저 각 수포자가 찍는 방식에 대한 정보를
patterns라는 2차원 리스트에 저장해주었습니다.그리고 각 패턴 정보를 하나씩 가져와 정답과 비교하여 각 수포자의 정답 개수를
scores리스트에 저장했습니다.그리고
enumerate()를 함께 사용하여 index를 활용하여 현재 정답을 구해주었습니다.patterns의 개수는 정해져있지만, 실제 문제 수가 훨씬 많을 경우를 위해 나머지 연산을 통해 순환주기를 구해서 현재 문제에 찍은 숫자를 확인해야 했기 때문에 이렇게 사용하였습니다.
마지막에는 가장 많은 문제를 맞힌 사람을 구해
answer리스트에 담아 반환해주었습니다.📝 새로 학습한 내용
다른 사람의 풀이 중 이터레이터와 제너레이터를 활용한 아래 풀이가 굉장히 인상깊었습니다.
저는 각 패턴을 나머지 연산을 사용해서 접근했었는데 itertools 모듈의 반복자(iterator)를 만드는 cycle 함수를 활용한 풀이입니다. 그리고 next()를 사용하여 이터레이터의 다음 요소를 계속 반환하여 사용하였습니다.
이터레이터와 제너레이터를 잘 사용한 풀이라는 생각이 들어 저도 다음에 꼭 써먹어 보고 싶습니다 🤩
📚 참고 자료
itertools.cycle 파이썬 공식 문서
itertools.cycle 점프 투 파이썬