Skip to content

Instantly share code, notes, and snippets.

@CleverProgrammer
Created November 27, 2015 04:50
Show Gist options
  • Save CleverProgrammer/16b2a5202c0ce305a022 to your computer and use it in GitHub Desktop.
Save CleverProgrammer/16b2a5202c0ce305a022 to your computer and use it in GitHub Desktop.
method based approach rather than a function based.
"""
Author: Rafeh Qazi
Create a student class that models a student.
Date: 11/26/2015
"""
from collections import namedtuple
class Student:
student_counter = 0
all_students = []
top_student = ''
def __init__(self, name, hours, quality_points):
self.name = name
self.hours = float(hours)
self.quality_points = float(quality_points)
self.result = self.quality_points / self.hours
Student.student_counter += 1
Student.all_students.append(self)
Student.top_student = Student.best_student()
@classmethod
def best_student(cls):
"""
Take all_students as input and return the student with the
best GPA.
:return: object
"""
top_student = Student.all_students[0]
for student in Student.all_students:
if student.result > top_student.result:
top_student = student
return top_student
def all_student_results(filename):
"""
Read file and return students' first-name, last-name, hours,
and the quality points.
:param filename: string
:return: list
"""
Student_info = namedtuple('Student_info',
'last_name first_name hours q_points')
with open(filename, mode='r') as f:
for line in f:
line = Student_info(*line.split())
Student(line.first_name, line.hours, line.q_points)
def print_results(top_student):
"""
Take the student with the highest GPA and print their report.
Print the report of the best student.
:param top_student: object
"""
print('The best student is {0} who has spent {1} hours and has a {2} gpa.'.format(
top_student.name, top_student.hours, top_student.result))
def main():
all_student_results('students.txt')
# print('Total Students:', Student.student_counter)
# print('All Students:', Student.all_students)
print_results(Student.top_student)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment