CSE 6040 Midterm 2 Questions and 100% Correct Answers 2026/27
Latest - Georgia Institute Of Technology.
Version 1.0.0
All of the header information is important. Please read it..
Topics number of exercises: This problem builds on your knowledge of Working with relational data
(SQL/Pandas), Working with Numpy Arrays. It has 7 exercises numbered 0 to 6. There are 13 available
points. However to earn 100% the threshold is 11 points. (Therefore once you hit 11 points you can stop. There
is no extra credit for exceeding this threshold.)
Exercise ordering: Each exercise builds logically on previous exercises but you may solve them in any order.
That is if you can't solve an exercise you can still move on and try the next one. Use this to your advantage as
the exercises are not necessarily ordered in terms of difficulty. Higher point values generally indicate more
difficult exercises.
Demo cells: Code cells starting with the comment ### define demo inputs load results from prior exercises
applied to the entire data set and use those to build demo inputs. These must be run for subsequent demos to
work properly but they do not affect the test cells. The data loaded in these cells may be rather large (at least in
terms of human readability). You are free to print or otherwise use Python to explore them but we may not print
them in the starter code.
Debugging you code: Right before each exercise test cell there is a block of text explaining the variables
available to you for debugging. You may use these to test your code and can print/display them as needed
(careful when printing large objects you may want to print the head or chunks of rows at a time).
Exercise point breakdown:
Exercise 0 - : 2 point(s)
Exercise 1 - : 1 point(s)
Exercise 2 - : 2 point(s)
Exercise 3 - : 2 point(s)
Exercise 4 - : 2 point(s)
Exercise 5 - : 1 point(s)
Exercise 6 - : 3 point(s)
Final reminders:
Submit after every exercise
Review the generated grade report after you submit to see what errors were returned
Stay calm, skip problems as needed and take short breaks at your leisure
CSE 6040 Midterm 2
, New York Collisions (https://cdn-
uploads.piazza.com/paste/h60voulh3533n3/524396350096ff4fbefa248694bb5658fd3
York-Collisions)
The City of New York collects detailed data on traffic collisions where police are involved. For each collision
information including the date, time, geographic coordinates, vehicle details, and demographics of the people
involved. They make this data and much, much more publicly available on https://opendata.cityofnewyork.us/
(https://opendata.cityofnewyork.us/). We have gone ahead and packaged this data into a SQLite object. The next
code cell opens a connection to it.
In this notebook we're going to explore the structure of the tables in the connection, summarize them, and then
preprocess and analyze geographic data.
In [1]: import re
import pandas as pd
import numpy as np
import sqlite3
conn = sqlite3.connect('file:resource/asnlib/publicdata/traffic.db?mode=ro', u
ri=True)
Structure of tables
There are 3 tables in our connection: CRASHES, VEHICLES, and PERSON. We don't know of the relationships
between the tables or even what columns they contain. The code we will write in the next two exercises will help
find these answers.
Exercise 0: (2 points)
get_table_cols
Your task: define get_table_cols as follows:
CSE 6040 Midterm 2
, Given a SQLite database connection and a table name, determine the column names for that table. Return your
result as a Python list sorted in alphabetical order.
Your function 'get_table_cols' should first verify that the table_name parameter only contains letters, numbers, or
underscores. If that is not the case a ValueError must be raised.
Hint: Your solution will likely require a dynamically generated query, as SQLite does not allow parameters in a
FROM clause.
Hint: If you choose to use a SELECT statement, be mindful of how many rows it returns... It's probably not a
good use of resources to use all of them.
CSE 6040 Midterm 2
, In [23]: ### Solution - Exercise 0
def get_table_cols(table_name, conn):
# GOAL: Determine if table_name contains only alphanumeric characters and
underscores. If not, raise ValueError.
# If so, return sorted list of column names in the table.
# INPUT:
# 'table_name' is a string
# 'conn' is a DB connection
# STRATEGY:
# 1. Check if each character in table_name is alphanumeric or an underscor
e. If not, raise Value Error.
# 2. Find columns in table_name. Sort and return.
# SOLUTION (VERSION 1):
for character in table_name:
if not (character.isalnum() or character == '_'):
raise ValueError
query = f'select * from {table_name} limit 1'
entire_table = pd.read_sql(query, conn)
column_names = entire_table.columns
return sorted(column_names)
#
# SOLUTION (VERSION 2):
for character in table_name:
# If you don't remember regex, .isalnum(), .isalpha(), .isnumeric()..
You can always do something like this!
if character not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW
XYZ0123456789_':
raise ValueError
# Google Search: 'sqlite get column names from table'
# Google Result: https://stackoverflow.com/questions/947215/how-to-get-a-l
ist-of-column-names-on-sqlite3-database
alternative_query = f'PRAGMA table_info({table_name})'
column_info = pd.read_sql(alternative_query, conn)
column_name_list = sorted(column_info['name'])
return column_name_list
### Demo function call
print(get_table_cols(table_name='vehicles', conn=conn))
['COLLISION_ID', 'CONTRIBUTING_FACTOR_1', 'CONTRIBUTING_FACTOR_2', 'CRASH_DAT
E', 'CRASH_TIME', 'DRIVER_LICENSE_JURISDICTION', 'DRIVER_LICENSE_STATUS', 'DR
IVER_SEX', 'POINT_OF_IMPACT', 'PRE_CRASH', 'PUBLIC_PROPERTY_DAMAGE', 'PUBLIC_
PROPERTY_DAMAGE_TYPE', 'STATE_REGISTRATION', 'TRAVEL_DIRECTION', 'UNIQUE_ID',
'VEHICLE_DAMAGE', 'VEHICLE_DAMAGE_1', 'VEHICLE_DAMAGE_2', 'VEHICLE_DAMAGE_3',
'VEHICLE_ID', 'VEHICLE_MAKE', 'VEHICLE_MODEL', 'VEHICLE_OCCUPANTS', 'VEHICLE_
TYPE', 'VEHICLE_YEAR']
CSE 6040 Midterm 2
Latest - Georgia Institute Of Technology.
Version 1.0.0
All of the header information is important. Please read it..
Topics number of exercises: This problem builds on your knowledge of Working with relational data
(SQL/Pandas), Working with Numpy Arrays. It has 7 exercises numbered 0 to 6. There are 13 available
points. However to earn 100% the threshold is 11 points. (Therefore once you hit 11 points you can stop. There
is no extra credit for exceeding this threshold.)
Exercise ordering: Each exercise builds logically on previous exercises but you may solve them in any order.
That is if you can't solve an exercise you can still move on and try the next one. Use this to your advantage as
the exercises are not necessarily ordered in terms of difficulty. Higher point values generally indicate more
difficult exercises.
Demo cells: Code cells starting with the comment ### define demo inputs load results from prior exercises
applied to the entire data set and use those to build demo inputs. These must be run for subsequent demos to
work properly but they do not affect the test cells. The data loaded in these cells may be rather large (at least in
terms of human readability). You are free to print or otherwise use Python to explore them but we may not print
them in the starter code.
Debugging you code: Right before each exercise test cell there is a block of text explaining the variables
available to you for debugging. You may use these to test your code and can print/display them as needed
(careful when printing large objects you may want to print the head or chunks of rows at a time).
Exercise point breakdown:
Exercise 0 - : 2 point(s)
Exercise 1 - : 1 point(s)
Exercise 2 - : 2 point(s)
Exercise 3 - : 2 point(s)
Exercise 4 - : 2 point(s)
Exercise 5 - : 1 point(s)
Exercise 6 - : 3 point(s)
Final reminders:
Submit after every exercise
Review the generated grade report after you submit to see what errors were returned
Stay calm, skip problems as needed and take short breaks at your leisure
CSE 6040 Midterm 2
, New York Collisions (https://cdn-
uploads.piazza.com/paste/h60voulh3533n3/524396350096ff4fbefa248694bb5658fd3
York-Collisions)
The City of New York collects detailed data on traffic collisions where police are involved. For each collision
information including the date, time, geographic coordinates, vehicle details, and demographics of the people
involved. They make this data and much, much more publicly available on https://opendata.cityofnewyork.us/
(https://opendata.cityofnewyork.us/). We have gone ahead and packaged this data into a SQLite object. The next
code cell opens a connection to it.
In this notebook we're going to explore the structure of the tables in the connection, summarize them, and then
preprocess and analyze geographic data.
In [1]: import re
import pandas as pd
import numpy as np
import sqlite3
conn = sqlite3.connect('file:resource/asnlib/publicdata/traffic.db?mode=ro', u
ri=True)
Structure of tables
There are 3 tables in our connection: CRASHES, VEHICLES, and PERSON. We don't know of the relationships
between the tables or even what columns they contain. The code we will write in the next two exercises will help
find these answers.
Exercise 0: (2 points)
get_table_cols
Your task: define get_table_cols as follows:
CSE 6040 Midterm 2
, Given a SQLite database connection and a table name, determine the column names for that table. Return your
result as a Python list sorted in alphabetical order.
Your function 'get_table_cols' should first verify that the table_name parameter only contains letters, numbers, or
underscores. If that is not the case a ValueError must be raised.
Hint: Your solution will likely require a dynamically generated query, as SQLite does not allow parameters in a
FROM clause.
Hint: If you choose to use a SELECT statement, be mindful of how many rows it returns... It's probably not a
good use of resources to use all of them.
CSE 6040 Midterm 2
, In [23]: ### Solution - Exercise 0
def get_table_cols(table_name, conn):
# GOAL: Determine if table_name contains only alphanumeric characters and
underscores. If not, raise ValueError.
# If so, return sorted list of column names in the table.
# INPUT:
# 'table_name' is a string
# 'conn' is a DB connection
# STRATEGY:
# 1. Check if each character in table_name is alphanumeric or an underscor
e. If not, raise Value Error.
# 2. Find columns in table_name. Sort and return.
# SOLUTION (VERSION 1):
for character in table_name:
if not (character.isalnum() or character == '_'):
raise ValueError
query = f'select * from {table_name} limit 1'
entire_table = pd.read_sql(query, conn)
column_names = entire_table.columns
return sorted(column_names)
#
# SOLUTION (VERSION 2):
for character in table_name:
# If you don't remember regex, .isalnum(), .isalpha(), .isnumeric()..
You can always do something like this!
if character not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW
XYZ0123456789_':
raise ValueError
# Google Search: 'sqlite get column names from table'
# Google Result: https://stackoverflow.com/questions/947215/how-to-get-a-l
ist-of-column-names-on-sqlite3-database
alternative_query = f'PRAGMA table_info({table_name})'
column_info = pd.read_sql(alternative_query, conn)
column_name_list = sorted(column_info['name'])
return column_name_list
### Demo function call
print(get_table_cols(table_name='vehicles', conn=conn))
['COLLISION_ID', 'CONTRIBUTING_FACTOR_1', 'CONTRIBUTING_FACTOR_2', 'CRASH_DAT
E', 'CRASH_TIME', 'DRIVER_LICENSE_JURISDICTION', 'DRIVER_LICENSE_STATUS', 'DR
IVER_SEX', 'POINT_OF_IMPACT', 'PRE_CRASH', 'PUBLIC_PROPERTY_DAMAGE', 'PUBLIC_
PROPERTY_DAMAGE_TYPE', 'STATE_REGISTRATION', 'TRAVEL_DIRECTION', 'UNIQUE_ID',
'VEHICLE_DAMAGE', 'VEHICLE_DAMAGE_1', 'VEHICLE_DAMAGE_2', 'VEHICLE_DAMAGE_3',
'VEHICLE_ID', 'VEHICLE_MAKE', 'VEHICLE_MODEL', 'VEHICLE_OCCUPANTS', 'VEHICLE_
TYPE', 'VEHICLE_YEAR']
CSE 6040 Midterm 2