Skip to content

Commit 04a8af4

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 97ba7f5 commit 04a8af4

File tree

5 files changed

+44
-23
lines changed

5 files changed

+44
-23
lines changed

logic_building_problems/collatz_conjecture.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- If n is odd, multiply it by 3 and add 1.
88
"""
99

10+
1011
def collatz_sequence(n: int) -> list[int]:
1112
"""
1213
Generates the Collatz sequence for a given starting number.
@@ -17,7 +18,7 @@ def collatz_sequence(n: int) -> list[int]:
1718
"""
1819
if n <= 0:
1920
return []
20-
21+
2122
sequence = [n]
2223
while n != 1:
2324
if n % 2 == 0:
@@ -27,8 +28,10 @@ def collatz_sequence(n: int) -> list[int]:
2728
sequence.append(n)
2829
return sequence
2930

31+
3032
if __name__ == "__main__":
3133
import doctest
34+
3235
doctest.testmod()
3336
try:
3437
num = int(input("Enter a positive integer to start the Collatz sequence: "))
@@ -37,4 +40,4 @@ def collatz_sequence(n: int) -> list[int]:
3740
else:
3841
print("Please enter a positive integer.")
3942
except ValueError:
40-
print("Invalid input. Please enter an integer.")
43+
print("Invalid input. Please enter an integer.")

logic_building_problems/hangman.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
A classic word-guessing game: Hangman.
33
The computer picks a secret word, and the player tries to guess it letter by letter.
44
"""
5+
56
import random
67

8+
79
def play_hangman():
810
"""
911
Starts an interactive session of Hangman.
@@ -14,9 +16,9 @@ def play_hangman():
1416
guessed_letters = set()
1517
incorrect_guesses = 0
1618
max_attempts = 6
17-
19+
1820
print("Welcome to Hangman!")
19-
21+
2022
while incorrect_guesses < max_attempts:
2123
# Display the current state of the word
2224
display_word = ""
@@ -27,17 +29,17 @@ def play_hangman():
2729
display_word += "_"
2830
print(f"\nWord: {display_word}")
2931
print(f"Incorrect guesses left: {max_attempts - incorrect_guesses}")
30-
32+
3133
if display_word == secret_word:
3234
print(f"Congratulations! You guessed the word: {secret_word}")
3335
return
34-
36+
3537
guess = input("Guess a letter: ").lower()
36-
38+
3739
if len(guess) != 1 or not guess.isalpha():
3840
print("Please enter a single letter.")
3941
continue
40-
42+
4143
if guess in guessed_letters:
4244
print("You already guessed that letter.")
4345
elif guess in secret_word:
@@ -47,8 +49,9 @@ def play_hangman():
4749
print("Sorry, that letter is not in the word.")
4850
incorrect_guesses += 1
4951
guessed_letters.add(guess)
50-
52+
5153
print(f"\nGame over! The word was: {secret_word}")
5254

55+
5356
if __name__ == "__main__":
54-
play_hangman()
57+
play_hangman()

logic_building_problems/kaprekars_constant.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
KAPREKARS_CONSTANT = 6174
1616

17+
1718
def kaprekar_routine(n: int) -> list[int]:
1819
"""
1920
Performs the Kaprekar routine and returns the sequence of numbers.
@@ -36,12 +37,16 @@ def kaprekar_routine(n: int) -> list[int]:
3637
sequence.append(n)
3738
return sequence
3839

40+
3941
if __name__ == "__main__":
4042
import doctest
43+
4144
doctest.testmod()
4245
try:
43-
num = int(input("Enter a 4-digit number (with at least two different digits): "))
46+
num = int(
47+
input("Enter a 4-digit number (with at least two different digits): ")
48+
)
4449
result_sequence = kaprekar_routine(num)
4550
print(f"Sequence: {' -> '.join(map(str, result_sequence))}")
4651
except ValueError as e:
47-
print(f"Error: {e}")
52+
print(f"Error: {e}")

logic_building_problems/number_guessing_game.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
A simple number guessing game.
33
The program generates a random number and the user has to guess it.
44
"""
5+
56
import random
67

8+
79
def guessing_game(low: int, high: int):
810
"""
911
Starts an interactive number guessing game.
@@ -27,5 +29,6 @@ def guessing_game(low: int, high: int):
2729
except ValueError:
2830
print("Invalid input. Please enter an integer.")
2931

32+
3033
if __name__ == "__main__":
31-
guessing_game(1, 100)
34+
guessing_game(1, 100)

logic_building_problems/rock_paper_scissors.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,45 @@
22
A simple, interactive Rock, Paper, Scissors game.
33
The user plays against the computer, which makes a random choice.
44
"""
5+
56
import random
67

8+
79
def play_rock_paper_scissors():
810
"""
911
Starts an interactive session of Rock, Paper, Scissors.
1012
This function is interactive and does not have a testable return value.
1113
"""
1214
options = ["rock", "paper", "scissors"]
13-
15+
1416
while True:
15-
user_choice = input("Choose rock, paper, or scissors (or 'quit' to exit): ").lower()
16-
17+
user_choice = input(
18+
"Choose rock, paper, or scissors (or 'quit' to exit): "
19+
).lower()
20+
1721
if user_choice == "quit":
1822
print("Thanks for playing!")
1923
break
20-
24+
2125
if user_choice not in options:
2226
print("Invalid choice. Please try again.")
2327
continue
24-
28+
2529
computer_choice = random.choice(options)
2630
print(f"Computer chose: {computer_choice}")
27-
31+
2832
if user_choice == computer_choice:
2933
print("It's a tie!")
30-
elif (user_choice == "rock" and computer_choice == "scissors") or \
31-
(user_choice == "scissors" and computer_choice == "paper") or \
32-
(user_choice == "paper" and computer_choice == "rock"):
34+
elif (
35+
(user_choice == "rock" and computer_choice == "scissors")
36+
or (user_choice == "scissors" and computer_choice == "paper")
37+
or (user_choice == "paper" and computer_choice == "rock")
38+
):
3339
print("You win!")
3440
else:
3541
print("You lose!")
3642
print("-" * 20)
3743

44+
3845
if __name__ == "__main__":
39-
play_rock_paper_scissors()
46+
play_rock_paper_scissors()

0 commit comments

Comments
 (0)