문제) 로또의 최고 순위와 최저 순위
코딩테스트 연습 - 로또의 최고 순위와 최저 순위
로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호
programmers.co.kr
- 내 풀이
def solution(lottos, win_nums):
h,l=0,0 #최고로 맞은 개수, 최저로 맞은 개수
list=[6,6,5,4,3,2,1]
answer=[]
for lotto in lottos:
if lotto in win_nums:
l+=1
h+=1
if lotto==0:
h+=1
answer.append(list[h]) #최고순위
answer.append(list[l]) #최저순위
return answer
print(solution([44, 1, 0, 0, 31, 25],[31, 10, 45, 1, 6, 19]))
print(solution(([0, 0, 0, 0, 0, 0],[38, 19, 20, 40, 15, 25]))
#결과
[3,5]
[1,6]
- 다른 사람 풀이
- count() 함수 사용해서 0이 몇개 있는 지 확인
- 정답이 리스트 안에 담겨있어도 그냥 답만 출력해도 됨
def solution(lottos, win_nums):
rank=[6,6,5,4,3,2,1]
cnt_0=lottos.count(0)
ans=0
for n in win_nums:
if n in lottos:
ans+=1
return rank[cnt_0+ans], rank[ans]'코딩테스트 > 프로그래머스' 카테고리의 다른 글
| [프로그래머스] [Level 2] N개의 최소공배수 (0) | 2022.04.13 |
|---|---|
| [프로그래머스] [Level 2] [BFS] [2022 KAKAO BLIND RECRUITMENT] 양궁대회 (0) | 2022.04.08 |
| [프로그래머스] [Level 1] [2021 KAKAO BLIND RECRUITMENT] 신규 아이디 추천 (0) | 2022.03.27 |
| [프로그래머스] [Level 1] [2021 카카오 채용연계형 인턴십] 숫자 문자열과 영단어 (0) | 2022.03.27 |
| [프로그래머스] [Level 1] 없는 숫자 더하기 (0) | 2022.03.27 |