In the above code, the display_all_products
function multiplies each item in the “items” list with all the other elements.
The outer loop iterates through each item, and then for each item in the outer loop, the inner loop iterates over each item. This makes total number of steps n x n
where n
is the number of items in the “items” list.
If you plot the inputs and the output steps, you should see the graph
for a quadratic equation as shown below. Here is the output:

xxxxxxxxxx
14
import numpy as np
import matplotlib.pyplot as plt
num_of_inputs = np.array([1,2,3,4,5,6,7])
steps = [pow(n,2) for n in num_of_inputs]
plt.plot(num_of_inputs, steps, 'r')
plt.xlabel('Number of inputs')
plt.ylabel('Number of steps')
plt.title('O(n^2) Complexity')
plt.show()
# No matplotlib support in this terminal.
# Go to the next screen for the results!
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment