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.
-
- Check whether the type is “GPA” using “if”.
- 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: “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()
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:
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:
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:
Want to see more full solutions like this?
Chapter 11 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- As described in Learning from Mistakes, the failure of the A380 to reach its sales goals was due to Multiple Choice: a) misunderstanding of supplier demands. b) good selection of hotel in the sky amenities. c) changes in customer demands. d) lack of production capacity.arrow_forwardNumerous equally balanced competitors selling products that lack differentiation in a slow growth industry are most likely to experience high: a) intensity of rivalry among competitors. b) threat of substitute products. c) threat of new entrants. d) bargaining power of suppliers.arrow_forwardA Dia file has been created for you to extend and can be found on Company.dia represents a completed ER schema which, models some of the information implemented in the system, as a starting point for this exercise. Understanding the ER schema for the Company database. To demonstrate that you understand the information represented by the schema, explain using EMPLOYEE, DEPARTMENT, PROJECT and DEPENDENT as examples: attributes, entities and relationships cardinality & participation constraints on relationships You should explain questions a and b using the schema you have been given to more easily explain your answers. Creating and Extending Entity Relationship (EER) Diagrams. To demonstrate you can create entity relationship diagrams extend the ER as described in Company.dia by modelling new requirements as follows: Create subclasses to extend Employee. The employee type may be distinguished further based on the job type (SECRETARY, ENGINEER, MANAGER, and TECHNICIAN) and based…arrow_forward
- Computer programs can be very complex, containing thousands (or millions) of lines of code and performing millions of operations per second. Given this, how can we possibly know that a particular computer program's results are correct? Do some research on this topic then think carefully about your response. Also, explain how YOU would approach testing a large problem. Your answer must be thoughtful and give some insight into why you believe your steps would be helpful when testing a large program.arrow_forwardCould you fix this? My marker has commented, What's missing? The input list is the link below. https://gmierzwinski.github.io/bishops/cs321/resources/CS321_Assignment_1_Input.txt result.put(true, dishwasherSum); result.put(false, sinkSum); return result; }}arrow_forwardPLEG136: Week 5 Portofolio Project Motion to Compelarrow_forward
- B A E H Figure 1 K Questions 1. List the shortest paths between all node pairs. Indicate the number of shortest paths that pass through each edge. Explain how this information helps determine edge betweenness. 2. Compute the edge betweenness for each configuration of DFS. 3. Remove the edge(s) with the highest betweenness and redraw the graph. Recompute the edge betweenness centrality for the new graph. Explain how the network structure changes after removing the edge. 4. Iteratively remove edges until at least two communities form. Provide step-by-step calculations for each removal. Explain how edge betweenness changes dynamically during the process. 5. How many communities do you detect in the final step? Compare the detected communities with the original graph structure. Discuss whether the Girvan- Newman algorithm successfully captures meaningful subgroups. 6. If you were to use degree centrality instead of edge betweenness for community detection, how would the results change?arrow_forwardUnit 1 Assignment 1 – Loops and Methods (25 points) Task: You are working for Kean University and given the task of building an Email Registration System. Your objective is to generate a Kean email ID and temporary password for every new user. The system will prompt for user information and generate corresponding credentials. You will develop a complete Java program that consists of the following modules: Instructions: 1. Main Method: ○ The main method should include a loop (of your choice) that asks for input from five users. For each user, you will prompt for their first name and last name and generate the email and password by calling two separate methods. Example о Enter your first name: Joe Enter your last name: Rowling 2.generateEmail() Method: This method will take the user's first and last name as parameters and return the corresponding Kean University email address. The format of the email is: • First letter of the first name (lowercase) + Full last name (lowercase) +…arrow_forwardI have attached my code, under I want you to show me how to enhance it and make it more cooler and better in graphics with following the instructions.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrNew Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage Learning