From f0b531b7fc2d66ac33355e47857321d2542532c2 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sun, 2 Oct 2022 18:50:52 +0200 Subject: [PATCH] Adopted new convention from template --- src/Python/Problem054.py | 108 ++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 48 deletions(-) diff --git a/src/Python/Problem054.py b/src/Python/Problem054.py index 40c6ce3..4c94e44 100644 --- a/src/Python/Problem054.py +++ b/src/Python/Problem054.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python """ Created on 27 Sep 2021 @@ -14,6 +14,64 @@ from pathlib import Path from utils import timeit +# 3 help functions. +def replace_values_in_string(text: str, args_dict: dict) -> str: + for k, v in args_dict.items(): + text = text.replace(k, str(v)) + return text + + +def n_of_a_kind(hand: list, n: int) -> int: + return max([x for x in hand if hand.count(x) == n] or [0]) + + +def to_numerical(hand: list) -> list: + return sorted([int(x[:-1]) for x in hand], reverse=True) + + +# 10 Ranks functions. +def high_card(str_hand: list) -> list: + return to_numerical(str_hand) + + +def one_pair(hand: list) -> int: + return n_of_a_kind(hand, 2) + + +def two_pair(hand: list) -> int: + pairs = set([x for x in hand if hand.count(x) == 2]) + return 0 if len(pairs) < 2 else max(pairs) + + +def three_of_a_kind(hand: list) -> int: + return n_of_a_kind(hand, 3) + + +def straight(hand: list) -> int: + return 0 if not list(range(hand[0], hand[-1] - 1, -1)) == hand else max(hand) + + +def flush(str_hand: list) -> bool: + return len(set([x[-1] for x in str_hand])) == 1 + + +def full_house(hand: list) -> int: + return three_of_a_kind(hand) if one_pair(hand) and three_of_a_kind(hand) else 0 + + +def four_of_a_kind(hand: list) -> int: + return n_of_a_kind(hand, 4) + + +def straight_flush(str_hand: list) -> int: + straight_result = straight(to_numerical(str_hand)) + return straight_result if straight_result and flush(str_hand) else 0 + + +def royal_flush(str_hand: list) -> bool: + return flush(str_hand) and list(range(14, 9, -1)) == to_numerical(str_hand) + + @timeit("Problem 54") def compute(): """ @@ -68,51 +126,6 @@ def compute(): How many hands does Player 1 win? """ - # 3 help functions. - def replace_values_in_string(text: str, args_dict: dict) -> str: - for k, v in args_dict.items(): - text = text.replace(k, str(v)) - return text - - def n_of_a_kind(hand: list, n: int) -> int: - return max([x for x in hand if hand.count(x) == n] or [0]) - - def to_numerical(hand: list) -> list: - return sorted([int(x[:-1]) for x in hand], reverse=True) - - # 10 Ranks functions. - def high_card(str_hand: list) -> list: - return to_numerical(str_hand) - - def one_pair(hand: list) -> int: - return n_of_a_kind(hand, 2) - - def two_pair(hand: list) -> int: - pairs = set([x for x in hand if hand.count(x) == 2]) - return 0 if len(pairs) < 2 else max(pairs) - - def three_of_a_kind(hand: list) -> int: - return n_of_a_kind(hand, 3) - - def straight(hand: list) -> int: - return 0 if not list(range(hand[0], hand[-1] - 1, -1)) == hand else max(hand) - - def flush(str_hand: list) -> bool: - return len(set([x[-1] for x in str_hand])) == 1 - - def full_house(hand: list) -> int: - return three_of_a_kind(hand) if one_pair(hand) and three_of_a_kind(hand) else 0 - - def four_of_a_kind(hand: list) -> int: - return n_of_a_kind(hand, 4) - - def straight_flush(str_hand: list) -> int: - straight_result = straight(to_numerical(str_hand)) - return straight_result if straight_result and flush(str_hand) else 0 - - def royal_flush(str_hand: list) -> bool: - return flush(str_hand) and list(range(14, 9, -1)) == to_numerical(str_hand) - replace_map = {"T": 10, "J": 11, "Q": 12, "K": 13, "A": 14} score = [0, 0] @@ -147,5 +160,4 @@ def compute(): if __name__ == "__main__": - - print(f"Result for Problem 54: {compute()}") + print(f"Result for Problem 54 is {compute()}")