Python Programming Scenario-Based Questions
Question 1: Library Inventory and Borrowing System
Scenario:
You're building a program to help a local library keep track of its book inventory and member
borrowing activities. Here are the requirements:
1. Book Details Storage: Each book should have an ID (integer), title (string), author (string), and
genre (string). Store this data in a list of dictionaries where each dictionary represents a book.
2. Genre Classification: Write a function called classify_books_by_genre that:
- Takes the list of books as input.
- Creates a dictionary where each key is a genre, and the value is a tuple containing the book IDs
of books in that genre.
- Returns this dictionary.
3. Unique Authors: Write a function called unique_authors that:
- Takes the list of books as input.
- Uses a set to collect unique authors.
- Returns a list of unique authors sorted alphabetically.
4. Member Borrowing Tracker: Members can borrow books by their IDs. Use a dictionary called
borrowed_books where:
- Each key is a member's name (string).
- Each value is a list of tuples, with each tuple containing (book ID, title).
5. Borrowing Limit and Conditional Loops:
- Each member can borrow up to 3 books at a time.
- Write a function borrow_book that:
- Accepts the borrowed_books dictionary, the member's name, and the book ID to borrow.
- Checks if the member already borrowed 3 books. If so, print a message saying they can't
borrow more.
- If not, add the book to their borrowed list if it's available and update the borrowed_books
dictionary.
6. Return Book Function:
- Write a function return_book that:
- Accepts the borrowed_books dictionary, the member's name, and the book ID to return.
- Checks if the book is in the member's borrowed list. If it is, remove it and confirm the return.
- If it isn't in their list, print an appropriate message.
7. Books Borrowed in Each Genre:
- Write a function books_borrowed_by_genre that:
- Accepts the borrowed_books dictionary and the list of books.
- Returns a dictionary with genres as keys and sets of member names who borrowed books in
each genre.
Example Questions:
- Implement each function step-by-step as per the scenario.
- Test the borrowing limit by attempting to borrow a fourth book for a specific member.
- Demonstrate the use of books_borrowed_by_genre by simulating a few borrow actions and
checking which members borrowed from each genre.
Question 2: Movie Management System for a Streaming Service
Scenario:
You're creating a Movie Management System for a small streaming service. This service wants to
manage its catalog of movies, keep track of users' favorite genres, and recommend movies based
on user preferences. Here are the requirements:
1. Movie Database: Each movie should have an ID (integer), title (string), genre (string), director
(string), and release_year (integer). Store these details in a list of dictionaries, where each dictionary
represents a movie.
2. Classify Movies by Year: Write a function called classify_movies_by_year that:
- Accepts the list of movies as input.
- Returns a dictionary where each key is a release year, and the value is a set containing tuples of
(movie ID, title) for movies released in that year.
3. User Preferences: Each user can have a list of favorite genres. Use a dictionary called
user_preferences where:
- Each key is a user's name (string).
- Each value is a list of the user's favorite genres (e.g., ["Action", "Drama"]).
4. Unique Directors: Write a function called unique_directors that:
- Accepts the list of movies as input.
- Uses a set to collect unique directors.
- Returns a list of unique directors sorted alphabetically.
5. Recommend Movies:
- Write a function recommend_movies that:
- Accepts the list of movies, the user_preferences dictionary, and the user's name.
- Finds movies in the user's favorite genres that are not already watched (use a list to keep track
of watched movies).
- Returns a list of tuples (movie ID, title) of recommended movies for that user, based on genre
preference.
6. Mark Movies as Watched:
- Write a function mark_as_watched that:
- Accepts a user's name, a movie ID, and a dictionary called watched_movies.
- Adds the movie to a set of watched movies for each user, with the user's name as the key and
the set of watched movies as the value.
- Returns a message confirming the movie is marked as watched.
7. Favorite Genre by Year:
- Write a function favorite_genre_by_year that:
- Accepts the user_preferences dictionary, the watched_movies dictionary, and a specific release
year.
- Returns a dictionary where keys are genres and values are sets of user names who have
watched movies from their favorite genres released in that year.
Example Questions:
- Implement each function according to the requirements in the scenario.
- Test the recommend_movies function by adding favorite genres for a user and ensuring only
movies from those genres are recommended.
- Simulate marking movies as watched and verify the output of favorite_genre_by_year for a specific
year.
- Use classify_movies_by_year to list all movies released in a given year and demonstrate how it
categorizes them by year.