Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a python code to generate following output: a ) GENERATE 5 0 0 0 RANDOM and unique numbers in separate text file AND SORT
Write a python code to generate following output:
a GENERATE RANDOM and unique numbers in separate text file AND SORT THEM
b Your code should Generate a RANDOM NUMBER and find out where does this generated number lies between in generated text file
Find out the time it takes to generate a random number in question b
Example : If txt file has these numbers serially if your generated number is it should says lies between and in txt file.
Please make sure your code should not generate random number like this lies between and in the file. The number should be unique it should not itself include the number
Please modify the below code:
import random
import time
import bisect
# a Generate unique random numbers and write them to a sorted text file
def generateuniquerandomnumbersfilename numnumbers:
with openfilenamew as file:
uniquenumbers set
while lenuniquenumbers numnumbers:
uniquenumbers.addrandomrandint # Adjust range as needed
sortednumbers sorteduniquenumbers
file.writelinesstrnum
for num in sortednumbers
# b Generate a random number and find its position in the text file
starttime time.time
def findrandomnumberpositionfilename:
with openfilenamer as file:
numbers intlinestrip for line in file
#starttime time.time
randomnumber random.randint # Adjust range to match file
#endtime time.time
# generationtime endtime starttime
index bisect.bisectleftnumbers randomnumber
if index :
printfrandomnumber is less than or equal to the first number in the file: numbers
elif index lennumbers:
printfrandomnumber is greater than or equal to the last number in the file: numbers
else:
printfrandomnumber lies between numbersindex and numbersindex in the file."
endtime time.time
generationtime endtime starttime
printfTime taken to generate random number: generationtime:f milliseconds"
# Example usage
filename "randomnumbers.txt
generateuniquerandomnumbersfilename
findrandomnumberpositionfilename
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started