Materials for Part of the Catalogue

Matrix MultiplicationMedium+20 XP+20

Materials for Part of the Catalogue

A furniture workshop supplies several showrooms and plans next month's materials with two tables:

  • orders[s][p]: pieces of product p ordered by showroom s (one row per showroom, one column per product);
  • materials[p][g]: amount of material g (metres of timber, litres of varnish, ...) that one piece of product p uses (one row per product, one column per material).

The matrix product orders ×\times× materials is a showrooms ×\times× materials table: how much of each material every showroom's whole order needs.

The buyers want something finer: the materials needed for only some of the products, for example how much varnish goes into the chairs and the shelves alone, showroom by showroom.

A useful tool here is the outer product. For a column u\mathbf{u}u with mmm entries and a row v\mathbf{v}v with kkk entries, the outer product uv\mathbf{u}\mathbf{v}uv is the m×km \times km×k table whose entry in row iii, column jjj is uivju_i v_jui​vj​: every entry of u\mathbf{u}u times every entry of v\mathbf{v}v.

Task: write catalogue_demand(orders, materials, products) returning the showrooms ×\times× materials table needed to make just the products whose indices are listed in products (no repeats, any order), as a list of rows with every entry rounded to 4 decimal places.

  • If products is empty, nothing is made: return a table of zeros of the right shape.
  • If products lists every product, the answer is the full product orders ×\times× materials.

Examples

orders = [[10, 4, 6, 2], [3, 8, 5, 0], [7, 2, 9, 5]]
materials = [[1.2, 0.8], [1.5, 0.4], [2.5, 0.1], [0.9, 0.6]]
products = [0, 1]
returns [[18.0, 9.6], [15.6, 5.6], [11.4, 6.4]]
orders = [[10, 4, 6, 2], [3, 8, 5, 0], [7, 2, 9, 5]]
materials = [[1.2, 0.8], [1.5, 0.4], [2.5, 0.1], [0.9, 0.6]]
products = [0, 1, 2, 3]
returns [[34.8, 11.4], [28.1, 6.1], [38.4, 10.3]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run