-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathsession-01-homework.py
More file actions
123 lines (103 loc) · 3.21 KB
/
Copy pathsession-01-homework.py
File metadata and controls
123 lines (103 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import csv
import os
import subprocess
FILE_PATH = "movies.csv"
def ensure_movies_file():
if os.path.exists(FILE_PATH):
return FILE_PATH
try:
subprocess.run(
[
"hf",
"download",
"Birkbeck/movies",
"movies.csv",
"--repo-type",
"dataset",
"--local-dir",
".",
],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except Exception:
pass
if os.path.exists(FILE_PATH):
return FILE_PATH
raise FileNotFoundError(
"movies.csv not found. Download it first with: "
"hf download Birkbeck/movies movies.csv --repo-type dataset --local-dir ."
)
def safe_float(value):
try:
return float(value)
except (TypeError, ValueError):
return None
def main():
path = ensure_movies_file()
with open(path, "r") as file:
rows = list(csv.reader(file))
header = rows[0]
data_rows = rows[1:]
genres_i = header.index("genres")
rating_i = header.index("rating_imdb")
runtime_i = header.index("runtime_min")
# Task 2: number of rows (excluding header) and number of columns.
print("Task 2")
print("Rows (excluding header):", len(data_rows))
print("Columns:", len(header))
print()
# Task 3: first 3 rows (including header).
print("Task 3 - First 3 rows (including header):")
for row in rows[:3]:
print(row)
print()
# Task 4: first movie where genres contains Action.
print("Task 4 - First movie where genres contains 'Action':")
found_action = None
for row in data_rows:
if "Action" in row[genres_i]:
found_action = row
break
print(found_action if found_action else "No matching movie found.")
print()
# Task 5: average rating_imdb.
print("Task 5 - Average rating_imdb:")
total_rating = 0.0
count_rating = 0
for row in data_rows:
value = safe_float(row[rating_i])
if value is None:
continue
total_rating += value
count_rating += 1
print(total_rating / count_rating if count_rating else "No valid values")
print()
# Task 6: average of one more numeric column (runtime_min).
print("Task 6 - Average runtime_min:")
total_runtime = 0.0
count_runtime = 0
for row in data_rows:
value = safe_float(row[runtime_i])
if value is None:
continue
total_runtime += value
count_runtime += 1
print(total_runtime / count_runtime if count_runtime else "No valid values")
print()
# Task 7: count movies with rating_imdb >= 8.0.
print("Task 7 - Count rating_imdb >= 8.0:")
high_rating_count = 0
for row in data_rows:
value = safe_float(row[rating_i])
if value is not None and value >= 8.0:
high_rating_count += 1
print(high_rating_count)
print()
# Task 8: complexity notes.
print("Task 8 - Complexity notes:")
print("First-match search (Action): time O(n), space O(1)")
print("Average computation: time O(n), space O(1)")
if __name__ == "__main__":
main()