Page 1 of 1

8 8 9 10 11 12 13 14 import numpy as np import time #import matplotlib.pyplot to enable plotting histogram import matplo

Posted: Wed Apr 27, 2022 3:12 pm
by answerhappygod
8 8 9 10 11 12 13 14 Import Numpy As Np Import Time Import Matplotlib Pyplot To Enable Plotting Histogram Import Matplo 1
8 8 9 10 11 12 13 14 Import Numpy As Np Import Time Import Matplotlib Pyplot To Enable Plotting Histogram Import Matplo 1 (61.34 KiB) Viewed 38 times
8 8 9 10 11 12 13 14 Import Numpy As Np Import Time Import Matplotlib Pyplot To Enable Plotting Histogram Import Matplo 2
8 8 9 10 11 12 13 14 Import Numpy As Np Import Time Import Matplotlib Pyplot To Enable Plotting Histogram Import Matplo 2 (40.48 KiB) Viewed 38 times
8 8 9 10 11 12 13 14 Import Numpy As Np Import Time Import Matplotlib Pyplot To Enable Plotting Histogram Import Matplo 3
8 8 9 10 11 12 13 14 Import Numpy As Np Import Time Import Matplotlib Pyplot To Enable Plotting Histogram Import Matplo 3 (31.1 KiB) Viewed 38 times
8 8 9 10 11 12 13 14 Import Numpy As Np Import Time Import Matplotlib Pyplot To Enable Plotting Histogram Import Matplo 4
8 8 9 10 11 12 13 14 Import Numpy As Np Import Time Import Matplotlib Pyplot To Enable Plotting Histogram Import Matplo 4 (69.11 KiB) Viewed 38 times
8 8 9 10 11 12 13 14 Import Numpy As Np Import Time Import Matplotlib Pyplot To Enable Plotting Histogram Import Matplo 5
8 8 9 10 11 12 13 14 Import Numpy As Np Import Time Import Matplotlib Pyplot To Enable Plotting Histogram Import Matplo 5 (84.37 KiB) Viewed 38 times
Please fill in the missing code so that the output will match
the images below. Please do this in python.
import numpy as np
import time
#import matplotlib.pyplot to enable plotting histogram
import matplotlib.pyplot as plt
def getRandomInts(min, maxPlusOne, numInts):
"""Assumes the following:
min is an int which specifies the
smallest random int to generate
maxPlusOne is an int which specifies
one more than largest random int to generate
numInts is an int which specifies how
many random ints to generate
Returns the list of random ints
"""
#start with empty list of random ints
randInts = []

#use for loop to iterate to get required number of
random ints
for i in range(numInts):
newInt = np.random.randint(min,
maxPlusOne)
#append newInt to randInts
list
randInts.append(newInt)
return randInts
def gcdIterative(a, b):
"""Assumbes a and b are ints
Implements loop to determine gcd of a
and b
Returns the gcd and numIterations
required to determine this
"""
#keep track of numIterations required
numIterations = 0
#initialize remainder to zero
remainder = 0
#implement loop to compute gcd
while (b != 0):
remainder = a % b
a = b
b = remainder
numIterations += 1
#after exiting loop return a and
numIterations
return a, numIterations
def displayListSubset(oneList, subsetSize):
""" Assumes onelist is a python list
subsetSize is a positive int that
specifies how many elements of the list to display
"""
#set up a list to contain the subset
listSubset = []
for i in range(subsetSize + 1):

listSubset.append((oneList))
print(listSubset)
def getGcdIterativeList(listA, listB):
"""Assumes listA and listB are equivalently sized
python lists of ints in
Implements iterative gcd method to
determine gcd of each pair
Returns the following:
execution time
required
list of gcd
values
list of num iterations
required
"""
#initialize list of gcd vals
gcdList = []
numIterations = []
#get start time
startTime = time. time()
#iterate through the lists and determine gcd of each
pair
for i in range(len(listA)):
curGcd, numIt = gcdIterative(listA,
listB)
#append this gcd to gcdList
gcdList.append(curGcd)
#append this numIterations to the
numIterations list
numIterations.append(numIt)
endTime = time.time()
itExeTime = endTime -
startTime
#return exe time, gcdList and
numIterations list
return itExeTime, gcdList,
numIterations

MIN = 2
MAX_PLUS_ONE = 51
NUM_INTS = 500000
NUM_DISPLAY = 10
8 8 9 10 11 12 13 14 import numpy as np import time #import matplotlib.pyplot to enable plotting histogram import matplotlib.pyplot as plt def getRandomInts(min, maxPlusOne, numInts): "Assumes the following: min is an int which specifies the smallest random int to generate maxPlusOne is an int which specifies one more than Largest random int to generate numInts is an int which specifies how many random ints to generate Returns the list of random ints 15 AF AF EN #start with empty list of random ints randInts = [] = #use for loop to iterate to get required number of random ints for i in range(numInts): newInt = np.random.randint(min, maxPlusOne) #append newInt to randInts list randInts.append(newInt) return randInts 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 def gedIterative(a, b): "Assumbes a and b are ints Implements Loop to determine god of a and b Returns the gcd and numIterations required to determine this AFF #keep track of numIterations required numIterations = 0 #initialize remainder to zero remainder = 0 #implement loop to compute god while (b != %): remainder = a % b a = b b = remainder numIterations += 1 #after exiting loop return a and numIterations return a, numIterations AF AT def displayListSubset (onelist, subsetSize): "" Assumes onelist is a python List subsetSize is a positive int that specifies how many elements of the list to display ar arar #set up a list to contain the subset listsubset = [] for i in range (subsetSize + 1): listsubset.append((oneList)) print(listsubset) def getGcdIterativeList(lista, listB): ***"Assumes Lista and ListB are equivalently sized python Lists of ints in Implements iterative ged method to determine god of each pair Returns the following:
Returns the following: execution time required List of god values List of num iterations required AF AF AF #initialize list of god vals gcdList = [] numIterations = [] 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 #get start time startTime = time. time() #iterate through the lists and determine gcd of each pair for i in range( len(lista)): curGcd, numIt = gcdIterative(lista, listB) #append this ged to gcdList gcdList.append(curGcd) #append this numIterations to the numIterations list numIterations.append(numit) endTime = time.time) itExeTime = endTime - startTime #return exe time, godlist and numIterations list return itExeTime, godlist, numIterations MIN = 2 MAX_PLUS_ONE = 51 = NUM_INTS = 500000 NUM_DISPLAY = 10 #plot the histogram for the number of iterations plt.figure() bins = 10 plt.hist(itNumit, bins, facecolor = 'green', label = 'iterative') plt.legend (loc = 'best') plt.title( "Number of iterations for iterative gcd function") plt.xlabel("Number of iterations required") plt.ylabel( "Num occurrences of each iteration value") 106
U 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 #missing steps: #declare constants (slide 4 of the HowTo-Lab11-cs112.pdf file) #call the getRandom Ints() function to get lista #call the getRandom Ints() function to get listB #call the getGcdIterativeList(lista, listB) to get the following: #itExtīime, gcdList, it NumIt #call the displayListSubset() to display first ten elements of lista #call the displayListSubset() to display first ten elements of listB #call the displayList Subset() to display first ten elements of the gcdList #call the displayListSubset() to display first ten elements of itNumIt list #plot the histogram for the number of iterations plt. figure() bins = 10 plt.hist(itNumIt, bins, facecolor = 'green', label = 'iterative') plt.legend(loc = 'best') plt.titled "Number of iterations for iterative gcd function") plt.xlabel("Number of iterations required") plt.ylabel( "Num occurrences of each iteration value")
. Call the getRandomInts() function two times, to get listA and listB Call the getGcdIterativeList() function to get the execution time, the list of god values and the list of number of iterations required Display subsets of all four lists (output should be similar to output you have been given) • Plot the histogram of the number of iterations required
Lab 11 Program output 140000 Number of iterations for iterative gcd function iterative First 10 elements of listA: [32, 28, 2, 12, 29, 12, 13, 15, 15, 35, 24] 120000 100000 First 10 elements of listB: [2, 49, 38, 2, 28, 3, 40, 30, 3, 14, 491 80000 First 10 elements of iterative gcdList: [2, 7, 2, 2, 1, 3, 1, 15, 3, 7, 1] Num occurrences of each iteration value JL. 60000 40000 First 10 elements of itNumIt: [1, 4, 2, 1, 2, 1, 3, 2, 1, 2, 3] 20000 Iterative method execution time: 0.4279 0 7 8 Number of iterations required