← Computer Programming I


Problem 1: The Password Strength Validator

Problem Statement: Write a function validate_password(password) that checks if a string meets the following security requirements:

  1. Must be at least 8 characters long.
  2. Must contain at least one uppercase letter.
  3. Must contain at least one digit (0-9).
  4. 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.

  1. Import the random module.
  2. Generate a random integer between 1 and 20.
  3. Use a while loop to ask the user for a guess.
  4. If the guess is too high, print “Too high!”.
  5. If the guess is too low, print “Too low!”.
  6. If the guess is correct, print “You got it!” and stop the loop.
  7. 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.

  1. Convert the lists to sets.
  2. Find the students who have not submitted yet.
  3. Find the students who submitted but are not on the class list (perhaps they transferred or spelled their name wrong).
  4. 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:

  1. Calculates the total sales.
  2. Calculates the average sale.
  3. Creates a new file with the given filename.
  4. 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:

  1. A recommendation must be a “friend of a friend” (2nd-degree connection).
  2. You cannot recommend someone who is already a direct friend of the user.
  3. You cannot recommend the user to themselves.
  4. 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:

  1. Find the starting position "S".
  2. Process the commands one by one.
  3. 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").
  4. In those invalid cases, the robot stays in its current spot for that turn.
  5. Mark every cell the robot visits (including the final spot) with a "*".
  6. 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)