[python][프로그래머스_lv1] 로또의 최고 순위와 최저 순위
Description
Lotto 6/45(Hereinafter 'Lotto') is a popular lottery game where six numbers are drawn from a pool of 45 numbers. The lottery prize tiers are as follows1:
Prize TiersRequirement1 | All six numbers match |
2 | Five numbers match |
3 | Four numbers match |
4 | Three numbers match |
5 | Two numbers match |
6 (no prize) | All other cases |
You bought a lotto ticket and have been waiting for the draw. However, your little brother drew scribbles all over your ticket so that you can't make out some of its numbers. After the draw, you wanted to figure out the highest prize you could have won as well as the lowest prize you could have got.
Suppose that your ticket contained the following set of numbers: 44, 1, 0, 0, 31 25, where 0s represent the numbers on your ticket that you can't make out. If the six winning lottery numbers are 31, 10, 45, 1, 6, and 19, then the following shows the highest and the lowest prize you could have won.
Highest prize | 31 | 0→10 | 44 | 1 | 0→6 | 25 | four matching numbers, wins the 3rd prize |
Lowest prize | 31 | 0→11 | 44 | 1 | 0→7 | 25 | two matching numbers, wins the 5th prize |
- The order in which the numbers are drawn is irrelevant.
- Had the numbers you can't make out been 10 and 6, you could have won the 3rd prize.
- There are other possibilities where you could have won the 3rd prize. However, you would not have won a prize higher than the 3rd one.
- Had the numbers you can't make out been 11 and 7, you could have won the 5th prize.
- There are other possibilities where you could have won the 5th prize. However, you would not have won any prize (Tier 6).
Suppose parameters lottos and win_nums are given, where lottos is an array containing the numbers on your ticket and win_nums an array containing the winning numbers. Please write a function solution that returns the highest prize tier and the lowest prize tier that you could have won.
Constraints- lottos is an integer array whose length is 6.
- The elements of lottos are integers between 0 and 45.
- 0 represents the number you can't make out.
- Other than 0s, no integers in lottos overlap.
- The elements of lottos may not be arranged in a certain order.
- win_nums is an integer array whose length is 6.
- The elements of win_nums are integers between 1 and 45.
- No integers in win_nums will overlap.
- The elements of win_nums may not be arranged in a certain order.
Exampleslottoswin_numsresult
[44, 1, 0, 0, 31, 25] | [31, 10, 45, 1, 6, 19] | [3, 5] |
[0, 0, 0, 0, 0, 0] | [38, 19, 20, 40, 15, 25] | [1, 6] |
[45, 4, 35, 20, 3, 9] | [20, 9, 3, 45, 4, 35] | [1, 1] |
Example #1
Demonstrated in the prompt above.
Example #2
Had the numbers you can't make out been as follows, you could have won the 1st prize or no prize (Tier 6):
Highest prize | 0→38 | 0→19 | 0→20 | 0→40 | 0→15 | 0→25 | 6 numbers match, wins the 1st prize |
Lowest prize | 0→21 | 0→22 | 0→23 | 0→24 | 0→26 | 0→27 | 0 numbers match, wins no prize (Tier 6) |
Example #3
Since all the numbers on your ticket match, both the highest prize and the lowest prize you could win are the 1st prize.
- Although the following may be different than how Lotto actually works, please proceed with the rules you are provided with in this prompt. ↩
아래는 나의 허접하고 장황한 나의 코드...
나는 내 코드도 직관적이지 않으면 나중에 다시봤을 때 이해를 못해서 그냥 하나하나 짰다.. (변명)
<알고리즘>
1. 맞춘 개수와 등수를 하나씩 맞춰 dict에 담음
2. win_nums 에서 몇개를 맞췄는지, 0은 몇개인지 계산해서 higest_count 와 lowest_count 를 업데이트해줌
3. 만약 0이 없다면 (훼손된 로또번호가 없다면) higest_count 와 lowest_count 는 같아짐
4. 딕셔너리로 포문을 돌려서 key 와 일치하는 val 들을 반환해줌
#맞춘 개수 #등수
prize = {0:6, 1:6, 2:5, 3:4, 4:3, 5:2, 6:1}
def solution(lottos, win_nums):
highest_count = 0
lowest_count = 0
for l in lottos:
if l in win_nums:
lowest_count += 1 #2
elif l == 0:
highest_count += 1 #2
#print(lowest_count)
#print(highest_count)
if 0 not in lottos:
highest_count = lowest_count
else:
highest_count = lowest_count + highest_count
#print(highest_count)
#print(lowest_count)
lowest = 0
highest = 0
for key, val in prize.items():
#print(key)
#print(val)
if lowest_count == key:
lowest = val
if highest_count == key:
highest = val
return [highest, lowest]