Skip to contentKEEP THE CRAFTTHE SANCTUARY

Group Integers into Ordered Buckets

Split integers into equal-width buckets, sort each bucket, and join the buckets in order.

The problem

Implement bucket_sort(values, bucket_width). values is a list of integers that may include negatives and duplicates, and bucket_width is a positive integer. Split the values into buckets that each cover bucket_width consecutive integers, sort each bucket on its own, and join the buckets from the lowest range to the highest. Return a new ascending list. You may call sorted on a single bucket, but not on the whole input. For example, bucket_sort([7, -1, 3, -4, 8], 3) returns [-4, -1, 3, 7, 8].

Starter code

def bucket_sort(values, bucket_width):
    pass

Public examples

  1. values = [7, -1, 3, -4, 8], bucket_width = 3gives[-4, -1, 3, 7, 8]
  2. values = [3, 1, 2], bucket_width = 10gives[1, 2, 3]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.