Modified gpasort program
Program plan:
- Import the necessary modules in “grade_sort.py” file.
- Define the “make_Student()” function,
- Returns student record values to the caller.
- Define the “read_Students()” function,
- Returns the list of student record to the caller.
- Define the “write_Students()” function,
- Write the student record.
- Define the “sort()” function,
- Create empty list.
- Create “for” loop,
- Assign the data from the file.
- Append the data to the new list using append() method.
- Call the function “sort()” to sort the data.
- Call the function “reverse()” to reverse the data.
- Return new list.
- Define the “main()” function,
- Assign the input file.
- Read the students record from the input file.
- Sort the data by calling “sort()” function.
- Assign the output file.
- Call the function “write_Students()”.
- Call the “main()” function.
- Create a class Student in “gpa.py” file,
- Define the “_init_()” method.
- Assign name hours and GPoints.
- Define the “get_Name()” method.
- Return the name.
- Define the “get_Hours()” method.
- Return hours.
- Define the “getQ_Points()” method.
- Return GPoints.
- Define the “gpa()” method.
- Return gpa
- Define the “make_Student()” method.
- Return name, hours, and grade points.
- Define the “main()” function.
- Define the “get_Name()” method.
- Assign name hours and GPoints.
- Define the “_init_()” method.
- Call the “main()” function.
Explanation of Solution
Program:
File name: “gpa_sort.py”
#Import required module
from gpa import Student
#Define the function make_Student()
def make_Student(info_Str):
#Make multiple assignment
Name, Hours, Gpoints = info_Str.split("\t")
#Return constructor
return Student(Name, Hours, Gpoints)
#Define the function read_Students()
def read_Students(file_name):
#Open the input file for reading
in_file = open(file_name, 'r')
#Create an empty list
Students = []
#Create for loop to iterate over all lines in a file
for line in in_file:
#Append the line in a list
Students.append(make_Student(line))
#Close the input file
in_file.close()
#Return the list
return Students
#Define the function write_Students()
def write_Students(Students, file_name):
#Open output file to write
out_file = open(file_name, 'w')
#Create a for loop to iterate over list
for s in Students:
#Print output
print("{0}\t{1}\t{2}".format(s[1],
s[2], s[3]), file = out_file)
#Close the output file
out_file.close()
#Define the function
def sort(Students):
#Create empty list
new_Data = []
#Create for loop
for i in range(len(Students)):
#Assign tuple
x = (Students[i].gpa(), Students[i].get_Name(),
Students[i].get_Hours(), Students[i].getQ_Points())
#Append the data at the end of new list
new_Data.append(x)
#Call the function "sort()" to sort the data
new_Data.sort()
#Call the function "reverse()" to reverse the data
new_Data.reverse()
#Return new list
return new_Data
#Define main() function
def main():
#Print the string
print("This program sorts student grade information by GPA")
#Assign the file name
file_name = "gpa1.txt"
#Read the data from a file
data = read_Students(file_name)
#Sort the data
data = sort(data)
#Assign the file name
file_name = "gpa_(sort1).txt"
#Call the function
write_Students(data, file_name)
if __name__ == '__main__':
#Call the main() function
main()
File name: “gpa.py”
#Create a class Student
class Student:
#Define _init_() method
def __init__(self, Name, Hours, Gpoints):
self.Name = Name
self.Hours = float(Hours)
self.Gpoints = float(Gpoints)
#Define get_Name() method
def get_Name(self):
#Return the name
return self.Name
#Define get_Hours()
def get_Hours(self):
#return hours
return self.Hours
#Define getQ_Points()
def getQ_Points(self):
#return grade points
return self.Gpoints
#Define the function gpa()
def gpa(self):
#return the value
return self.Gpoints / self.Hours
#Define the function make_Student()
def make_Student(info_Str):
#Make multiple assignment
Name, Hours, Gpoints = info_Str.split("\t")
#Return the constructor
return Student(Name, Hours, Gpoints)
#Define the main() function
def main():
#Open the input file for reading
file_name = input("Enter the name of the grade file: ")
in_file = open(file_name, 'r')
#Set best to the record for the first student in the file
best = make_Student(in_file.readline())
#Process lines of the file using "for" loop
for line in in_file:
#Make the line of file into a student record
s = make_Student(line)
#Checck whether the student is best so far
if s.gpa() > best.gpa():
#Assign the best student record
best = s
#Close the input file
in_file.close()
#Print information about the best student
print("The best student is:", best.get_Name())
print("Hours:", best.get_Hours())
print("GPA:", best.gpa())
if __name__ == '__main__':
#Call the main() function
main()
Contents of “gpa1.txt”
Adams, Henry 127 228
Computewell, Susan 100 400
DibbleBit, Denny 18 41.5
Jones, Jim 48.5 155
Smith, Frank 37 125.33
Output:
This program sorts student grade information by GPA
>>>
Screenshot of output file “gps_(sort1).txt after execution:
Want to see more full solutions like this?
Chapter 11 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- Write a python program that takes two lists, merges the two lists, sorts the resulting list, and then finds the median of the elements in the two lists. [You cannot use python build-in sort() function, you can call your own sort function or copy your code from previous tasks instead] ===================================================== Sample Input 1 list_one = [1, 2, 1, 4] list_two = [5, 4, 1] Sample Output 1 Sorted list = [1, 1, 1, 2, 4, 4, 5] Median = 2 ===================================================== Sample Input 2 list_one = [1, 7, 9, 10] list_two = [2, 7, 6, 5] Sample Output 2 Sorted list = [1, 2, 5, 6, 7, 7, 9, 10] Median = 6.5 #todo#You can use math functions like math.floor to help the calculationimport math def task7(list_in_1, list_in_2):# YOUR CODE HEREreturn medianarrow_forwardWrite a python program that takes two lists, merges thetwo lists, sorts the resulting list, and then finds the median of theelements in the two lists. **<font color='red'>[You cannot use python build-in sort() function, you can call your own sort function or copy your code from previous tasks instead]</font>** ===================================================== **Sample Input 1** list_one = \[1, 2, 1, 4\] list_two = \[5, 4, 1\] **Sample Output 1** Sorted list = \[1, 1, 1, 2, 4, 4, 5\] Median = 2 ===================================================== **Sample Input 2** list_one = \[1, 7, 9, 10\] list_two = \[2, 7, 6, 5\] **Sample Output 2** Sorted list = \[1, 2, 5, 6, 7, 7, 9, 10\] Median = 6.5 this is my code but its not returning median #todo#You can use math functions like math.floor to help the calculation import math def task7(list_in_1, list_in_2): # YOUR CODE HERE list_in_1.extend(list_in_2) sort_list=[] while list_in_1: min=list_in_1[0] for…arrow_forwardWrite a python program that has the following functionality:• Using a loop it creates a two-dimensional list with 3 elements, such that theseelements are empty lists. (i.e., [ [ ], [ ], [ ] ] ).• Using a nested loop your program should store three (3) random integers between 1and 25 (inclusive) in each of these empty lists. A sample list after this step shouldlook like this; [ [4, 17, 25], [7, 7, 16], [21, 17, 5] ]• Using another nested loop your program should multiply each integer stored in thatlist and print the result. For the sample list given above the result is 2379048000.arrow_forward
- implement QuickSort of ints that sorts the numbers in the non-decreasing order and in c language Implement the rearrange function used for QuickSort using the O(n) time algorithm with two pointers. The function gets as input an array, and index of the pivot. The function rearranges the array, and returns the index of the pivot after the rearrangement. please write form as: int rearrange(int* A, int n, int pivot_index); 2, Implement the QuickSort algorithm. For n<=2 the algorithm just sorts the (small) array (smaller number first). - For n>=3 the algorithm uses the rearrange function with the pivot chosen to be the median of A[0], A[n/2], A[n-1]. write as form: void quick_sort(int* A, int n);arrow_forwardWritten in Dr Racket: Write a recursive function that takes a list of numbers as input and returns a list of the numbers in ascending order.Use the quicksort algorithmUse ( 20 13 74 5 12 9 22 95 22 6 101 72 3 53 33 21 96) as input. To do that, calculate the average value of a sublist before dividing it.(quicksort ‘( 20 13 74 5 12 9 22 95 22 6 101 72 3 53 33 21 96))returns ‘(3 5 6 9 12 13 20 21 22 22 33 53 72 74 95 96 101) Do not use sort, quicksort, set!, or mean.arrow_forwardusing python Adjust values in a list by normalizing When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This can be done by normalizing to values between 0 and 1, or throwing away outliers. Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, adjust each integer in the list by subtracting the smallest value from all the integers. Ex: If the input is: 5 30 50 10 70 65 the output is: 20 40 0 60 55 The 5 indicates that there are five integers in the list, namely 30, 50, 10, 70, and 65. The smallest value in the list is 10, so the program subtracts 10 from all integers in the list.arrow_forward
- PYTHON: Give a recursive implement to the following function: def split_by_sign(lst, low, high) The function is given a list lst of non-zero integers, and two indices: low and high (low ≤ high), which indicate the range of indices that need to be considered. The function should reorder the elements in lst, so that all the negative numbers would come before all the positive numbers. Note: The order in which the negative elements are at the end, and the order in which the positive are at the end, doesn’t matter, as long as all he negative are before all the positive. Please type answer no write by hend.arrow_forwardUsing recursion, write a Python function def before(k,A) which takes an integer k and an array A of integers as inputs and returns a new array consisting of all the integers in A which come before the last occurrence of k in A, in the same order they are in A. For example, if A is [1,2,3,6,7,2,3,4] then before(3,A) will return [1,2,3,6,7,2]. If k does not occur in A, the function should return None.arrow_forwardWrite a Python function `removeDuplicates(myList)` that removes duplicate values from a list. While the algorithm for this problem might seem trivial, its implementation is not. In general, it is not advisable to modify a list while you are iterating on it, as in the following pseudo-code: for element in myList: if <condition>: <remove element(s) from myList> # recipe for disasterarrow_forward
- Write a program in Java to implement Quick sort algorithm to sort a given set of elements and determine the time required to sort the elements. Repeat the experiment for different values of n, the number of elements in the list to be sorted and plot a graph of the time taken versus n. The elements can be read from keyboard, a file or can be generated using the random number generator.arrow_forwardInstead of reading in an entire list N1,N2,⋯N1,N2,⋯ all at once, some algorithms (depending on the task to be done) can read in only one element at a time and process that single element completely before inputting the next one. This can be a useful technique when the list is very big (e.g., billions of elements) and there might not be enough memory in the computer to store it in its entirety. Write an algorithm that reads in a sequence of values v≥0, one at a time, and computes the average of all the numbers. You should stop the computation when you input a value of V = −1. Do not include this negative value in your computations; it is not a piece of data but only a marker to identify the end of the list. Write a java program and please share the code / an example of the code working.arrow_forwardNeed help in solving the problem explained in the screenshot in python. Need the time complexity to be a minimum as possible, so using Binary Search would be the best approach. The code def solve(n,k): # solve problem here. Return correct answer n, k = list(map(int,input().rstrip().split(" ")))print(solve(n,k))arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning