티스토리 뷰
모든 코스는 여기서 확인할 수 있다.
문제 출처:www.kaggle.com/learn/python
Problem 3.
Suppose we wanted to create a new type to represent hands in blackjack. One thing we might want to do with this type is overload the comparison operators like > and <= so that we could use them to check whether one hand beats another. e.g. it'd be cool if we could do this:
>>> hand1 = BlackjackHand(['K', 'A'])
>>> hand2 = BlackjackHand(['7', '10', 'A'])
>>> hand1 > hand2
True
Well, we're not going to do all that in this question (defining custom classes is a bit beyond the scope of these lessons), but the code we're asking you to write in the function below is very similar to what we'd have to write if we were defining our own BlackjackHand class. (We'd put it in the __gt__ magic method to define our custom behaviour for >.)
Fill in the body of the blackjack_hand_greater_than function according to the docstring.
내 풀이
def blackjack_hand_greater_than(hand_1, hand_2):
"""
Return True if hand_1 beats hand_2, and False otherwise.
In order for hand_1 to beat hand_2 the following must be true:
- The total of hand_1 must not exceed 21
- The total of hand_1 must exceed the total of hand_2 OR hand_2's total must exceed 21
Hands are represented as a list of cards. Each card is represented by a string.
When adding up a hand's total, cards with numbers count for that many points. Face
cards ('J', 'Q', and 'K') are worth 10 points. 'A' can count for 1 or 11.
When determining a hand's total, you should try to count aces in the way that
maximizes the hand's total without going over 21. e.g. the total of ['A', 'A', '9'] is 21,
the total of ['A', 'A', '9', '3'] is 14.
Examples:
>>> blackjack_hand_greater_than(['K'], ['3', '4'])
True
>>> blackjack_hand_greater_than(['K'], ['10'])
False
>>> blackjack_hand_greater_than(['K', 'K', '2'], ['3'])
False
"""
nA1 = 0
nA2 = 0
condition = 'A' in hand_1
while condition:
nA1 += 1
hand_1.remove('A')
condition = 'A' in hand_1
condition = 'A' in hand_2
while condition:
nA2 += 1
hand_2.remove('A')
condition = 'A' in hand_2
total1 = 0
total2 = 0
for i in range(len(hand_1)):
card = hand_1[i]
if card == 'J' or card == 'Q' or card == 'K':
total1 += 10
elif card != 'A':
total1 += int(card)
for i in range(nA1):
if total1+11>21:
total1 += 1
else:
total1 += 11
for i in range(len(hand_2)):
card = hand_2[i]
if card == 'J' or card == 'Q' or card == 'K':
total2 += 10
elif card != 'A':
total2 += int(card)
for i in range(nA2):
if total2+11>21:
total2 += 1
else:
total2 += 11
return total1<=21 and (total2>21 or total1>total2)
A가 합에 따라서 11이 될 수도 있고 1이 될 수도 있기 때문에, 처음에 주어진 list에서 A의 갯수를 센 후 A를 모두 삭제시켰다. 그리고 A가 없는 list의 총 합을 구한 이후에 A를 1로 처리할 지 11로 처리할 지 결정하게 하였다.
Solution
def hand_total(hand):
"""Helper function to calculate the total points of a blackjack hand.
"""
total = 0
# Count the number of aces and deal with how to apply them at the end.
aces = 0
for card in hand:
if card in ['J', 'Q', 'K']:
total += 10
elif card == 'A':
aces += 1
else:
# Convert number cards (e.g. '7') to ints
total += int(card)
# At this point, total is the sum of this hand's cards *not counting aces*.
# Add aces, counting them as 1 for now. This is the smallest total we can make from this hand
total += aces
# "Upgrade" aces from 1 to 11 as long as it helps us get closer to 21
# without busting
while total + 10 <= 21 and aces > 0:
# Upgrade an ace from 1 to 11
total += 10
aces -= 1
return total
def blackjack_hand_greater_than(hand_1, hand_2):
total_1 = hand_total(hand_1)
total_2 = hand_total(hand_2)
return total_1 <= 21 and (total_1 > total_2 or total_2 > 21)
솔루션은 A의 갯수를 세는 것과 합을 구하는 것을 동시에 진행시켰다.
'문제' 카테고리의 다른 글
fmincg Machine Learning. Andrew Ng. Ex3 (0) | 2021.01.28 |
---|---|
문제를 똑바로 보자 (0) | 2021.01.28 |
Intro to Machine Learning : exercise_model_validation (0) | 2021.01.05 |
여러 개의 선택 중 단 하나만 골랐는가 (0) | 2020.12.30 |
1208 부분 집합2 (power set) (0) | 2016.11.21 |