Question

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, 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 [] Page 5 of 7 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'],..]

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'],..]

i am not really sure of what an example of a stat 2d list is but i think this is one

import csv

def read_stats(filename):

    stat = []

    file = open(filename, 'r')

    lines = file.readlines()

    flag = 0

    for i in lines:

        stat.append(i.strip().split(','))

    return stat[1:]

print(read_stats('crime_in_vancouver.csv'))

THE CRIME IN VANCOUVER RETURNS THIS

Sample run: read_stats('crime_in_vancouver.csv') returns

[['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'], ….]

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

Thanks for the question, here is the function code in python. I have attempted to answer this question.

Hope this helps. let me know for any changes or modifications.

thanks !

=================================================================

def filter_crime_stats(stats,param, x, y):
    param = param.lower()
    if param=='year' :
        filtered_list=[]
        for record in stats:
            year = int(record[1])
            if x<=year<=y:filtered_list.append(record)
        return filtered_list
    elif param=='month':
        filtered_list=[]
        for record in stats:
            year = int(record[2])
            if x<=year<=y:filtered_list.append(record)
        return filtered_list
    elif param=='day':
        filtered_list=[]
        for record in stats:
            year = int(record[3])
            if x<=year<=y:filtered_list.append(record)
        return filtered_list
    else:
        return 'Invalid'


stats = [['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']]

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

=================================================================

Code screenshot

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

  • 1.Write a function called high_point that takes a list of integers and returns True if there...

    1.Write a function called high_point that takes a list of integers and returns True if there exists a value in the list that is bigger than the values immediately before and after it in the list. Your function must return False if no such value exists. The values in the beginning and the end of the list cannot be high points. (20 points) Test Cases: print(high_point([2,5,8,9,7,9])) True print(high_point([2,5,6,6,3])) False print(high_point([2,5,8,21,22])) False 2. Write a while loop to repeatedly ask for...

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
ADVERTISEMENT