Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone help me out in writing the codes, just a little bit hard import rand

ID: 3634460 • Letter: C

Question

can someone help me out in writing the codes, just a little bit hard
import random and time
from search import *

Get length of list from the user
Create a list containing the values 0 to length - 1
Shuffle the list with random.shuffle()

Start linear search timer
Loop 1000 times:
Generate a random integer between 0 and length - 1
Call linear search on the list with this random number
Stop linear search timer

Start binary search timer
Sort list with list.sort()
Loop 1000 times:
Generate a random integer between 0 and length - 1
Call binary search on list with random number
Stop binary search timer

Print out resulting times

Explanation / Answer

#!/usr/bin/env python
# This demonstrates a binary search through sorted data using bisect.
# A large array of random numbers is generated and then sorted.
# The the application shows where a given number would be inserted
# in the random data list.

from bisect import bisect
from random import randrange
import sys

# Get the given number from the command line argument.
# If the command line argument does not work then use a random number.
try:
number = int(sys.argv[1])
except:
print 'A number was not given, so a random number will be used.'
number = randrange(10000000)

# Generate a sorted list of 100 thousand random numbers.
print 'Generating sorted random list...'
list = []
for i in range (0,100000):
list.append(randrange(10000000))

# This does all the work.
list.sort()
insert_point = bisect (list, number)

# Show where number would be inserted.
print
print 'list[%d]=%d' % (insert_point - 1, list[insert_point - 1])
print ' > %s goes here <' % (number)
print 'list[%d]=%d' % (insert_point, list[insert_point])

# End.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote