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. Im 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
MAXPLUSONE
numInts
NUMDISPLAY
def getRandomIntsMIN 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 rangenumInts:
newInt nprandom.randintMIN MAXPLUSONE
#append newInt to randInts list
randInts.appendnewInt
return randInts
def gcdIterativeab:
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
#initialize remainder to zero
remainder
#implement loop to compute gcd
whileb:
remainder a b
a b
b remainder
numIterations
#after exiting loop return a and numIterations
return a numIterations
def displayListSubsetoneList 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 rangesubsetSize :
listSubset.appendoneListi
printlistSubset
def getGcdIterativeListlistA 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 rangelenlistA:
curGcd, numIt gcdIterativelistAi listBi
#append this gcd to gcdList
gcdList.appendcurGcd
#append this numIterations to the numIterations list
numIterations.appendnumIt
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
pltfigure
bins
plthistitNumIt bins, facecolor 'green', label 'iterative'
pltlegendloc 'best'
pltxlabelNumber of iterations required'
pltylabelNum occurences of each iteration value'
plttitleNumber of iterations for iterative gcd function'
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
