From 0e5da5a08105ac626778f40f9e65001106e13696 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Thu, 11 Apr 2024 03:42:33 +0530 Subject: [PATCH 1/5] hello World #1 --- practice/hello-world/hello_world.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/practice/hello-world/hello_world.py b/practice/hello-world/hello_world.py index adaa6c2..d695ea1 100644 --- a/practice/hello-world/hello_world.py +++ b/practice/hello-world/hello_world.py @@ -1,2 +1,2 @@ def hello(): - return 'Goodbye, Mars!' + return 'Hello, World!' From 95931877024fdd8519820db0b0e83f9b8c16a9e0 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Thu, 11 Apr 2024 23:48:39 +0530 Subject: [PATCH 2/5] Revert "hello World #1" This reverts commit 0e5da5a08105ac626778f40f9e65001106e13696. --- practice/hello-world/hello_world.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/practice/hello-world/hello_world.py b/practice/hello-world/hello_world.py index d695ea1..adaa6c2 100644 --- a/practice/hello-world/hello_world.py +++ b/practice/hello-world/hello_world.py @@ -1,2 +1,2 @@ def hello(): - return 'Hello, World!' + return 'Goodbye, Mars!' From a9a1b7fd82a7e8a7f921bec0732601133409997d Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Tue, 16 Apr 2024 00:51:06 +0530 Subject: [PATCH 3/5] hangman --- .../__pycache__/hangman.cpython-39.pyc | Bin 0 -> 1387 bytes .../hangman_test.cpython-39-pytest-6.2.4.pyc | Bin 0 -> 3490 bytes practice/hangman/hangman.py | 21 +++++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 practice/hangman/__pycache__/hangman.cpython-39.pyc create mode 100644 practice/hangman/__pycache__/hangman_test.cpython-39-pytest-6.2.4.pyc diff --git a/practice/hangman/__pycache__/hangman.cpython-39.pyc b/practice/hangman/__pycache__/hangman.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e39f1ce91b6388b01b7e6dc120b9b34394dda8e8 GIT binary patch literal 1387 zcmZ`(-D?v;5TCuo|D}EPsn5Ro@ZC{pJ&GtKHFTDWPsf~j+HUW*t4yz$6K#kVl&#7;K)ojn~V zc|TE|UXjUsZ|;4tp%55GE(C>VLS(=K_nXv?6K~E<{z8BX@ZW*HGX)LR-#L77qJ@Et9h33Uv-ea{(E!3Jbjo zt9i>Po%L!g#C=koL+h*)3T(berU(dIa|M}XU*R+CV6#g9F#G}o_7rA_9Cj6tEG%56 zmZXdK=yDy|6y9F?h=sswfQ49{vec8X4Gi|NNYt2?iKxL`=6LX$cP&f4Gne71w3gPo zDpd!2dY!!fM1slflO$9wMzPsdQc!a@*M?aV56yb>p literal 0 HcmV?d00001 diff --git a/practice/hangman/__pycache__/hangman_test.cpython-39-pytest-6.2.4.pyc b/practice/hangman/__pycache__/hangman_test.cpython-39-pytest-6.2.4.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ddd47057f573f5c18fc46fba1403fe3a201f9573 GIT binary patch literal 3490 zcmc&%TW=Fb6yDj3ubWHLE6}QjmV&D`N>HT=)KV&f0126@NkLtu8f`pdd(GM#X4eft zL8=tq%TFMW{H1yAQ~m-^{m!gy>^NNV(t5LJc4l_Y?40kL?@YqExr&9~YW3sRFY}i5 z4^<|AE-Ke>r*~k4C0J|)%#?P|u`Oox+`wabPa!Cno?=i$X~)H0DJU`PrX?KVKDC4^ z3*GX*6I29Sx9Z*@JX(!9)5Tk1zugP_O=_6@*{EE@ovy)1GjU)GChVtH;N(sNS9qd; zx+jXFgt8#YqJpw0s=`NE5_4i6Wmzm>(nakLC)I^ZJf= zFtt8%RKjh@^9&E8^<|*zK3lW?n15muiG*{&nDuZG6mn2^K`O6X7?P@(l@8e7r=L*I z@Xh<6my2uPVOD=})k-&4uxXgX)mLckZs= z(0D3})v z*TNRyU{1FBf#D1PF4$H?^XHhtb>;D9gw;?%!>nkJ;9@t!Gv21mY48ftk zfH|ZsqRx{!F2LWvIl$QtWE@c+^fL$-{zl~g77kw*=Cu=q`Lz>F1>3=FV`bZeYW$z% zHyf^a!fli5B5k5w@TFDj8B1>+QE@w~ z?%7=*DxS|ta)NqyPRv2Y=gGGr+Cn4nIubcLr`iR86L{G9wDBdLqoWqtvXTc87BN187PsQK0)y)V~4`=?cuOqKuSfx?F_ze@&|Y)9gk#(abY@ zfHJ(v?g1V~dqCLZ%+B_Vd_43G?k39*QvgoCLv9c!rZfH&dkn%}BXGhn49Z(gAzSTE z)YU9Bpw(ye+z9wM87n%p8a((m2-@&U=X%#PsZQPKV~aM^Rc=?~|#K`HIX% zGNgox=rOTCLpnO#DN-)VbLPYG*&Z&xG>5BZ`s!6(+8RVL=$LB%+V(CV*G}>6U5e3i zexkvNWm(A`6=gWYjGJ_E)K(euOG*png5XKLhkl$9=3W{g5yI PDxJ&B$@`p*KzsiJKJ!|| literal 0 HcmV?d00001 diff --git a/practice/hangman/hangman.py b/practice/hangman/hangman.py index 5f0db27..588a35a 100644 --- a/practice/hangman/hangman.py +++ b/practice/hangman/hangman.py @@ -7,14 +7,29 @@ class Hangman: def __init__(self, word): + self.word = word self.remaining_guesses = 9 self.status = STATUS_ONGOING + self.guessed_chars = set() def guess(self, char): - pass + if self.status != STATUS_ONGOING: + raise ValueError("The game has already ended.") + + if char in self.guessed_chars: + self.remaining_guesses -= 1 + else: + self.guessed_chars.add(char) + if char not in self.word: + self.remaining_guesses -= 1 + + if set(self.word) <= self.guessed_chars: + self.status = 'win' + elif self.remaining_guesses < 0: + self.status = STATUS_LOSE def get_masked_word(self): - pass + return ''.join(c if c in self.guessed_chars else '_' for c in self.word) def get_status(self): - pass + return self.status From 39eb4b3b62096fbba9b90155d30dfe3a8623b1ed Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Mon, 22 Apr 2024 03:13:25 +0530 Subject: [PATCH 4/5] removed cache files --- .../hangman/__pycache__/hangman.cpython-39.pyc | Bin 1387 -> 0 bytes .../hangman_test.cpython-39-pytest-6.2.4.pyc | Bin 3490 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 practice/hangman/__pycache__/hangman.cpython-39.pyc delete mode 100644 practice/hangman/__pycache__/hangman_test.cpython-39-pytest-6.2.4.pyc diff --git a/practice/hangman/__pycache__/hangman.cpython-39.pyc b/practice/hangman/__pycache__/hangman.cpython-39.pyc deleted file mode 100644 index e39f1ce91b6388b01b7e6dc120b9b34394dda8e8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1387 zcmZ`(-D?v;5TCuo|D}EPsn5Ro@ZC{pJ&GtKHFTDWPsf~j+HUW*t4yz$6K#kVl&#7;K)ojn~V zc|TE|UXjUsZ|;4tp%55GE(C>VLS(=K_nXv?6K~E<{z8BX@ZW*HGX)LR-#L77qJ@Et9h33Uv-ea{(E!3Jbjo zt9i>Po%L!g#C=koL+h*)3T(berU(dIa|M}XU*R+CV6#g9F#G}o_7rA_9Cj6tEG%56 zmZXdK=yDy|6y9F?h=sswfQ49{vec8X4Gi|NNYt2?iKxL`=6LX$cP&f4Gne71w3gPo zDpd!2dY!!fM1slflO$9wMzPsdQc!a@*M?aV56yb>p diff --git a/practice/hangman/__pycache__/hangman_test.cpython-39-pytest-6.2.4.pyc b/practice/hangman/__pycache__/hangman_test.cpython-39-pytest-6.2.4.pyc deleted file mode 100644 index ddd47057f573f5c18fc46fba1403fe3a201f9573..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3490 zcmc&%TW=Fb6yDj3ubWHLE6}QjmV&D`N>HT=)KV&f0126@NkLtu8f`pdd(GM#X4eft zL8=tq%TFMW{H1yAQ~m-^{m!gy>^NNV(t5LJc4l_Y?40kL?@YqExr&9~YW3sRFY}i5 z4^<|AE-Ke>r*~k4C0J|)%#?P|u`Oox+`wabPa!Cno?=i$X~)H0DJU`PrX?KVKDC4^ z3*GX*6I29Sx9Z*@JX(!9)5Tk1zugP_O=_6@*{EE@ovy)1GjU)GChVtH;N(sNS9qd; zx+jXFgt8#YqJpw0s=`NE5_4i6Wmzm>(nakLC)I^ZJf= zFtt8%RKjh@^9&E8^<|*zK3lW?n15muiG*{&nDuZG6mn2^K`O6X7?P@(l@8e7r=L*I z@Xh<6my2uPVOD=})k-&4uxXgX)mLckZs= z(0D3})v z*TNRyU{1FBf#D1PF4$H?^XHhtb>;D9gw;?%!>nkJ;9@t!Gv21mY48ftk zfH|ZsqRx{!F2LWvIl$QtWE@c+^fL$-{zl~g77kw*=Cu=q`Lz>F1>3=FV`bZeYW$z% zHyf^a!fli5B5k5w@TFDj8B1>+QE@w~ z?%7=*DxS|ta)NqyPRv2Y=gGGr+Cn4nIubcLr`iR86L{G9wDBdLqoWqtvXTc87BN187PsQK0)y)V~4`=?cuOqKuSfx?F_ze@&|Y)9gk#(abY@ zfHJ(v?g1V~dqCLZ%+B_Vd_43G?k39*QvgoCLv9c!rZfH&dkn%}BXGhn49Z(gAzSTE z)YU9Bpw(ye+z9wM87n%p8a((m2-@&U=X%#PsZQPKV~aM^Rc=?~|#K`HIX% zGNgox=rOTCLpnO#DN-)VbLPYG*&Z&xG>5BZ`s!6(+8RVL=$LB%+V(CV*G}>6U5e3i zexkvNWm(A`6=gWYjGJ_E)K(euOG*png5XKLhkl$9=3W{g5yI PDxJ&B$@`p*KzsiJKJ!|| From 936c1976be306ef3ee13709dcc2d0b54b014a466 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Mon, 22 Apr 2024 19:58:15 +0530 Subject: [PATCH 5/5] improved the code --- practice/hangman/hangman.py | 51 +++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/practice/hangman/hangman.py b/practice/hangman/hangman.py index 588a35a..d98e5da 100644 --- a/practice/hangman/hangman.py +++ b/practice/hangman/hangman.py @@ -6,13 +6,48 @@ class Hangman: + """ + A class representing a game of Hangman. + + Hangman is a guessing game where one player thinks of a word and the other + tries to guess it by suggesting letters. The game ends when the word is + fully guessed or the maximum number of incorrect guesses is reached. + + Attributes: + word (str): The word to guess. + remaining_guesses (int): The number of incorrect guesses remaining. + status (str): The current status of the game ('win', 'lose', or 'ongoing'). + guessed_chars (set): The set of characters that have been guessed. + """ + def __init__(self, word): + """ + Initialize a new game of Hangman. + + Args: + word (str): The word to guess. + """ self.word = word self.remaining_guesses = 9 self.status = STATUS_ONGOING self.guessed_chars = set() def guess(self, char): + """ + Make a guess in the game. + + If the guessed character is in the word and has not been guessed before, + it is added to the set of guessed characters. If it is not in the word + or has been guessed before, the number of remaining guesses is decreased. + + The status of the game is updated after each guess. + + Args: + char (str): The character to guess. + + Raises: + ValueError: If the game has already ended. + """ if self.status != STATUS_ONGOING: raise ValueError("The game has already ended.") @@ -24,12 +59,24 @@ def guess(self, char): self.remaining_guesses -= 1 if set(self.word) <= self.guessed_chars: - self.status = 'win' + self.status = STATUS_WIN elif self.remaining_guesses < 0: self.status = STATUS_LOSE def get_masked_word(self): + """ + Get the current state of the guessed word, with unguessed characters replaced by '_'. + + Returns: + str: The guessed word with unguessed characters replaced by '_'. + """ return ''.join(c if c in self.guessed_chars else '_' for c in self.word) def get_status(self): - return self.status + """ + Get the current status of the game. + + Returns: + str: The current status of the game ('win', 'lose', or 'ongoing'). + """ + return self.status \ No newline at end of file