Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Problem: You will use two files: 1) offence_codes.csv that contains offence codes and info about each code. 2) crime.csv contains info about 455872 crimes

Python Problem: You will use two files: 1) offence_codes.csv that contains offence codes and info about each code. 2) crime.csv contains info about 455872 crimes reported in Denver in 2018. Link: https://drive.google.com/file/d/1h6NLEH9AAef8ix5TP5UO4QNo0ttoZfEr/view?usp=sharing

1. Write a function code_to_names(code) that takes an int crime code and returns a list of strings where each string contains the code extension and the full name of the crime. Then write function code_from_keywords(keywords) that a list of strings and returns a list of all the crime codes that include all the keywords in their description.

2. Write a function crimes_by_code(code) that returns a list of all the crimes with the given code that have occurred. Each element of the return list is a tuple having the crime id, the abbreviated crime name, the date and time, and neighbourhood id of the crime. Followed by a function crimes_by_code_extension(code, extension) that returns a list of all the crimes with the given code and extension that have occurred.

Use Skelton Code

import csv ########################################################### # Question 1 ########################################################### def code_to_names(code): names = [] with open('offense_codes.csv') as csv_file: reader = csv.reader(csv_file, delimiter=',') next(reader) # skip the first row for row in reader: pass # Your code goes here return names ########################################################### # Question 2 ########################################################### def codes_from_keywords(keywords): codelist = [] with open('offense_codes.csv') as csv_file: reader = csv.reader(csv_file, delimiter=',') next(reader) # skip the first row for row in reader: pass return codelist ########################################################### # Question 3 ########################################################### def crimes_by_code(code): return [] # Your code goes here ########################################################### # Question 4 ########################################################### def crimes_by_code_extension(code, extension): crimes = [] with open('crime.csv') as csv_file: reader = csv.reader(csv_file, delimiter=",") next(reader) # skip the first row for row in reader: pass # Your code goes here return crimes

Final Output of program should look like this:

>>> code_to_names(2399) ['0: Theft - other', '1: Bicycle theft', '2: Theft of fuel by driving off without paying', '3: Theft of cable services', '4: Theft of construction equipment', '5: Theft of a trailer'] >>> >>> code_to_names(5707) ['0: Criminal trespassing'] >>> >>> codes_from_keywords(['bicycle', 'theft']) [2399] >>> >>> codes_from_keywords(['homicide', 'vehicle']) [] >>> codes_from_keywords(['homicide']) [5450, 7099, 7099, 7099, 902, 903, 904, 907, 908, 910, 911, 912] >>> >>> >>> codes_from_keywords(['homicide', 'police']) [907, 908] >>> >>> >>> codes_from_keywords(['terrorism']) [] >>> codes_from_keywords(['threat']) [5215, 1103] >>> >>> code_to_names(5215) ['0: Bomb threat'] >>> >>> code_to_names(907) ['0: Homicide of a Police Officer w/gun'] >>> >>> >>> code_to_names(908) ['0: Homicide of a Police Officer w/weapon'] >>> >>> crimes_by_code(907) [] >>> >>> crimes_by_code(908) [] >>> >>> bike_thefts = crimes_by_code(2399) >>> len(bike_thefts) 26213 >>> >>> import random >>> random.choice(bike_thefts) ('2016709223', 'theft-other', '11/4/2016 7:10:00 PM', 'baker') >>> >>> random.choice(bike_thefts) ('2018506702', 'theft-other', '7/26/2018 2:16:00 PM', 'speer') >>> >>> random.choice(bike_thefts) ('2016288223', 'theft-other', '5/7/2016 5:00:00 PM', 'university-hills') >>> >>> random.choice(bike_thefts) ('20156002885', 'theft-bicycle', '4/6/2015 6:30:00 PM', 'baker') >>> random.choice(bike_thefts) ('20166005004', 'theft-other', '5/20/2015 7:00:00 PM', 'country-club') >>> random.choice(bike_thefts) ('2017240382', 'theft-other', '4/10/2017 1:00:00 PM', 'lincoln-park') >>> random.choice(bike_thefts) ('20176010956', 'theft-bicycle', '11/17/2017 4:00:00 PM', 'baker') >>> random.choice(bike_thefts) ('2017188807', 'theft-bicycle', '3/22/2017 12:40:00 PM', 'bear-valley') >>> random.choice(bike_thefts) ('201886029', 'theft-other', '1/29/2018 9:00:00 AM', 'west-colfax') >>> random.choice(bike_thefts) ('2015408025', 'theft-bicycle', '7/18/2015 8:30:00 PM', 'congress-park') >>> >>> >>> codes_from_keywords(['homicide']) [5450, 7099, 7099, 7099, 902, 903, 904, 907, 908, 910, 911, 912] >>> >>> code_to_names(5450) ['0: Vehicular homicide'] >>> code_to_names(7099) ['0: Crimes against a person - other', '1: Conspiracy to commit homicide', '2: Solicitation to commit homicide', '3: Reckless endangerment', '4: Disarming a peace officer', '5: Accessory to commit homicide'] >>> >>> >>> a = crimes_by_code(7099) >>> len(a) 177 >>> >>> crimes_by_code_extension(7099, 1) [('2018405726', 'homicide-conspiracy', '6/17/2018 2:32:00 AM', 'bear-valley')] >>> crimes_by_code_extension(7099, 44) [] >>> crimes_by_code_extension(7099, 4) [('2018416946', 'disarming-a-peace-officer', '6/21/2018 6:23:00 PM', 'cbd'), ('201813622', 'disarming-a-peace-officer', '1/6/2018 9:41:00 PM', 'lincoln-park'), ('2018365254', 'disarming-a-peace-officer', '5/31/2018 10:50:00 AM', 'windsor'), ('2018878491', 'disarming-a-peace-officer', '12/31/2018 10:59:00 PM', 'west-colfax'), ('20192419', 'disarming-a-peace-officer', '1/2/2019 8:45:00 AM', 'capitol-hill'), ('2014233261', 'disarming-a-peace-officer', '5/8/2014 12:30:00 AM', 'south-park-hill'), ('2014430822', 'disarming-a-peace-officer', '8/16/2014 8:57:00 PM', 'overland'), ('2014471660', 'disarming-a-peace-officer', '9/6/2014 5:11:00 PM', 'platt-park'), ('2014490308', 'disarming-a-peace-officer', '9/16/2014 11:38:00 AM', 'westwood'), ('2015158108', 'disarming-a-peace-officer', '3/22/2015 7:20:00 PM', 'cbd'), ('201540255', 'disarming-a-peace-officer', '1/20/2015 6:45:00 PM', 'hilltop'), ('2015322253', 'disarming-a-peace-officer', '6/10/2015 3:10:00 PM', 'union-station'), ('2016277324', 'disarming-a-peace-officer', '5/4/2016 12:05:00 PM', 'union-station'), ('2016364649', 'disarming-a-peace-officer', '6/10/2016 6:46:00 PM', 'cbd'), ('201681206', 'disarming-a-peace-officer', '2/7/2016 11:10:00 PM', 'five-points'), ('2017160398', 'disarming-a-peace-officer', '3/10/2017 5:49:00 AM', 'clayton'), ('201875361', 'disarming-a-peace-officer', '1/31/2018 9:45:00 PM', 'hampden-south'), ('201766425', 'disarming-a-peace-officer', '1/30/2017 11:31:00 AM', 'west-colfax'), ('2018471465', 'disarming-a-peace-officer', '7/12/2018 11:50:00 AM', 'auraria'), ('2018293321', 'disarming-a-peace-officer', '5/2/2018 10:22:00 AM', 'platt-park'), ('201647850', 'disarming-a-peace-officer', '1/23/2016 10:52:00 AM', 'south-park-hill'), ('2015663559', 'disarming-a-peace-officer', '11/12/2015 7:40:00 PM', 'chaffee-park'), ('2015350883', 'disarming-a-peace-officer', '6/23/2015 9:40:00 PM', 'cbd'), ('2017342681', 'disarming-a-peace-officer', '5/26/2017 7:01:00 PM', 'five-points'), ('2017320194', 'disarming-a-peace-officer', '5/17/2017 5:35:00 AM', 'cbd'), ('2017797594', 'disarming-a-peace-officer', '11/28/2017 8:25:00 PM', 'five-points'), ('2018553595', 'disarming-a-peace-officer', '8/14/2018 2:48:00 PM', 'cbd')] >>> >>> crimes_by_code_extension(7099, 5) [] >>> 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Draw a labelled diagram of the Dicot stem.

Answered: 1 week ago