Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

Describe the level surfaces of g(x, y, z) = x2 + Y2 z2 for c = 0

Answered: 1 week ago

Question

Why are employees considering union representation?

Answered: 1 week ago

Question

What is the total annual turnover rate?

Answered: 1 week ago