Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Edit the code below so that: (assignment requirement are below) 1. in your out100a.txt file is only 10 values. 94 548 777 1669 2196 3432
Edit the code below so that: (assignment requirement are below)
1. in your out100a.txt file is only 10 values. 94 548 777 1669 2196 3432 7834 9070 9186 9472 Must be 100 values.
2. Also your program must not ask to enter input filename. It must take input from in10.txt and in100.txt files without asking input filenames.
---------
def first_sorting(input_filename, output_filename): try: infile = open(input_filename, 'r') #Open the input file in read mode outfile = open(output_filename, 'w') #Open the output file in write mode #Read the first line in file and remove the trailing newline and convert to integer k = int(infile.readline().strip()) #Initialize array of size k A = [] #Creating empty array for i in range(0, k): A.append(0) #Adding zero to all array elements #Reading next line in file which is the number list number_line = infile.readline().strip() #Read the line and remove the training newline #Sorting input numbers for num in number_line.split(): #splitting th line to get the numbers number = int(num) #Convert the number from file to integer A[number-1] = A[number-1] + 1 #Add one to array element at appropriate index #Writing the sorted numbers to output file for index in range(0, k): if A[index] > 0: #If array elemnet id greater than 0 for j in range(0, A[index]): #print the array index, array element times outfile.write(str(index+1)) #write the index to file outfile.write(" ") #write space as seperator to file #close both files infile.close() outfile.close() print("First sorting successfull!!!") except IOError: #Display error if file open/read/write failed and exit from program print("File open/read/write Error!!!") except: print("Sorting Failed!!!") def second_sorting(input_filename, output_filename): try: infile = open(input_filename, 'r') #Open the input file in read mode outfile = open(output_filename, 'w') #Open the output file in write mode #Read the first line in file and remove the trailing newline and convert to integer k = int(infile.readline().strip()) #Reading next line in file which is the number list number_line = infile.readline().strip() #Read the line and remove the training newline #Adding the numbers from file to array A A = [] for num in number_line.split(): #splitting th line to get the numbers number = int(num) #Convert the number from file to integer A.append(number) #Add number yo array A #Sorting the numbers using bubbnle sort n = len(A) #finding the length of array A # Traverse through all array elements for i in range(n-1): # range(n) also work but outer loop will repeat one time more than needed. for j in range(0, n-i-1): # Last i elements are already in place # traverse the array from 0 to n-i-1 # Swap if the element found is greater than the next element if A[j] > A[j+1] : A[j], A[j+1] = A[j+1], A[j] #Writing the sorted array to output file for number in A: outfile.write(str(number)) outfile.write(" ") #close both files infile.close() outfile.close() print("Second sorting successfull!!!") except IOError: #Display error if file open/read/write failed and exit from program print("File open/read/write Error!!!") except: print("Sorting Failed!!!") #Program starts here print("Please type file name (for example in10.txt)") input_filename = input("Enter the input filename: "); #Reading the input filename #Removing the ".txt" and "in" from input_filename X = input_filename.replace(".txt", "") X = X.replace("in","") #Generating the output_filename for first type sorting output_filename = "out" + X + "a.txt" first_sorting(input_filename, output_filename) #calling first_sorting() #Generating the output_filename for second type sorting output_filename = "out" + X + "b.txt" second_sorting(input_filename, output_filename) #calling second_sorting()Objects and Algorithms Exercise 2 Sorting Page 1 of 2 Exercise 2 Sorting Algorithm Assume that you have n integer numbers that >= 1 and = 1 and
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