Question: Python! Please help asap. I ' m using Spyder Code to display text output like the picture The final component of this program is to

Python! Please help asap. I'm using Spyder
Code to display text output like the picture
The final component of this program is to write the code needed to
display the required text in the iPython console:
The first ten elements of all four lists
To do this, call the displayListSubset() four times
Note that by writing a function, you can write the code one
time and then use it many time
The execution time, rounded to four decimal places
Compare your programs output to the output seen in the next slide to
verify that you have done this correctly.
``````````````````````
import numpy as np
import time
#import matplotlib.pyplot to enable plotting histogram
import matplotlib.pyplot as plt
MIN=2
MAX_PLUS_ONE =51
numInts =500000
NUM_DISPLAY =10
def getRandomInts(MIN, MAX_PLUS_ONE, 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, MAX_PLUS_ONE)
#append newInt to randInts list
randInts.append(newInt)
return randInts
def gcdIterative(a,b):
"""
Assumes 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[i]))
print(listSubset)
def getGcdIterativeList(listA, listB):
"""
Assumes listA and listB are equivalently sized python lists of ints
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[i], listB[i])
#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
#display output
??????????????
#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.xlabel('Number of iterations required')
plt.ylabel('Num occurences of each iteration value')
plt.title('Number of iterations for iterative gcd function')

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!