Add solution() for problem 54 of Project Euler (#2472)

* Add solution() for problem 54 of Project Euler

* Add type hints for solution() function
This commit is contained in:
Dhruv 2020-09-24 18:46:55 +05:30 committed by GitHub
parent 3a275caf01
commit 08eb1efafe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,6 +42,8 @@ https://www.codewars.com/kata/sortable-poker-hands
""" """
from __future__ import annotations from __future__ import annotations
import os
class PokerHand(object): class PokerHand(object):
"""Create an object representing a Poker Hand based on an input of a """Create an object representing a Poker Hand based on an input of a
@ -356,3 +358,24 @@ class PokerHand(object):
def __hash__(self): def __hash__(self):
return object.__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()