In: Computer Science
USING PYTHON 3.7:
import Student as st
import random
THRESHOLD = 0.01
tests = 0
passed = 0
def main():
test_get_higher_grade_student()
test_is_grade_above()
test_get_students()
test_get_classlist()
# test_count_above()
# test_get_average_grade()
print("TEST RESULTS:", passed, "/", tests)
def test_get_higher_grade_student():
s1a = st.Student("V0002000", 89)
s1b = st.Student("V0002001", 89)
s2 = st.Student("V0002002", 67)
s3 = st.Student("V0002002", 97)
result = get_higher_grade_student(s1a,s1b)
print_test("get_higher_grade_student(s1a,s1b)", result
== s1a)
result = get_higher_grade_student(s1a,s2)
print_test("get_higher_grade_student(s1a,s1b)", result
== s1a)
result = get_higher_grade_student(s1a,s3)
print_test("get_higher_grade_student(s1a,s1b)", result
== s3)
# (Student, Student -> bool)
# returns the Student with higher grade of s1 and s2, or s1
if grades are equal
def get_higher_grade_student(s1, s2):
print("fix me")
# TODO: complete get_higher_grade_student
# this should not always return s1
return s1
def test_is_grade_above():
s1 = st.Student("V0002000", 89)
s2 = st.Student("V0002002", 67)
result = is_grade_above(s1, 89)
print_test("is_grade_above(s1, 89)", result ==
False)
result = is_grade_above(s1, 88)
print_test("is_grade_above(s1, 89)", result ==
True)
result = is_grade_above(s2, 50)
print_test("is_grade_above(s2, 50)", result ==
True)
result = is_grade_above(s2, 80)
print_test("is_grade_above(s2, 80)", result ==
False)
# (Student, int -> bool)
# returns True if the grade of Student s is above the given
integer
def is_grade_above(s, n):
print("fix me")
# TODO: complete is_grade_above
def test_get_students():
result = get_students('studentData.txt')
expected = [st.Student('V00123456',
89),st.Student('V00123457', 99),
st.Student('V00123458', 30),st.Student('V00123459',
78)]
print_test("result = get_students('studentData.txt')",
result == expected)
# (str -> (list of Student))
# creates a new list of Students from data in the file
named filename
# where each line of file has a sid and grade separated by a
comma
def get_students(filename):
print("fix me")
# TODO: complete get_students
def test_get_classlist():
students = []
result = get_class_list(students)
expected = []
print_test("result = get_classlist(students)", result
== expected)
students = [st.Student('V00123456',
89),st.Student('V00123457', 99),
st.Student('V00123458', 30),st.Student('V00123459',
78)]
result = get_class_list(students)
expected =
['V00123456','V00123457','V00123458','V00123459']
print_test("result = get_classlist(students)", result
== expected)
# ((list of Student) -> (list of str))
# returns a new list of Students ids for each Student in
students
def get_class_list(students):
print("fix me")
# TODO: complete get_classlist
# TODO:
# Design and test a function that will take a list of
Students
# and a grade and counts and return the number of Students
# who have a grade above the given grade
# TODO:
# Design and test a function that will take a list of
Students
# computes and returns the average grade of all Students in
students
# (str, bool -> None)
# prints test_name and "passed" if expr is True otherwise prints
"failed"
def print_test(test_name, expr):
global tests
global passed
tests += 1
if(expr):
print(test_name + ': passed')
passed += 1
else:
print(test_name + ': failed')
# The following code will call your main function
# It also allows our grading script to call your main
# DO NOT ADD OR CHANGE ANYTHING PAST THIS LINE
# DOING SO WILL RESULT IN A ZERO GRADE
if __name__ == '__main__':
main()
studentData.txt ->
V00123456, 89
V00123457, 19
V00123458, 30
V00123459, 78
-------------------------//---------------------------//-----------------------------//-----------------------
class Student:
# constructor
def __init__(self, sid, grade):
self.sid = sid
self.grade = grade
def __str__(self):
return self.sid + ':' + str(self.grade)
def __repr__(self):
return str(self)
def get_sid(self):
return self.sid
def get_grade(self):
return self.grade
def set_sid(self, sid):
self.sid = sid
def set_grade(self, grade):
self.grade = grade
def __eq__(self, other):
if self.sid == other.get_sid():
return True
else:
return False
def main():
print('exercise1')
s1 = Student('V00123456', 90)
print(s1)
s2 = Student('V00456789', 60)
list = [s1,s2]
print(list)
result = s1.get_sid()
print(result)
result = s1.get_grade()
print(result)
s1.set_grade(98)
s1.set_sid("V00222210")
print(s1)
s1 = Student("V0002000", 89)
print(s1)
s2 = Student("V0002000", 67)
print(s2)
s3 = Student("V0002001", 67)
print(s3)
print(s1 == s2)
print(s1 == s1)
print(s1.__eq__(s2))
print(s2.__eq__(s3))
s1a = Student("V0002000", 89)
s1b = Student("V0002020", 65)
#
s2a = Student("V0002000", 67)
s2b = Student("V0002020", 65)
#
list1 = [s1a,s1b]
list2 = [s2a,s2b]
list3 = [s1a,s2a]
#
print('list1==list1', list1==list1)
print('list1==list2', list1==list2)
print('list1==list3', list1==list3)
main()
import Student as st import random THRESHOLD = 0.01 tests = 0 passed = 0 def main(): test_get_higher_grade_student() test_is_grade_above() test_get_students() test_get_classlist() test_count_above() test_get_average_grade() print("TEST RESULTS:", passed, "/", tests) def test_get_higher_grade_student(): s1a = st.Student("V0002000", 89) s1b = st.Student("V0002001", 89) s2 = st.Student("V0002002", 67) s3 = st.Student("V0002002", 97) result = get_higher_grade_student(s1a, s1b) print_test("get_higher_grade_student(s1a,s1b)", result == s1a) result = get_higher_grade_student(s1a, s2) print_test("get_higher_grade_student(s1a,s1b)", result == s1a) result = get_higher_grade_student(s1a, s3) print_test("get_higher_grade_student(s1a,s1b)", result == s3) # (Student, Student -> bool) # returns the Student with higher grade of s1 and s2, or s1 if grades are equal def get_higher_grade_student(s1, s2): # TODO: complete get_higher_grade_student if s1.get_grade() >= s2.get_grade(): return s1 else: return s2 def test_is_grade_above(): s1 = st.Student("V0002000", 89) s2 = st.Student("V0002002", 67) result = is_grade_above(s1, 89) print_test("is_grade_above(s1, 89)", result == False) result = is_grade_above(s1, 88) print_test("is_grade_above(s1, 89)", result == True) result = is_grade_above(s2, 50) print_test("is_grade_above(s2, 50)", result == True) result = is_grade_above(s2, 80) print_test("is_grade_above(s2, 80)", result == False) # (Student, int -> bool) # returns True if the grade of Student s is above the given integer def is_grade_above(s, n): # TODO: complete is_grade_above if s.get_grade() > n: return True else: return False def test_get_students(): result = get_students('studentData.txt') expected = [st.Student('V00123456', 89), st.Student('V00123457', 99), st.Student('V00123458', 30), st.Student('V00123459', 78)] print_test("result = get_students('studentData.txt')", result == expected) # (str -> (list of Student)) # creates a new list of Students from data in the file named filename # where each line of file has a sid and grade separated by a comma def get_students(filename): # TODO: complete get_students student_list = [] fileread = open(filename, 'r') for line in fileread.readlines(): data = line.strip().split(',') sid = data[0].strip() grade = int(data[1].strip()) student_list.append(st.Student(sid, grade)) fileread.close() return student_list def test_get_classlist(): students = [] result = get_class_list(students) expected = [] print_test("result = get_classlist(students)", result == expected) students = [st.Student('V00123456', 89), st.Student('V00123457', 99), st.Student('V00123458', 30), st.Student('V00123459', 78)] result = get_class_list(students) expected = ['V00123456', 'V00123457', 'V00123458', 'V00123459'] print_test("result = get_classlist(students)", result == expected) # ((list of Student) -> (list of str)) # returns a new list of Students ids for each Student in students def get_class_list(students): # TODO: complete get_classlist id_list = [] for student in students: id_list.append(student.get_sid()) return id_list # TODO: # Design and test a function that will take a list of Students # and a grade and counts and return the number of Students # who have a grade above the given grade def test_count_above(): students = [st.Student('V00123456', 89), st.Student('V00123457', 99), st.Student('V00123458', 30), st.Student('V00123459', 78)] result = count_above(students, 75) print_test("result = count_above(students)", result == 3) def count_above(students, grade): count = 0 for student in students: if student.get_grade() > grade: count += 1 return count # TODO: # Design and test a function that will take a list of Students # computes and returns the average grade of all Students in students def test_get_average_grade(): students = [st.Student('V00123456', 89), st.Student('V00123457', 99), st.Student('V00123458', 30), st.Student('V00123459', 78)] result = get_average_grade(students) print_test("result = get_average_grade(students)", result == 74) def get_average_grade(students): avg = 0 for student in students: avg += student.get_grade() avg = avg / len(students) return avg # (str, bool -> None) # prints test_name and "passed" if expr is True otherwise prints "failed" def print_test(test_name, expr): global tests global passed tests += 1 if (expr): print(test_name + ': passed') passed += 1 else: print(test_name + ': failed') # The following code will call your main function # It also allows our grading script to call your main # DO NOT ADD OR CHANGE ANYTHING PAST THIS LINE # DOING SO WILL RESULT IN A ZERO GRADE if __name__ == '__main__': main()
SCREENSHOT
OUTPUT
No changes in Student class.