Introduction
What is a Number Guessing Game?
A number guessing game is a simple game where:
The system generates a random number
The player tries to guess that number
The system gives hints like:
"Too high"
"Too low"
The game continues until the correct number is guessed
How Performance is Measured
The number of attempts taken by a player is recorded
The difficulty level (Easy, Medium, Hard) may also be chosen
In a number guessing game, just finding the correct number is not enough.
We also need to compare players and rank them based on their performance.
This can be done using a leaderboard system, where players are sorted based on:
- Difficulty level
- Number of attempts
Problem Statement
The task is to:
- Show the leaderboard details
- Sort the leaderboard based on:
- Difficulty (Easy → Medium → Hard)
- Attempts (less attempts = better rank)
Example Input
Alice Hard 7
Bob Easy 3
Charlie Medium 5
David Easy 2
Expected Output
David Easy 2
Bob Easy 3
Charlie Medium 5
Alice Hard 7
Concept Used: Selection Sort
Selection Sort works in a simple way:
- Find the smallest element
- Place it in the correct position
- Repeat for the remaining elements
In this problem:
-
“Smallest” means:
- Easier difficulty first
- If same difficulty → fewer attempts
Step 1: Store Leaderboard Data
leaderboard = [
{"name": "Alice", "difficulty": "Hard", "attempts": 7},
{"name": "Bob", "difficulty": "Easy", "attempts": 3},
{"name": "Charlie", "difficulty": "Medium", "attempts": 5},
{"name": "David", "difficulty": "Easy", "attempts": 2}
]
Explanation
-
leaderboardis a list - Each player is stored as a dictionary
-
Keys:
-
name→ player name -
difficulty→ level -
attempts→ number of tries
-
Step 2: Define Difficulty Order
order = {"Easy": 1, "Medium": 2, "Hard": 3}
Explanation
- Difficulty is text, so we convert it into numbers
- Easy = 1, Medium = 2, Hard = 3
- This helps in comparison during sorting
Step 3: Selection Sort Code
n = len(leaderboard)
for i in range(n):
min_index = i
for j in range(i + 1, n):
if (order[leaderboard[j]["difficulty"]], leaderboard[j]["attempts"]) < \
(order[leaderboard[min_index]["difficulty"]], leaderboard[min_index]["attempts"]):
min_index = j
leaderboard[i], leaderboard[min_index] = leaderboard[min_index], leaderboard[i]
Line-by-Line Explanation
Line 1
n = len(leaderboard)
- Finds the total number of players
Line 2
for i in range(n):
- Outer loop
- Moves from first position to last
Line 3
min_index = i
- Assumes current position has the smallest element
Line 4
for j in range(i + 1, n):
- Inner loop
- Checks remaining elements
Line 5 (Important)
if (order[leaderboard[j]["difficulty"]], leaderboard[j]["attempts"]) < (order[leaderboard[min_index]["difficulty"]], leaderboard[min_index]["attempts"]):
Explanation:
- Compares two players
- First compares difficulty
- Then compares attempts
So:
- Easy comes before Medium
- If same → fewer attempts wins
Line 6
min_index = j
- Updates the index of the smallest player
Line 7
leaderboard[i], leaderboard[min_index] = leaderboard[min_index], leaderboard[i]
- Swaps the players
- Places the smallest element in correct position
Step 4: Display Sorted Leaderboard
for player in leaderboard:
print(player)
Explanation
- Loops through sorted list
- Prints each player
Output
{'name': 'David', 'difficulty': 'Easy', 'attempts': 2}
{'name': 'Bob', 'difficulty': 'Easy', 'attempts': 3}
{'name': 'Charlie', 'difficulty': 'Medium', 'attempts': 5}
{'name': 'Alice', 'difficulty': 'Hard', 'attempts': 7}
Conclusion
This project uses:
- Basic data structures (list and dictionary)
- Selection Sort for ordering
Selection Sort is simple to understand and works well for small datasets like this leaderboard.
Top comments (0)