S2Q2 · Movie List Watch Time and Unique Movies¶
⚡ Quick Reference
Type: Full I/O problem
Core idea: parse each list's two lines, accumulate total watch time per list and a global set of unique movies, then print.
n = int(input())
totals = []
all_movies = set()
for _ in range(n):
list_line = input()
time_line = input()
list_name, movies_str = list_line.split(";")
movies = movies_str.split(",")
times = list(map(int, time_line.split(",")))
totals.append((list_name, sum(times)))
all_movies.update(movies)
for name, total in totals:
print(f"{name}: {total}")
print("Unique movies across all lists:", ", ".join(sorted(all_movies)))
Key rules:
- First line of each entry: ListName;Movie1,Movie2,...
- Second line: t1,t2,... (corresponding watch times)
- Print totals in input order
- Print unique movies sorted lexicographically, comma-and-space separated
Problem Statement¶
Problem (I/O type)
Read n movie lists, each with movies and watch times. Print total watch time per list, then all unique movie names sorted alphabetically.
Example:
3
Watchlist1;Inception,Dunkirk,Tenet
120,106,150
Watchlist2;Interstellar,Tenet
169,150
Watchlist3;Inception,Joker
120,122
Watchlist1: 376
Watchlist2: 319
Watchlist3: 242
Unique movies across all lists: Dunkirk, Inception, Interstellar, Joker, Tenet
Understanding the input format¶
Each list entry spans two lines:
Watchlist1;Inception,Dunkirk,Tenet ← ListName;Movie1,Movie2,...
120,106,150 ← t1,t2,... (same order as movies)
Parsing:
list_line.split(";") → ["Watchlist1", "Inception,Dunkirk,Tenet"]
movies_str.split(",") → ["Inception", "Dunkirk", "Tenet"]
time_line.split(",") → ["120", "106", "150"]
sum(map(int, ...)) → 376
Tenet appears in both Watchlist1 and Watchlist2 - the set deduplicates automatically.
Tracing the example¶
| List | Movies | Times | Total | New unique movies |
|---|---|---|---|---|
| Watchlist1 | Inception, Dunkirk, Tenet | 120, 106, 150 | 376 | Inception, Dunkirk, Tenet |
| Watchlist2 | Interstellar, Tenet | 169, 150 | 319 | Interstellar (Tenet already in set) |
| Watchlist3 | Inception, Joker | 120, 122 | 242 | Joker (Inception already in set) |
Unique movies sorted: Dunkirk, Inception, Interstellar, Joker, Tenet ✓
Solution approaches¶
n = int(input())
totals = []
all_movies = set()
for _ in range(n):
list_name, movies_str = input().split(";")
times = list(map(int, input().split(",")))
movies = movies_str.split(",")
totals.append((list_name, sum(times)))
all_movies.update(movies)
for name, total in totals:
print(f"{name}: {total}")
print("Unique movies across all lists:", ", ".join(sorted(all_movies)))
n = int(input())
totals = []
all_movies = set()
for _ in range(n):
# Parse list line
list_line = input()
list_name, movies_str = list_line.split(";")
movies = movies_str.split(",")
# Parse time line
time_line = input()
times = [int(t) for t in time_line.split(",")]
# Accumulate
total = sum(times)
totals.append((list_name, total))
for movie in movies:
all_movies.add(movie)
# Output totals
for name, total in totals:
print(f"{name}: {total}")
# Output unique movies
unique_sorted = sorted(all_movies)
print("Unique movies across all lists: " + ", ".join(unique_sorted))
n = int(input())
entries = []
for _ in range(n):
name, movies_str = input().split(";")
times = list(map(int, input().split(",")))
entries.append((name, movies_str.split(","), sum(times)))
list(map(lambda e: print(f"{e[0]}: {e[2]}"), entries))
all_movies = sorted(set(
movie for _, movies, _ in entries for movie in movies
))
print("Unique movies across all lists:", ", ".join(all_movies))
Collects all entries first, then uses map(lambda ...) for printing totals. The nested generator flattens all movie lists into one set for deduplication.
Key takeaways¶
set.update() for batch deduplication
all_movies.update(movies) adds all movies from the current list to the set at once - automatically deduplicating. Equivalent to a loop of add() calls but cleaner.
Store totals in order before printing
Sets are unordered - they can't track per-list totals. Store (name, total) tuples in a list to preserve input order for the first output block, then use the set only for the unique movies.
", ".join(sorted(set)) for sorted unique output
sorted() returns a sorted list from any iterable including sets. ", ".join(...) formats it as comma-and-space separated. The two together handle both requirements in one line.