Answered step by step
Verified Expert Solution
Link Copied!

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 5000 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 6356,6360 serially , if your generated number is 6357, it should says 6357 lies between 6356 and 6360 in txt file.
Please make sure your code should not generate random number like this (2421 lies between 2419 and 2421 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 5000 unique random numbers and write them to a sorted text file
def generate_unique_random_numbers(filename, num_numbers):
with open(filename,"w") as file:
unique_numbers = set()
while len(unique_numbers)< num_numbers:
unique_numbers.add(random.randint(1,10000)) # Adjust range as needed
sorted_numbers = sorted(unique_numbers)
file.writelines(str(num)+"
" for num in sorted_numbers)
# b) Generate a random number and find its position in the text file
start_time = time.time()
def find_random_number_position(filename):
with open(filename,"r") as file:
numbers =[int(line.strip()) for line in file]
#start_time = time.time()
random_number = random.randint(1,10000) # Adjust range to match file
#end_time = time.time()
# generation_time = end_time - start_time
index = bisect.bisect_left(numbers, random_number)
if index ==0:
print(f"{random_number} is less than or equal to the first number in the file: {numbers[0]}")
elif index == len(numbers):
print(f"{random_number} is greater than or equal to the last number in the file: {numbers[-1]}")
else:
print(f"{random_number} lies between {numbers[index -1]} and {numbers[index]} in the file.")
end_time = time.time()
generation_time =(end_time - start_time)*1000
print(f"Time taken to generate random number: {generation_time:.6f} milliseconds")
# Example usage
filename = "random_numbers.txt"
generate_unique_random_numbers(filename,5000)
find_random_number_position(filename)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago