Concurrency · 11 / 15 Exercise Medium

Splitting work into chunks

Objective

To take advantage of several processors you have to divide the work into independent chunks, process them separately and combine the results (map-reduce). Real threads cannot be created in the browser, so the program simulates the situation in a deterministic way.

Instructions

  • Read the number of workers k and a line with a list of integers.
  • Split the list into k consecutive chunks as equal as possible: with n elements, each chunk has n // k and the first n % k chunks get one more.
  • Each worker computes the sum of the squares of its chunk. Show Worker i: [chunk] -> sum (the chunk is shown as a Python list; it may be empty).
  • At the end show Total: t, the sum of the results of all the workers.
Input
3
1 2 3 4 5 6 7
Output
Worker 1: [1, 2, 3] -> 14
Worker 2: [4, 5] -> 41
Worker 3: [6, 7] -> 85
Total: 140

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise