Question

In: Computer Science

USING PYTHON 3.7: import Student as st import random THRESHOLD = 0.01 tests = 0 passed...

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

Solutions

Expert Solution

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.


Related Solutions

#Python 3.7 "Has no attribute" error - def get():     import datetime     d = date_time_obj.date()...
#Python 3.7 "Has no attribute" error - def get():     import datetime     d = date_time_obj.date()     return(d) print(a["Date"]) print("3/14/2012".get()) How to write the "get" function (without any imput), to convery the format ""3/14/2012" to the format "2012-03-14", by simply using "3/14/2012".get() ?
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade point average (GPA) on a scale of 0 to 4 where A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take as input two lists. One list contains the grades received in each course, and the second list contains the corresponding credit hours for each course. The output should be the calculated GPA. To...
For a hypothesis test, where ?0:?1=0 and ?1:?1≠0, and using ?=0.01 , give the following:
Consider the data set below. x 22 99 88 44 99 99 y 66 44 88 55 77 11 For a hypothesis test, where ?0:?1=0 and ?1:?1≠0, and using ?=0.01 , give the following: (a) The test statistic ?= (b) The degree of freedom ??= (c) The rejection region |?|> The final conclustion is A. There is not sufficient evidence to reject the null hypothesis that ?1=0 β 1 = 0 . B. We can reject the null hypothesis that...
Please provide Python 3.7 implementation of Graph ADT using in-built structure List only (not using dict)....
Please provide Python 3.7 implementation of Graph ADT using in-built structure List only (not using dict). Basic structure of graph will be: Node = [] Edges = [ [ ] , [ ] ] Also provide method to print the graph along with all path
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks...
complete the code to determine the highest score value in python import random #variables and constants...
complete the code to determine the highest score value in python import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLL_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "4 of a kind" ] pScore = 0 cScore = 0 num_Score = int( input ("Enter a number of round: ") ) print ("\n") count = 0 while count < num_Score: #set this to the value MAX_ROLLS pdice = [0,0,0,0,0]...
The first random number generator comes from Python itself. To use it you need to import...
The first random number generator comes from Python itself. To use it you need to import the Python package. Then call the random() method. See below. import random print (random.random()) It is important to know that a random number generator really is random. If so, it should have uniform distribution between 0 and 1. Test the random number generators in Python for uniformity.
Using Python 3 In 2017, the tuition for a full time student in a university is...
Using Python 3 In 2017, the tuition for a full time student in a university is $6,450 per year. The tuition will be going up for the next 7 years at a rate of 3.5% per year. Write your program using a loop that displays the projected year tuition for the next 7 years. You should display the actual year (2018, 2019, through 2024) and the tuition amount per year.
Python Code for 8-queens using random restart algorithms
Python Code for 8-queens using random restart algorithms
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT