In: Computer Science
how to creat Create a text file that contains a list of integer numbers separated by comma (numbers.txt). E.g. 3, 7, 5, 1, 11, 8,2, 6 2- how to Write a python program (minMax.py) to read the file numbers.txt and find the largest and smallest numbers in the file. (without using use min(), max()). calculate the average and output the results. 3-Use python file i/o, write a python program (fileCopy.py) that makes a copy of minMax.py (Read contents of minMax.cp and save it to minMaxCopy .py) so that each time when the program is run, a different copy is created. You may want to use datetime module to get the current time. E.g. minMaxCopy20201009072003.py was created on 10/09/2020 3seconds past 7:20
Text format:
import sys f = open('numbers.txt') min = sys.maxsize max = -sys.maxsize-1 sum = 0 n = 0 while True: line = f.readline() if line == '': break ls = line.split(',') for i in range(len(ls)): x = int(ls[i].replace(' ', '').replace('\n', '')) if x < min: min = x if x > max: max = x sum += x n+=1 print("Smallest number:", min) print("Largest number:", max) print("Average:", float(sum)/n)
Output:
Text format:
import datetime now = datetime.datetime.now() f1 = open('minMax.py') filename = 'minMaxCopy' + str(now.year) + str(now.month) + str(now.day) + str(now.hour) +\ str(now.minute) + str(now.second) + '.py' f2 = open(filename, 'w') ls = f1.readlines() for i in ls: f2.write(i) f2.close() Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason. Always avoid copying from existing answers to avoid plagiarism. Thank you.