\(\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}\)
What order is best?¶
Here I have a list of 4 numbers:
>>> my_numbers = [-1, 5, 10, 21]
Here I have another list of 4 numbers:
>>> your_numbers = [3, 0, -10, 15]
Here is a function, that takes each element in the two lists, multiplies them, and retuns the sum.
>>> def list_product(first_list, second_list):
... product = 0
... for i in range(len(first_list)):
... value = first_list[i] * second_list[i]
... product = product + value
... return product
Let’s see the list_product
for our two lists:
>>> list_product(my_numbers, your_numbers)
212
Now - here is your problem. You can shuffle the second list,
your_numbers
, in any way you like. What is the right order for the
numbers in your_numbers
, to give the highest value for the
list_product
?
For example, here I’m doing a random shuffle of the list:
>>> #: The random module
>>> import random
>>> random.shuffle(your_numbers)
>>> your_numbers
[15, 0, -10, 3]
And I calculate the list product again:
>>> list_product(my_numbers, your_numbers)
-52
See if you can find the best ordering to give the highest sum.
If you’ve done that, see if you can find the best ordering for the lowest sum.