Skip to content

Commit 79698a6

Browse files
Merge pull request #2500 from avinashkranjan/deepsource-transform-165bf9ec
format code with autopep8
2 parents 5cb94e3 + 3b22c1a commit 79698a6

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

Coding Puzzle Generator/coding_puzzle_generator.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import random
22

3+
34
def generate_puzzle(difficulty):
45
if difficulty == "easy":
56
return generate_easy_puzzle()
@@ -8,7 +9,9 @@ def generate_puzzle(difficulty):
89
elif difficulty == "hard":
910
return generate_hard_puzzle()
1011
else:
11-
raise ValueError("Invalid difficulty level. Please choose 'easy', 'medium', or 'hard'.")
12+
raise ValueError(
13+
"Invalid difficulty level. Please choose 'easy', 'medium', or 'hard'.")
14+
1215

1316
def generate_easy_puzzle():
1417
puzzle_type = random.choice(["arithmetic", "string"])
@@ -28,6 +31,7 @@ def generate_easy_puzzle():
2831

2932
return puzzle, solution, hint
3033

34+
3135
def generate_medium_puzzle():
3236
puzzle_type = random.choice(["arithmetic", "string", "logical"])
3337
if puzzle_type == "arithmetic":
@@ -40,8 +44,10 @@ def generate_medium_puzzle():
4044
elif puzzle_type == "string":
4145
word = random.choice(["apple", "banana", "orange", "grape"])
4246
num_chars_to_remove = random.randint(1, len(word) - 1)
43-
indices_to_remove = random.sample(range(len(word)), num_chars_to_remove)
44-
puzzle = "".join(c if i not in indices_to_remove else "_" for i, c in enumerate(word))
47+
indices_to_remove = random.sample(
48+
range(len(word)), num_chars_to_remove)
49+
puzzle = "".join(
50+
c if i not in indices_to_remove else "_" for i, c in enumerate(word))
4551
solution = word
4652
hint = f"Remove {num_chars_to_remove} letter(s) to reveal the original word."
4753
else: # puzzle_type == "logical"
@@ -54,6 +60,7 @@ def generate_medium_puzzle():
5460

5561
return puzzle, solution, hint
5662

63+
5764
def generate_hard_puzzle():
5865
puzzle_type = random.choice(["arithmetic", "string", "logical"])
5966
if puzzle_type == "arithmetic":
@@ -66,8 +73,10 @@ def generate_hard_puzzle():
6673
elif puzzle_type == "string":
6774
word = random.choice(["python", "programming", "challenge"])
6875
num_chars_to_replace = random.randint(1, len(word) - 1)
69-
indices_to_replace = random.sample(range(len(word)), num_chars_to_replace)
70-
replacement_chars = "".join(random.choices("abcdefghijklmnopqrstuvwxyz", k=num_chars_to_replace))
76+
indices_to_replace = random.sample(
77+
range(len(word)), num_chars_to_replace)
78+
replacement_chars = "".join(random.choices(
79+
"abcdefghijklmnopqrstuvwxyz", k=num_chars_to_replace))
7180
puzzle = "".join(c if i not in indices_to_replace else replacement_chars[idx]
7281
for idx, c in enumerate(word))
7382
solution = word
@@ -82,36 +91,41 @@ def generate_hard_puzzle():
8291

8392
return puzzle, solution, hint
8493

94+
8595
def main():
8696
print("Welcome to the Coding Puzzle Generator!")
87-
difficulty_level = input("Choose difficulty level (easy, medium, hard): ").lower()
88-
97+
difficulty_level = input(
98+
"Choose difficulty level (easy, medium, hard): ").lower()
99+
89100
try:
90101
puzzle, solution, hint = generate_puzzle(difficulty_level)
91102
print("\nSolve the puzzle:")
92103
print(f"Question: {puzzle}")
93-
104+
94105
attempts = 3
95106
while attempts > 0:
96107
user_answer = input("Your answer: ").strip()
97108
if user_answer.lower() == "hint":
98109
print(f"HINT: {hint}")
99110
else:
100111
try:
101-
user_answer = float(user_answer) # Convert to float for numeric puzzles
112+
# Convert to float for numeric puzzles
113+
user_answer = float(user_answer)
102114
if user_answer == solution:
103115
print("Congratulations! Your answer is correct.")
104116
break
105117
else:
106118
attempts -= 1
107119
if attempts > 0:
108-
print(f"Sorry, that's incorrect. You have {attempts} {'attempts' if attempts > 1 else 'attempt'} remaining.")
120+
print(
121+
f"Sorry, that's incorrect. You have {attempts} {'attempts' if attempts > 1 else 'attempt'} remaining.")
109122
else:
110123
print(f"Sorry, the correct answer is: {solution}")
111124
except ValueError:
112125
print("Invalid input. Please enter a numeric answer.")
113126
except ValueError as e:
114127
print(e)
115128

129+
116130
if __name__ == "__main__":
117131
main()

0 commit comments

Comments
 (0)