Week 15 Tutorial: Revision Tutorial
Problem 1: The Password Strength Validator
Problem Statement:
Write a function validate_password(password) that checks if a string meets the following security requirements:
- Must be at least 8 characters long.
- Must contain at least one uppercase letter.
- Must contain at least one digit (
0-9). - Must NOT contain the word “password” (case-insensitive).
The function should return True if valid, and False otherwise.
Test Case:
print(validate_password("apple")) # False (too short)
print(validate_password("Password123")) # False (contains 'password')
print(validate_password("security")) # False (no uppercase, no digit)
print(validate_password("SecureCode99")) # True
Problem 2: The "Guess the Number" Game
Problem Statement: Create a simple interactive game.
- Import the
randommodule. - Generate a random integer between 1 and 20.
- Use a
whileloop to ask the user for a guess. - If the guess is too high, print “Too high!”.
- If the guess is too low, print “Too low!”.
- If the guess is correct, print “You got it!” and stop the loop.
- Bonus: Limit the user to only 5 attempts. If they fail 5 times, print “Game Over” and the correct number.
Sample Run:
I'm thinking of a number between 1 and 20.
Attempt 1/5. Enter your guess: 10
Too high!
Attempt 2/5. Enter your guess: 5
Too low!
Attempt 3/5. Enter your guess: 7
You got it!
Problem 3: The Missing assignment Checker
Problem Statement: You have a list of all students in the class and a list of students who have submitted their assignment.
- Convert the lists to sets.
- Find the students who have not submitted yet.
- Find the students who submitted but are not on the class list (perhaps they transferred or spelled their name wrong).
- Print both groups sorted alphabetically.
Data:
all_students = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank"]
submitted = ["alice", "Bob", "Frank", "George"] # Note: 'alice' is lowercase, 'George' is new
Expected Output:
Not submitted:
- Charlie
- David
- Eve
Not on class list:
- George
Problem 4: The Report Generator
Problem Statement:
You have a dictionary of sales data. Write a function generate_report(sales_data, filename) that:
- Calculates the total sales.
- Calculates the average sale.
- Creates a new file with the given filename.
- Writes the details in a professional format (see expected output).
Data:
sales = {"Mon": 100.50, "Tue": 200.00, "Wed": 150.75}
Expected File Content:
WEEKLY SALES REPORT
-------------------
Mon: $100.50
Tue: $200.00
Wed: $150.75
-------------------
Total: $451.25
Average: $150.42
Problem 5: The Matrix Updater
Problem Statement:
You have a 3x3 grid of numbers representing pixels in a grayscale image.
grid = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
Write a script that increases the brightness of every pixel by 10. However, the maximum value a pixel can be is 255. If adding 10 makes it greater than 255, set it to 255.
Modify the grid list directly (do not create a new one).
Expected Output:
[[20, 30, 40], [50, 60, 70], [80, 90, 100]]
Problem 6: The Social Network Recommender
Problem Statement: You are building a “People You May Know” feature for a social media app. You have a dictionary representing the social graph where keys are names and values are sets of their friends.
Write a function recommend_friends(network, user) that returns a list of friend recommendations for a specific user.
The Rules:
- A recommendation must be a “friend of a friend” (2nd-degree connection).
- You cannot recommend someone who is already a direct friend of the user.
- You cannot recommend the user to themselves.
- The Challenge: The output list must be sorted by the number of mutual friends in descending order. If two people have the same number of mutual friends, sort them alphabetically.
Data:
social_graph = {
"Alice": {"Bob", "Charlie", "David"},
"Bob": {"Alice", "Eve", "Frank"},
"Charlie": {"Alice", "Eve"},
"David": {"Alice"},
"Eve": {"Bob", "Charlie"},
"Frank": {"Bob"}
}
Logic Hints:
- You need to look at Alice’s friends (Bob, Charlie, David).
- Then look at their friends to find candidates (Eve, Frank).
- Count how many times each candidate appears (Eve is friends with Bob AND Charlie, so she has 2 mutuals with Alice. Frank is only friends with Bob, so 1 mutual).
Test Case:
# Alice knows Bob, Charlie, David.
# Bob knows Eve, Frank. (Eve and Frank are candidates)
# Charlie knows Eve. (Eve is a candidate)
# David knows nobody else.
# Eve has 2 mutual friends with Alice (Bob and Charlie).
# Frank has 1 mutual friend with Alice (Bob).
print(recommend_friends(social_graph, "Alice"))
Expected Output:
['Eve', 'Frank']
Problem 7: The Warehouse Robot
Problem Statement: You are simulating a robot moving through a warehouse grid. The grid is a 2D list where:
"."represents an empty space."X"represents a wall/obstacle."S"represents the robot’s starting position.
Write a function navigate_robot(grid, commands) that takes the grid and a string of commands (e.g., "UDLR" for Up, Down, Left, Right).
The Rules:
- Find the starting position
"S". - Process the commands one by one.
- The robot moves 1 step in the specified direction UNLESS:
- The move would take it out of bounds (off the grid).
- The move hits a wall (
"X").
- In those invalid cases, the robot stays in its current spot for that turn.
- Mark every cell the robot visits (including the final spot) with a
"*". - Return the final coordinates as a tuple
(row, col)and print the modified grid.
Data:
warehouse = [
[".", ".", "X", ".", "."],
[".", "S", ".", "X", "."],
[".", ".", ".", ".", "."],
["X", "X", "X", ".", "."]
]
moves = "RRDDRU"
# Logic trace:
# Start at (1,1).
# R -> (1,2).
# R -> Hits 'X' at (1,3)! Stay at (1,2).
# D -> (2,2).
# D -> Hits 'X' at (3,2)! Stay at (2,2).
# R -> (2,3).
# U -> Hits 'X' at (1,3)! Stay at (2,3).
Test Case:
final_pos = navigate_robot(warehouse, moves)
print(f"Final Position: {final_pos}")
Expected Output:
['.', '.', 'X', '.', '.']
['.', '*', '*', 'X', '.']
['.', '.', '*', '*', '.']
['X', 'X', 'X', '.', '.']
Final Position: (2, 3)
This content will be available starting January 13, 2026.