Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c77e642

Browse files
Merge pull request avinashkranjan#2386 from andoriyaprashant/branch11
Coding Puzzle Generator Script added
2 parents 7f21d37 + efb7ba2 commit c77e642

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

‎Coding Puzzle Generator/README.md‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Coding Puzzle Generator
2+
3+
Welcome to the Coding Puzzle Generator! This Python script generates interactive coding puzzles with various difficulty levels and different programming topics. It currently includes three types of puzzles: arithmetic, string manipulation, and logical puzzles.
4+
5+
## How to Use
6+
7+
1. Clone the repository or download the `coding_puzzle_generator.py` script.
8+
9+
2. Make sure you have Python installed on your system.
10+
11+
3. Run the script using the following command in your terminal or command prompt:
12+
13+
```bash
14+
python coding_puzzle_generator.py
15+
```
16+
17+
4. You will be prompted to choose a difficulty level (easy, medium, or hard) for the puzzle. Enter your choice and hit Enter.
18+
19+
5. Solve the puzzle displayed in the console.
20+
21+
6. If you get stuck or need a hint, you can enter `hint` as your answer, and the script will provide a hint for the current puzzle.
22+
23+
7. You have three attempts to solve each puzzle. If you enter an incorrect answer, the script will inform you of the remaining attempts.
24+
25+
8. After completing or exhausting your attempts, the script will reveal the correct answer and move on to the next puzzle.
26+
27+
## Puzzle Types
28+
29+
### 1. Arithmetic Puzzle
30+
Arithmetic puzzles involve basic arithmetic operations with random numbers. Your task is to solve the arithmetic expression and provide the correct answer.
31+
32+
### 2. String Manipulation Puzzle
33+
String manipulation puzzles challenge you to perform tasks like rearranging letters or replacing characters in a given word. You must reveal the original word based on the provided puzzle.
34+
35+
### 3. Logical Puzzle
36+
Logical puzzles include tasks related to boolean logic using `and`, `or`, and `not` operators. You need to evaluate the logical expression and provide the correct boolean result.
37+
38+
## Customization
39+
40+
You can easily customize the puzzle types, difficulty levels, and hints by modifying the `generate_puzzle()` function in the `coding_puzzle_generator.py` script. Feel free to add more puzzle types and enhance the interactive solving experience.
41+
42+
## Requirements
43+
44+
- Python
45+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import random
2+
3+
def generate_puzzle(difficulty):
4+
if difficulty == "easy":
5+
return generate_easy_puzzle()
6+
elif difficulty == "medium":
7+
return generate_medium_puzzle()
8+
elif difficulty == "hard":
9+
return generate_hard_puzzle()
10+
else:
11+
raise ValueError("Invalid difficulty level. Please choose 'easy', 'medium', or 'hard'.")
12+
13+
def generate_easy_puzzle():
14+
puzzle_type = random.choice(["arithmetic", "string"])
15+
if puzzle_type == "arithmetic":
16+
num1 = random.randint(1, 10)
17+
num2 = random.randint(1, 10)
18+
operator = random.choice(["+", "-"])
19+
puzzle = f"{num1} {operator} {num2}"
20+
solution = eval(puzzle)
21+
hint = "This is an arithmetic puzzle."
22+
else: # puzzle_type == "string"
23+
word = random.choice(["hello", "python", "coding", "challenge"])
24+
shuffle_word = "".join(random.sample(word, len(word)))
25+
puzzle = shuffle_word
26+
solution = word
27+
hint = "Rearrange the letters to form a meaningful word."
28+
29+
return puzzle, solution, hint
30+
31+
def generate_medium_puzzle():
32+
puzzle_type = random.choice(["arithmetic", "string", "logical"])
33+
if puzzle_type == "arithmetic":
34+
num1 = random.randint(10, 50)
35+
num2 = random.randint(1, 10)
36+
operator = random.choice(["+", "-", "*"])
37+
puzzle = f"{num1} {operator} {num2}"
38+
solution = eval(puzzle)
39+
hint = "This is an arithmetic puzzle."
40+
elif puzzle_type == "string":
41+
word = random.choice(["apple", "banana", "orange", "grape"])
42+
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))
45+
solution = word
46+
hint = f"Remove {num_chars_to_remove} letter(s) to reveal the original word."
47+
else: # puzzle_type == "logical"
48+
num1 = random.randint(1, 10)
49+
num2 = random.randint(1, 10)
50+
operator = random.choice(["and", "or"])
51+
puzzle = f"{num1} {operator} {num2}"
52+
solution = eval(puzzle.capitalize())
53+
hint = "This is a logical puzzle."
54+
55+
return puzzle, solution, hint
56+
57+
def generate_hard_puzzle():
58+
puzzle_type = random.choice(["arithmetic", "string", "logical"])
59+
if puzzle_type == "arithmetic":
60+
num1 = random.randint(50, 100)
61+
num2 = random.randint(10, 20)
62+
operator = random.choice(["+", "-", "*", "/"])
63+
puzzle = f"{num1} {operator} {num2}"
64+
solution = eval(puzzle)
65+
hint = "This is an arithmetic puzzle."
66+
elif puzzle_type == "string":
67+
word = random.choice(["python", "programming", "challenge"])
68+
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))
71+
puzzle = "".join(c if i not in indices_to_replace else replacement_chars[idx]
72+
for idx, c in enumerate(word))
73+
solution = word
74+
hint = f"Replace {num_chars_to_replace} letter(s) with {replacement_chars} to reveal the original word."
75+
else: # puzzle_type == "logical"
76+
num1 = random.randint(1, 10)
77+
num2 = random.randint(1, 10)
78+
operator = random.choice(["and", "or", "not"])
79+
puzzle = f"{operator.capitalize()} {num1} == {num2}"
80+
solution = eval(f"{num1} {operator} {num2}")
81+
hint = "This is a logical puzzle."
82+
83+
return puzzle, solution, hint
84+
85+
def main():
86+
print("Welcome to the Coding Puzzle Generator!")
87+
difficulty_level = input("Choose difficulty level (easy, medium, hard): ").lower()
88+
89+
try:
90+
puzzle, solution, hint = generate_puzzle(difficulty_level)
91+
print("\nSolve the puzzle:")
92+
print(f"Question: {puzzle}")
93+
94+
attempts = 3
95+
while attempts > 0:
96+
user_answer = input("Your answer: ").strip()
97+
if user_answer.lower() == "hint":
98+
print(f"HINT: {hint}")
99+
else:
100+
try:
101+
user_answer = float(user_answer) # Convert to float for numeric puzzles
102+
if user_answer == solution:
103+
print("Congratulations! Your answer is correct.")
104+
break
105+
else:
106+
attempts -= 1
107+
if attempts > 0:
108+
print(f"Sorry, that's incorrect. You have {attempts} {'attempts' if attempts > 1 else 'attempt'} remaining.")
109+
else:
110+
print(f"Sorry, the correct answer is: {solution}")
111+
except ValueError:
112+
print("Invalid input. Please enter a numeric answer.")
113+
except ValueError as e:
114+
print(e)
115+
116+
if __name__ == "__main__":
117+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /