Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use PYTHON In this assignment, you'll be exploring the database of Crime in Denver contained in this zip file. The database contains two files the

Use PYTHON

In this assignment, you'll be exploring the database of Crime in Denver contained in this zip file. The database contains two files

the zip is in the https://drive.google.com/file/d/1h6NLEH9AAef8ix5TP5UO4QNo0ttoZfEr/view?usp=sharing

  1. offence_codes.csv: Is a CSV file that contains offence codes and information about each code

  2. crime.csv: Is a CSV file containing information about 455872 crimes reported in Denver, mostly occurring in 2018.

Begin by downloading a4.py which contains skeleton code that opens and reads the CSV files line-by-line, with an obvious place for you to add your code.

Questions

For any clarification about these questions, refer to the sample output below.

  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.
  2. Write a function code_from_keywords(keywords) that an a list of strings and returns a list of all the crime codes that include all the keywords in their description
  3. 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 he crime id, the abbreviated crime name, the date and time, and neighbourhood id of the crime. See the sample output for an example.
  4. Write 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. It's the code approved.image text in transcribed

    Sample Output

     >>> 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) [] >>> 
1 import csv 3 4 # Question 1 5 6 def code_to_names (code): 7 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: 9 12 pass # Your code goes here 13 return names 15 # 16 # Question 2 18 def codes_from_keywords (keywords): 19 28 codelist-[ with open('offense_codes.csv') as csv file: readercsv.reader(csv_file, delimiter-',) next(reader) # skip the first row for row in reader: 24 25 26 pass return codelist 28 # Question 3 e def crimes_by_code(code): 31 32 33 34 # Question 4 return [] # Your code goes here 36 def crimes_by_code_extension(code, extension): 37 38 39 48 41 42 43 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 a4.py 1:1 LF UTF-8 Python GitHub - Git (0) GitHub Git (O)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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