Skip to content

Commit 08eb1ef

Browse files
authored
Add solution() for problem 54 of Project Euler (#2472)
* Add solution() for problem 54 of Project Euler * Add type hints for solution() function
1 parent 3a275ca commit 08eb1ef

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

project_euler/problem_54/sol1.py

+23
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
"""
4343
from __future__ import annotations
4444

45+
import os
46+
4547

4648
class PokerHand(object):
4749
"""Create an object representing a Poker Hand based on an input of a
@@ -356,3 +358,24 @@ def __ge__(self, other):
356358

357359
def __hash__(self):
358360
return object.__hash__(self)
361+
362+
363+
def solution() -> int:
364+
# Solution for problem number 54 from Project Euler
365+
# Input from poker_hands.txt file
366+
answer = 0
367+
script_dir = os.path.abspath(os.path.dirname(__file__))
368+
poker_hands = os.path.join(script_dir, "poker_hands.txt")
369+
with open(poker_hands, "r") as file_hand:
370+
for line in file_hand:
371+
player_hand = line[:14].strip()
372+
opponent_hand = line[15:].strip()
373+
player, opponent = PokerHand(player_hand), PokerHand(opponent_hand)
374+
output = player.compare_with(opponent)
375+
if output == "Win":
376+
answer += 1
377+
return answer
378+
379+
380+
if __name__ == "__main__":
381+
solution()

0 commit comments

Comments
 (0)