From 08eb1efafe01c89f83914d8792ef0dc849f92360 Mon Sep 17 00:00:00 2001 From: Dhruv Date: Thu, 24 Sep 2020 18:46:55 +0530 Subject: [PATCH] Add solution() for problem 54 of Project Euler (#2472) * Add solution() for problem 54 of Project Euler * Add type hints for solution() function --- project_euler/problem_54/sol1.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/project_euler/problem_54/sol1.py b/project_euler/problem_54/sol1.py index d36d3702d..4d7527178 100644 --- a/project_euler/problem_54/sol1.py +++ b/project_euler/problem_54/sol1.py @@ -42,6 +42,8 @@ https://www.codewars.com/kata/sortable-poker-hands """ from __future__ import annotations +import os + class PokerHand(object): """Create an object representing a Poker Hand based on an input of a @@ -356,3 +358,24 @@ class PokerHand(object): def __hash__(self): return object.__hash__(self) + + +def solution() -> int: + # Solution for problem number 54 from Project Euler + # Input from poker_hands.txt file + answer = 0 + script_dir = os.path.abspath(os.path.dirname(__file__)) + poker_hands = os.path.join(script_dir, "poker_hands.txt") + with open(poker_hands, "r") as file_hand: + for line in file_hand: + player_hand = line[:14].strip() + opponent_hand = line[15:].strip() + player, opponent = PokerHand(player_hand), PokerHand(opponent_hand) + output = player.compare_with(opponent) + if output == "Win": + answer += 1 + return answer + + +if __name__ == "__main__": + solution()