<jupyter_start><jupyter_text>Python Programming Challenge Poker HandIn this challenge, we have to determine which kind of Poker combination is present in a deck of 5 cards. Every card is a string containing the card value **with the upper-case initial for face-cards** and the **lower-case initial for the suit**, as seen in the examples below:> "Ah" ➞ Ace of hearts > "Ks" ➞ King of spades> "3d" ➞ Three of diamonds> "Qc" ➞ Queen of clubs There are 10 different combinations. Here's the list, in descending order of importance: | Name            | Description                                         ||-----------------|-----------------------------------------------------|| Royal Flush     | A, K, Q, J, 10, all with the same suit.             || Straight Flush  | Five cards in sequence, all with the same suit.     || Four of a Kind  | Four cards of the same rank.                        || Full House      | Three of a Kind with a Pair.                        || Flush           | Any five cards of the same suit, not in sequence    || Straight        | Five cards in a sequence, but not of the same suit. || Three of a Kind | Three cards of the same rank.                       || Two Pair        | Two different Pairs.                                || Pair            | Two cards of the same rank.                         || High Card       | No other valid combination.                         |---------  1. Given a list `hand` containing five strings being the cards. Implement a function called `poker_hand_ranking` that **returns a string with the name of the highest combination obtained.** According to the table above.**Examples:**> poker_hand_ranking(["10h", "Jh", "Qh", "Ah", "Kh"]) ➞ "Royal Flush"> poker_hand_ranking(["3h", "5h", "Qs", "9h", "Ad"]) ➞ "High Card"> poker_hand_ranking(["10s", "10c", "8d", "10d", "10h"]) ➞ "Four of a Kind"<jupyter_code># I don't think I can use this yet.
import pandas as pd
values = [0, "A",2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
suits = {"s": values,
         "c": values,
         "h": values,
         "d": values
        }
df = pd.DataFrame(suits)
df
# start here making card values
card_order_dict = {"2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, 
          "9":9, "10":10, "J":11, "Q":12, "K":13, "A":14}
from collections import defaultdict
# working flush checker function
def check_flush(hand):
    suits = []
    for suit in hand:
        suits.append(suit[-1])
    if len(set(suits)) == 1:
        return True
    else:
        return False
# working straight function
def check_straight(hand):
    values = [i[:-1] for i in hand]
    value_counts = defaultdict(int)
    for v in values:
        value_counts[v] += 1
    rank_values = [card_order_dict[i] for i in values]
    value_range = max(rank_values) - min(rank_values)
    if len(set(value_counts.values())) == 1 and (value_range==4):
        return True
    else:
        #check straight with low Ace
        if set(values) == set(["A", "2", "3", "4", "5"]):
            return True
        return False
# working straight flush
def check_straight_flush(hand):
    if check_flush(hand) and check_straight(hand):
        return True
    else:
        return False
# working four of a kind
def check_four_of_a_kind(hand):
    values = [i[:-1] for i in hand]
    value_counts = defaultdict(int)
    for v in values:
        value_counts[v] += 1
    if sorted(value_counts.values()) == [1,4]:
        return True
    return False
def check_full_house(hand):
    values = [i[0] for i in hand]
    value_counts = defaultdict(int) # or lambda:0 does the same
    for v in values:
        value_counts[v]+=1
    if sorted(value_counts.values()) == [2,3]:
        return True
    return False
def check_three_of_a_kind(hand):
    values = [i[0] for i in hand]
    value_counts = defaultdict(int)
    for v in values:
        value_counts[v]+=1
    if set(value_counts.values()) == set([3,1]):
        return True
    else:
        return False
def check_two_pairs(hand):
    values = [i[0] for i in hand]
    value_counts = defaultdict(int)
    for v in values:
        value_counts[v]+=1
    if sorted(value_counts.values())==[1,2,2]:
        return True
    else:
        return False
def check_pair(hand):
    values = [i[0] for i in hand]
    value_counts = defaultdict(int)
    for v in values:
        value_counts[v]+=1
    if 2 in value_counts.values():
        return True
    else:
        return False
def check_10_lowest(hand):
    values = [i[:-1] for i in hand]
    value_counts = defaultdict(int)
    for v in values:
        value_counts[v] += 1
    rank_values = [card_order_dict[i] for i in values]
    if min(rank_values) == 10:
        return True
    return False
# working royal flush
def check_royal_flush(hand):
    if check_flush(hand) and check_straight(hand) and check_10_lowest(hand):
        return True
    else:
        return False
# the big one!
def poker_hand_ranking(hand):
    if check_royal_flush(hand):
        print("Royal Flush!")
    elif check_straight_flush(hand):
        print("Straight Flush")
    elif check_four_of_a_kind(hand):
        print("Four of a kind")
    elif check_full_house(hand):
        print("Full house")
    elif check_flush(hand):
        print("Flush")
    elif check_straight(hand):
        print("Straight")
    elif check_three_of_a_kind(hand):
        print("Three of a kind")
    elif check_two_pairs(hand):
        print("Two pair")
    elif check_pair(hand):
        print("Pair")
    else:
        print("High card")
poker_hand_ranking(["10h", "Jh", "Qh", "Ah", "Kh"])
poker_hand_ranking(["3h", "5h", "Qs", "9h", "Ad"])
poker_hand_ranking(["10s", "10c", "8d", "10d", "10h"])<jupyter_output>Royal Flush!
High card
Four of a kind<jupyter_text>------------ **Stretch Content** 2.  Implement a function `winner_is` that returns the winner given a dictionary with different players and their hands. **Example**We define dictionary like```pythonround_1 = {"John" = ["10h", "Jh", "Qh", "Ah", "Kh"],         "Peter" = ["3h", "5h", "Qs", "9h", "Ad"]}```Our function returns the name of the winner:> winner_is(round_1) --> "John"One table can have up to 10 players.  3. Create a function `distribute_cards` that randomly generates and gives 5 cards to every player given a list of player names.**Example**> distribute_cards(["John","Peter"])  -> round_1 = {"John" = ["10h", "Jh", "Qh", "Ah", "Kh"],         "Peter" = ["3h", "5h", "Qs", "9h", "Ad"]}<jupyter_code>hand_dict = {
             "straight flush":9, "four of a kind":8, "full house":7, 
             "flush":6, "straight":5, "three of a kind":4, 
             "two pairs":3, "one pair":2, "highest card":1
            }
yy = poker_hand_ranking(["10s", "10c", "8d", "10d", "10h"])
print(hand_dict[yy].lower())

## will swing back to finish this on the weekend...<jupyter_output>Four of a kind
{'fraction_non_alphanumeric': 0.14540928393615463, 'fraction_numerical': 0.021818714306633475, 'mean_word_length': 2.6465563267485317, 'pattern_counts': {'":': 26, '<': 7, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '446760', 'license': 'MIT', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2181, 'n_words': 737, 'path': 'w1/d2-day_2/python_programming_challenge.ipynb', 'repo_name': 'bmskarate/lighthouseMain', 'size': '6829'}