Python Programming: An Introduction to Computer Science, 3rd Ed.
Python Programming: An Introduction to Computer Science, 3rd Ed.
3rd Edition
ISBN: 9781590282755
Author: John Zelle
Publisher: Franklin, Beedle & Associates
Question
Book Icon
Chapter 11, Problem 9PE
Program Plan Intro

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.
  • Call the “main()” function.

Expert Solution & Answer
Check Mark

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()

Sample Output

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:

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 11, Problem 9PE

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
The Binary Search algorithm works by testing a mid-point, then eliminating half of the list. In this exercise, you are going to take our binary search algorithm and add print statements so that you can track how the search executes. Inside of the recursive binary search function, add print statements to print out the starting, ending, and midpoint values each time. Then as you test a value, print out the results, either too high, too low, or a match. Sample Output Starting value: 0 Ending value: 9 Testing midpoint value: 4 Too high! Starting value: 0 Ending value: 3 Testing midpoint value: 1 Too low! Starting value: 2 Ending value: 3 Testing midpoint value: 2 Match!
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 median please only use list_in_1, list_in_2 and return median no other function allowed and return median please use only them and no sort function thank you so much
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 calculation import math def task7(list_in_1, list_in_2):  # YOUR CODE HERE    return median please use list_in_1, list_in_2 and return median  thank you so much
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning