Question

Write a function crimes_by_code(code,dict) that takes an integer value code and a dictionary dict containing the...

Write a function crimes_by_code(code,dict) that takes an integer value code and a dictionary dict containing the statistics in the crime codes as arguments. This function returns a list of all the crimes with the given code that have occurred in the database. See the sample output for an example.

Sample run:
crimes_by_code(110, dict) returns

[['Theft of Vehicle', '2005', '2', '12', '22', '30', '', 'Victoria-Fraserview', '495178', '5452000'], ['Theft of Vehicle', '2003', '6', '5', '17', '0', '10XX ALBERNI ST', 'West End', '491059.4824', '5459122.418'], ['Theft of Vehicle', '2003', '9', '26', '8', '30', '10XX ALBERNI ST', 'West End', '491059.4824', '5459122.418'], ['Theft of Vehicle', '2004', '7', '24', '11', '15', '10XX ALBERNI ST', 'West End', '491059.4824', '5459122.418'], ['Theft of Vehicle', '2004', '8', '24', '21', '0', '10XX ALBERNI ST', 'West End', '491059.4824', '5459122.418'],...]

So for this question the "code" which in the example is 110 comes from a json file that in the previous question was sorted into a dictionary that looks like this:

{'101': 'Break and Enter Commercial', '102': 'Break and Enter Residential/Other', '103': 'Vehicle Collision or Pedestrian Struck (with Fatality)', '104': 'Vehicle Collision or Pedestrian Struck (with Injury)', '105': 'Homicide', '106': 'Mischief', '107': 'Offence Against a Person', '108': 'Other Theft', '109': 'Theft from Vehicle', '110': 'Theft of Vehicle', '111': 'Theft of Bicycle'}

And "dict" are the types of the corresponding crime code. The rest of the information (ie. '2005', '2', '12', '22', '30', '', 'Victoria-Fraserview', '495178', '5452000) will come from a previous function with output in the format of a 2D list:

[['Break and Enter Commercial', '2012', '12', '14', '8', '52', '', 'Oakridge', '491285', '5453433'], ['Break and Enter Commercial', '2019', '3', '7', '2', '6', '10XX SITKA SQ', 'Fairview', '490612.9648', '5457109.822'], ....] (This is only a part of it, it's really long)

So all in all the function should take the code and dict value from a previous function which saved the values to a dictionary and use it to get the rest of the information from another previous function which saved the information to a 2D list.

Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<python code>>>>>>>>>>>>>>>>>>>>>>>>

crime_code={'101': 'Break and Enter Commercial', '102': 'Break and Enter Residential/Other', '103': 'Vehicle Collision or Pedestrian Struck (with Fatality)', '104': 'Vehicle Collision or Pedestrian Struck (with Injury)', '105': 'Homicide', '106': 'Mischief', '107': 'Offence Against a Person', '108': 'Other Theft', '109': 'Theft from Vehicle', '110': 'Theft of Vehicle', '111': 'Theft of Bicycle'}

prev_output=[['Break and Enter Commercial', '2012', '12', '14', '8', '52', '', 'Oakridge', '491285', '5453433'], ['Break and Enter Commercial', '2019', '3', '7', '2', '6', '10XX SITKA SQ', 'Fairview', '490612.9648', '5457109.822'],]

def crimes_by_code(num, crime_code):
#creating a empty list to record crimes by code given
crimes=[]
#change prev_output to your returned list by the previous function
for i in prev_output:
#if value of dictionary key matches with 1st value of output list of previous function
#append that crime to crime list
if str(i[0]) in crime_code.values():
crimes.append(i)
#return list of crime commited by code
return crimes

crimes_by_code(101, crime_code)

<<<<<<<<<<<<<<<<<<<<<<<<<<<<sample O/P>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Add a comment
Know the answer?
Add Answer to:
Write a function crimes_by_code(code,dict) that takes an integer value code and a dictionary dict containing the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function crimes_by_code(code,dict) that takes an integer value code and a dictionary dict containing the...

    Write a function crimes_by_code(code,dict) that takes an integer value code and a dictionary dict containing the statistics in the crime codes as arguments. This function returns a list of all the crimes with the given code that have occurred in the database. See the sample output for an example. Sample run: crimes_by_code(110, dict) returns [['Theft of Vehicle', '2005', '2', '12', '22', '30', '', 'VictoriaFraserview', '495178', '5452000'], ['Theft of Vehicle', '2003', '6', '5', '17', '0', '10XX ALBERNI ST', 'West End', '491059.4824',...

  • Write a function called filter_crime_stats(stats,param,x,y) that takes a 2D list of crime stats, a string argument...

    Write a function called filter_crime_stats(stats,param,x,y) that takes a 2D list of crime stats, a string argument param from the set {YEAR, MONTH, DAY}, an integer start value x and an integer end value y as arguments, and returns the 2D list of crime stats containing the crime type, year, month, day, and neighbourhood of all crimes reported between the start value and the end value of the {YEAR/MONTH/DAY}. The value of param is not case sensitive. For example, Year, YEAR,...

  • Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state...

    Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state codes and state names. The file has four columns and they are separated by commas. The first column is the state full name and the second column is the state code. You don't have to worry about column 3 & 4. You should eliminate any row that is without a state code. Save the two columns into a dictionary with key = state code...

  • Problem 1 (Reading Statistics) Write a function called read_stats(filename) that takes a csv (comma separated values)...

    Problem 1 (Reading Statistics) Write a function called read_stats(filename) that takes a csv (comma separated values) filename as an argument and returns a 2D list containing the statistics in that file. The returned list should contain lists of stats for each crime reported and should not include the header line from the file. The file crime_in_vancouver.csv (included in the folder) has the following structure: • The first row provides headers for each column • Each subsequent row represents the stats...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT