I'm working on a python3 lab that requires me to write a python code that generates datapoints. There also needs to be at least 200 different points for each line. Use sizes like 10000.
However, I keep getting an error stating that "unsupported operand type(s) for *: 'map' and 'float'" caused by the underlined portion of code below. I understand that numpy doesn't work with map() on python3 and that I'll have to change "total_steps" into a list but I haven't been able to find a solution that works.
#!/usr/local/python3
import random
import matplotlib.pyplot as plt
import numpy as np
from decimal import Decimal
def partition(arr, low, high):
i = (low-1)
pivot = arr[high]
for j in range(low, high):
if arr[j] <= pivot:
i = i+1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return (i+1)
def quickSort(arr, low, high, step):
if len(arr) == 1:
return step
if low < high:
pi = partition(arr, low, high)
step += quickSort(arr, low, pi-1, step)
step += quickSort(arr, pi+1, high, step)
step += 1
return step
def gen(size):
return [random.randrange(10000) for i in range(size)]
def to_decimal(l):
return [Decimal(i) for i in l]
sizes = sorted(random.sample(range(200, 10001), 20))
print(sizes)
total_steps = []
for size in sizes:
datapoints = gen(size)
total_steps.append(quickSort(datapoints, 0, size-1, 0))
print(total_steps)
fig = plt.figure()
ax = plt.axes()
sizes = np.array(sizes)/10000
total_steps = np.array(map(Decimal,total_steps))*0.00001
--------------------------------------------------------
plt.plot(sizes, to_decimal(total_steps))
plt.show()