Question

Write a function called sort_by_years(stats) that takes a 2D list of stats as argument and returns...

Write a function called sort_by_years(stats) that takes a 2D list of stats as argument and returns it sorted in descending order by the YEAR column, i.e., the crimes reported in 2020 will appear first in the returned list, then the crimes reported in 2019, and so on. You may use any of the sorting algorithms covered in class to accomplish this. However, you may not use python's built-in sort() or sorted() function. Note: your function should not sort the input list in-place, but rather return a sorted copy of the list. See the sample output for an example. Sample run: sort_by_years(stats) returns [['Break and Enter Commercial', '2020', '1', '1', '8', '12', '10XX BEACH AVE', 'West End', '490197.4785', '5458239.421'],['Break and Enter Commercial', '2020', '2', '12', '22', '14', '10XX BURNABY ST', 'West End', '490423.396', '5458517.254'],['Break and Enter Commercial', '2020', '2', '8', '9', '0', '14XX E 12TH AVE', 'Kensington-Cedar Cottage', '494531.568', '5456325.349'],['Break and Enter Commercial', '2020', '2', '13', '3', '49', '14XX E BROADWAY AVE', 'GrandviewWoodland', '494606.204', '5456629.4'],…

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

def mergesort(li):
   if len(li)<2:
       return li
   mid=len(li)//2
   l1=li[:mid]
   l2=li[mid:]
   l1=mergesort(l1)
   l2=mergesort(l2)
   return merge(l1,l2)
  
def merge(l1,l2):
   i,j=0,0
   l3=[]
   while i<len(l1) and j<len(l2):
       if int(l1[i][1])<int(l2[j][1]):
           l3.append(l2[j])
           j+=1
       else:
           l3.append(l1[i])
           i+=1
   if i<len(l1):
       l3.extend(l1[i:])
   if j<len(l2):
       l3.extend(l2[j:])
   return l3

def sort_by_years(stats):
   temp = [stats[i].copy() for i in range(len(stats))]
   return mergesort(temp)


stats = [
           ['Break and Enter Commercial', '2020', '1', '1', '8', '12', '10XX BEACH AVE', 'West End', '490197.4785', '5458239.421'],
           ['Break and Enter Commercial', '2020', '2', '12', '22', '14', '10XX BURNABY ST', 'West End', '490423.396', '5458517.254'],
           ['Break and Enter Commercial', '2020', '2', '8', '9', '0', '14XX E 12TH AVE', 'Kensington-Cedar Cottage', '494531.568', '5456325.349'],
           ['Break and Enter Commercial', '2020', '2', '13', '3', '49', '14XX E BROADWAY AVE', 'GrandviewWoodland', '494606.204', '5456629.4']
       ]

print(sort_by_years(stats))

Add a comment
Know the answer?
Add Answer to:
Write a function called sort_by_years(stats) that takes a 2D list of stats as argument and returns...
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