Question

Problem 5 (Filtering Crime Statistics) Write a function called filter_crime_stats(stats,param,x,y) that takes a 2D list of...

Problem 5 (Filtering Crime Statistics)

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, year, YeAr etc. all should be evaluated to be the same value. You must validate user’s input for param and return appropriate results. If the crime is not found in the given list of stats, your function should return an empty list. See the sample output for an example.

Sample run:

filter_crime_stats(stats,'location',4500,56000) returns

Invalid parameter

filter_crime_stats(stats,'month',13,20) returns []

filter_crime_stats(stats,'year',2007,2008) returns

[['Break and Enter Commercial', '2008', '10', '3', 'Central Business District'],['Break and Enter Commercial', '2007', '8', '14', 'Kitsilano'],['Vehicle Collision or Pedestrian Struck (with Fatality)', '2007', '10', '14', 'Marpole'],['Break and Enter Commercial', '2007', '6', '11', 'Grandview-Woodland'],['Break and Enter Residential/Other', '2007', '11', '15', 'West End'],['Break and Enter Residential/Other', '2007', '6', '6', 'West End'],..]

In Python

Make sure the program is available. Please upload the complete program picture and program content.

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

code :

output :

raw_code :

def filter_crime_stats(stats,param,x,y):
out = []
param = param.lower() #converting to lower case
if(param not in ['year','month','day']): #validating param
return print('Invalid parameter')
  
  
for i in stats:
if(param == 'year'):
if(int(i[1]) >= x and int(i[1]) <=y): #checking x and y
out.append(i)
elif(param == 'month'):
if(int(i[2]) >= x and int(i[2]) <=y):
out.append(i)
elif(param == 'day'):
if(int(i[3]) >= x and int(i[3]) <=y):
out.append(i)
print (out) #printing output


#sample run

stats = [['Break and Enter comericial','2008','10','3','Central Business District'],
['Break and Enter Comericial','2007','8','14','Kitsilano'],
['Vehicle collision or pedestrian struck ','2009','10','14','Marpole']]

  
filter_crime_stats(stats,'location',4500,56000)
filter_crime_stats(stats,'month',13,20)
filter_crime_stats(stats,'year',2007,2008)

Add a comment
Know the answer?
Add Answer to:
Problem 5 (Filtering Crime Statistics) Write a function called filter_crime_stats(stats,param,x,y) that takes a 2D list of...
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
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