Question: from random import randint # creating a random number to store number of numbers number _ of _ numbers = randint ( 5 0 ,

from random import randint
# creating a random number to store number of numbers
number_of_numbers = randint(50,55)
# a) writing to a file
with open('numbers_file.txt','w') as file: # opening file
for i in range(number_of_numbers):
# generating a random number in range 0-100
random_number = randint(0,100)
# inserting it into the file
file.write(str(random_number)+'
')
# b) read the numbers from the file into a list
# list to store the numbers
list_of_numbers =[]
with open('numbers_file.txt','r') as file: # opening file
lines = file.readlines()
for number in lines:
list_of_numbers.append(int(number))
# c) sort and show the list
print('List of numbers before sorting:')
print(list_of_numbers)
length = len(list_of_numbers)
# traverse through all elements
for i in range(length):
# last i elements are already sorted
for j in range(0, length - i -1):
# traverse the array from 0 to length - i -1
# swap if element is greater than next element
if list_of_numbers[j]> list_of_numbers[j +1]:
list_of_numbers[j], list_of_numbers[j +1]= list_of_numbers[j +1], list_of_numbers[j]
print('List of numbers after sorting:')
print(list_of_numbers)
# d) calculating the median
if length %2==1:
median = list_of_numbers[length //2]
else:
median =(list_of_numbers[length //2-1]+ list_of_numbers[length //2])/2
print('The median of the numbers is:', median)

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 Databases Questions!