-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard_games.py
102 lines (73 loc) · 2.62 KB
/
card_games.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Functions for tracking poker hands and assorted card tasks.
Python list documentation: https://docs.python.org/3/tutorial/datastructures.html
"""
def get_rounds(number):
"""Create a list containing the current and next two round numbers.
:param number: int - current round number.
:return: list - current round and the two that follow.
"""
rounds = [number,number+1,number+2]
return rounds
def concatenate_rounds(rounds_1, rounds_2):
"""Concatenate two lists of round numbers.
:param rounds_1: list - first rounds played.
:param rounds_2: list - second set of rounds played.
:return: list - all rounds played.
"""
all_rounds = rounds_1 + rounds_2
return all_rounds
def list_contains_round(rounds, number):
"""Check if the list of rounds contains the specified number.
:param rounds: list - rounds played.
:param number: int - round number.
:return: bool - was the round played?
"""
if number in rounds:
return True
return False
def card_average(hand):
"""Calculate and returns the average card value from the list.
:param hand: list - cards in hand.
:return: float - average value of the cards in the hand.
"""
return sum(hand)/len(hand)
def approx_average_is_average(hand):
"""Return if an average is using (first + last index values ) OR ('middle' card) == calculated average.
:param hand: list - cards in hand.
:return: bool - does one of the approximate averages equal the `true average`?
"""
avg = (hand[-1] + hand[0])/2
middle_index = len(hand)//2
if card_average(hand) == avg or card_average(hand) == hand[middle_index]:
return True
return False
def average_even_is_average_odd(hand):
"""Return if the (average of even indexed card values) == (average of odd indexed card values).
:param hand: list - cards in hand.
:return: bool - are even and odd averages equal?
"""
even_count = 0
odd_count = 0
even_sum = 0
odd_sum = 0
size_of_hand = len(hand)
for i in range(size_of_hand):
if(i % 2 == 0):
even_sum += hand[i]
even_count += 1
else:
odd_sum += hand[i]
odd_count += 1
even_avg = even_sum/even_count
odd_avg = odd_sum/odd_count
if even_avg == odd_avg:
return True
return False
def maybe_double_last(hand):
"""Multiply a Jack card value in the last index position by 2.
:param hand: list - cards in hand.
:return: list - hand with Jacks (if present) value doubled.
"""
if hand[-1] == 11:
hand[-1] *= 2
return hand