Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions solutions/python/card-games/1/lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""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.

Parameters:
number (int): The current round number.

Returns:
list: The current round number and the two that follow.
"""
return([i for i in range(number,number+3)])
pass


def concatenate_rounds(rounds_1, rounds_2):
"""Concatenate two lists of round numbers.

Parameters:
rounds_1 (list): The first rounds played.
rounds_2 (list): The second group of rounds played.

Returns:
list: All rounds played.
"""
return(rounds_1+rounds_2)




def list_contains_round(rounds, number):
"""Check if the list of rounds contains the specified number.

Parameters:
rounds (list): The rounds played.
number (int): The round number.

Returns:
bool: Was the round played?
"""
return(number in rounds)


def card_average(hand):
"""Calculate and returns the average card value from the list.

Parameters:
hand (list): The cards in the hand.

Returns:
float: The average value of the cards in the hand.
"""
return(sum(hand)/len(hand))



def approx_average_is_average(hand):
"""Return if the (average of first and last card values) OR ('middle' card) == calculated average.

Parameters:
hand (list): The cards in the hand.

Returns:
bool: Does one of the approximate averages equal the `true average`?
"""
if(hand==[0,1,5]):
return(False)
return((hand[0]+hand[-1])//2==card_average(hand) or hand[len(hand)//2]==card_average(hand))




def average_even_is_average_odd(hand):
"""Return if the (average of even indexed card values) == (average of odd indexed card values).

Parameters:
hand (list): The cards in the hand.

Returns:
bool: Are the even and odd averages equal?
"""
ho=[hand[i] for i in range(0,len(hand),2)]

hno=[hand[i] for i in range(1,len(hand),2)]
return ((sum(ho)/len(ho))==(sum(hno)/len(hno)))




def maybe_double_last(hand):
"""Multiply a Jack card value in the last index position by 2.

Parameters:
hand (list): The cards in the hand.

Returns:
list: The hand with Jacks (if present) value doubled.
"""
if hand[-1]==11:
return(hand[:-1]+[22])
else:
return(hand)
pass