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 3PE
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 “main()” function,
    • Get the input file.
    • Read the students record for the input file.
    • Make a “while” loop for “True”.
      • Get the type of input to sort.
      • Get the type of ordering from the user.
        • Check whether the type is “GPA” using “if”.
          • If it is true, sort the data based on the “GPA”.
          • Rename the file name.
          • Check whether the ordering is "D"
            • If it is true, reverse the data.
          • Use “break” to exit.
        • Check whether the type is “name” using “elif”.
          • If it is true, sort the data based on the “name”.
          • Rename the file name.
          • Check whether the ordering is "D"
            • If it is true, reverse the data.
          • Use “break” to exit.
        • Check whether the type is “credits” using “if”.
          • If it is true, sort the data based on the “credits”.
          • Rename the file name.
          • Check whether the ordering is "D"
            • If it is true, reverse the data.
          • Use “break” to exit.
              • Write the data into the output file.
  • 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: “gpasort.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.get_Name(), s.get_Hours(), s.getQ_Points()), file = out_file)

    #Close the output file

    out_file.close()

#Define the main() function

def main():

    #Print the string

    print("This program sorts student grade information by GPA, name, or credits.")

    #Get the input file

    file_name = 'gpa1.txt'

    #Assign the data return from read_Students()

    data = read_Students(file_name)

    #Create "while" loop

    while True:

        #Get the type

        x = (input('Type "GPA", "name", or "credits" >>>  '))

        #Get the type of ordering

        m = (input('Type "A" for ascending, "D" for descending.'))

        #Check whether the type is "GPA"

        if x == 'GPA':

            #Sort the data based on the gpa

            data.sort(key=Student.gpa)

            #Rename the file name

            s = "_(GPA)"

            #Check whether the ordering is "D"

            if m == "D":

                #Reverse the data

                data.reverse()

            #Use break to exit

            break

        #Check whether the type is "name"

        elif x == 'name':

            #Sort the data based on the name

            data.sort(key=Student.get_Name)

            #Rename the file name

            s = "_(name)"

            #Check whether the ordering is "D"

            if m == "D":

                #Reverse the data

                data.reverse()

            #Use break to exit

            break

        #Check whether the type is "credits"

        elif x == 'credits':

            #Sort the data based on the credits

            data.sort(key=Student.getQ_Points)

            #Rename the file name

            s = "_(credits)"

            #Check whether the ordering is "D"

            if m == "D":

                #Reverse the data

                data.reverse()

            #Use break to exit

            break

        #Otherwise

        else:

            #Print the string

            print("Please try again.")

    #Assign the output file

    filename = "GPA2" + s + ".py"

    #Write the data into output file

    write_Students(data, filename)

    #Print output file

    print("The data has been written to", filename)

#Call the main function

if __name__ == '__main__': 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 funcition 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

Screenshot of output file “GPA2.py” before execution:

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 11, Problem 3PE , additional homework tip  1

Output:

This program sorts student grade information by GPA, name, or credits.

Type "GPA", "name", or "credits" >>>  name

Type "A" for ascending, "D" for descending.D

The data has been written to GPA2_(name).py

>>>

Screenshot of output file “GPA2_(name).py after execution:

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 11, Problem 3PE , additional homework tip  2

Additional output:

This program sorts student grade information by GPA, name, or credits.

Type "GPA", "name", or "credits" >>>  GPA

Type "A" for ascending, "D" for descending.D

The data has been written to GPA2_(GPA).py

>>>

Screenshot of output file “GPA2_(gpa).py after execution:

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

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
Payroll Flowchart Analysis Discuss the risks depicted by the following payroll system flowchart. Describe the internal control improvements to the system that are needed to reduce these risks with specific explanations. An illustration of the above flowchart: It is represented in six columns and the column header reads: supervisor; payroll; data processing; accounts payable; cash disbursement; general ledger. In supervisor column, timekeeper enters into time cards and that leads to “Review Time cards and prepares Personnel Action Form” that further leads to two source documents “time cards, per action form.” Per action, form enters into prepare payroll in payroll column. Prepare payroll enters into paychecks and two source documents “payroll register, payroll register.” Payroll register leads to inverted triangle and payroll leads to A. Paycheck enters into review and distribute in supervisor column leads to paycheck and that further leads to employees. In data processing column,…
Using the following execution shown below, explain what is done in each of the ARIES recovery algorithm phases: LSN   LOG 00   begin_checkpoint 10 end_checkpoint 20 update: T1 writes P1 30 update: T2 writes P2 40 update: T3 writes P3 50 T2 commit 60 update: T3 writes P2 70 T2 end 80 update: T1 writes P5 90 T3 abort   CRASH, RESTART In addition to the execution shown here, the system crashes during recovery after writing two log records to stable storage and again after writing another two log records
A new application is being developed and will be using a database that includes a relation about items: Item (item_id:integer, item_name:string, color:string, price:real). Both the purchasing department in charge of obtaining raw material and the manufacturing department in charge of manufacturing the items can change the price of manufactured items according to changes that may happen in the raw material cost or production cost. The two departments use different transactions to update the price of items. The new application uses the following sequences of actions, listed in the order they are submitted to the DBMS: Sequence S1: T1:R(X), T2:W(X), T2:W(Y), T3:W(Y), T1:W(Y), T1:Commit, T2:Commit, T3:Commit Sequence S2: T1:R(X), T2:W(Y), T2:W(X), T3:W(Y), T1:W(Y), T1:Commit, T2:Commit, T3:Commit For each of the following concurrency control mechanisms, describe how they will handle each of the sequences (S1 & S2). Strict 2PL with timestamps used for deadlock prevention. Conservative…
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
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning