-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.py
More file actions
182 lines (156 loc) · 4.54 KB
/
Copy pathsolution.py
File metadata and controls
182 lines (156 loc) · 4.54 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# %% [markdown]
# # SQL Murder Mystery
#
# A crime has taken place and the detective needs your help. The detective gave
# you the crime scene report, but you somehow lost it.
#
# ## Clue 1
#
# You vaguely remember that the crime was a **murder** that occurred sometime on
# **Jan. 15, 2018** and that it took place in **SQL City**. Start by retrieving
# the corresponding crime scene report from the police department's database. If
# you want to get the most out of this mystery, try to work through it only
# using your SQL environment and refrain from using a notepad.
# %%
import pandas as pd
from IPython.display import Markdown, display
# %%
drivers_license = pd.read_csv("drivers_license.zip")
income = pd.read_csv("income.zip")
get_fit_now_members = pd.read_csv("get_fit_now_members.zip")
interview = pd.read_csv("interview.zip")
person = pd.read_csv("person.zip")
facebook_event_check_in = pd.read_csv("facebook_event_check_in.zip")
get_fit_now_check_in = pd.read_csv("get_fit_now_check_in.zip")
crime_scene_report = pd.read_csv("crime_scene_report.zip")
# %%
db = [
drivers_license,
income,
get_fit_now_members,
interview,
person,
facebook_event_check_in,
get_fit_now_check_in,
crime_scene_report,
]
# %% [markdown]
# 
# %%
deposition = (
crime_scene_report.assign(
date=pd.to_datetime(crime_scene_report["date"], format="%Y%m%d")
)
.set_index("date")
.loc["1/15/2018"]
.query("type == 'murder' and city == 'SQL City'")
)
display(deposition)
# %%
display(Markdown("## Clue 2"))
display(Markdown(deposition["description"].values[0]))
# %%
def namestr(obj, namespace=locals()) -> str:
return [name for name in namespace if namespace[name] is obj][0]
# %%
def print_table_names():
print([namestr(d) for d in db])
# %%
for d in db:
print(namestr(d))
display(d.sample())
print()
# %%
witness_1 = person.loc[person["address_street_name"] == "Northwestern Dr"].loc[
lambda df: df["address_number"] == df["address_number"].max()
]
display(witness_1)
# %%
witness_2 = person.loc[
(person["name"].str.contains("Annabel"))
& (person["address_street_name"] == "Franklin Ave")
]
display(witness_2)
# %%
witnesses = pd.concat([witness_1, witness_2])
# %%
clue_3 = interview.loc[interview["person_id"].isin(witnesses["id"])].merge(
witnesses, left_on="person_id", right_on="id"
)
# %%
display(Markdown("## Clue 3"))
for t in clue_3["transcript"]:
display(Markdown(t))
print()
# %%
suspect = (
get_fit_now_members.loc[
(get_fit_now_members["membership_status"] == "gold")
& (get_fit_now_members["id"].str[:3] == "48Z")
]
.merge(person, left_on="person_id", right_on="id", suffixes=["_gym", "_person"])
.merge(
drivers_license.loc[drivers_license["plate_number"].str.contains("H42W")],
left_on="license_id",
right_on="id",
suffixes=["_person", "_driver"],
)
).merge(
get_fit_now_check_in.assign(
check_in_date=pd.to_datetime(
get_fit_now_check_in["check_in_date"], format="%Y%m%d"
),
)
.set_index("check_in_date")
.loc["1/9/2018"],
left_on="id_gym",
right_on="membership_id",
)
# %%
display(suspect.T)
# %%
display(Markdown("## Clue 4"))
display(
Markdown(
interview.loc[interview["person_id"].isin(suspect["person_id"])][
"transcript"
].iloc[0]
)
)
# %%
mastermind_concert = (
drivers_license.loc[
(drivers_license["height"].isin(range(65, 68)))
& (drivers_license["car_make"] == "Tesla")
& (drivers_license["hair_color"] == "red")
& (drivers_license["gender"] == "female")
]
.merge(person, left_on="id", right_on="license_id", suffixes=["_driver", "_person"])
.merge(
(
facebook_event_check_in.assign(
date=pd.to_datetime(facebook_event_check_in["date"], format="%Y%m%d")
)
.set_index("date")
.loc["12/2017"]
.reset_index()
.query("event_name == 'SQL Symphony Concert'")
),
left_on="id_person",
right_on="person_id",
)
)
# %%
display(mastermind_concert)
# %%
mastermind = person.loc[
person["id"].isin(mastermind_concert.drop_duplicates()["person_id"])
]
# %%
display(mastermind)
# %%
query = f"INSERT INTO solution VALUES (1, '{mastermind['name'].iloc[0]}');SELECT value FROM solution;"
print(query)
# %% language="bash"
#
# sqlite3 sql-murder-mystery.db "INSERT INTO solution VALUES (1, 'Miranda Priestly');SELECT value FROM solution;"