This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
forked from slgraff/edx-mitx-6.00.1x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec8.3-subjectCode.py
95 lines (79 loc) · 2.76 KB
/
lec8.3-subjectCode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# lec8.3-subjectCode.py
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
# Lecture 8, video 3
#
# Comment out functions as needed to try each subsequent version
# which demonstrates error handling techniques
def getSubjectStats(subject, weights):
return [[elt[0], elt[1], avg(elt[1], weights)]
for elt in subject]
def dotProduct(a,b):
result = 0.0
for i in range(len(a)):
result += a[i]*b[i]
return result
# def avg(grades, weights):
# return dotProduct(grades, weights)/len(grades)
# def avg(grades, weights):
# try:
# return dotProduct(grades, weights)/len(grades)
# except ZeroDivisionError:
# print 'no grades data'
#
# def avg(grades, weights):
# try:
# return dotProduct(grades, weights)/len(grades)
# except ZeroDivisionError:
# print 'no grades data'
# return 0.0
def convertLetterGrade(grade):
if type(grade) == int:
return grade
elif grade == 'A':
return 90.0
elif grade == 'B':
return 80.0
elif grade == 'C':
return 70.0
elif grade == 'D':
return 60.0
else:
return 50.0
def avg(grades, weights):
try:
return dotProduct(grades, weights)/len(grades)
except ZeroDivisionError:
print 'no grades data'
return 0.0
# For a type error see if we can convert the grade to a usable type
except TypeError:
newgrades = [convertLetterGrade(elt) for elt in grades]
return dotProduct(newgrades, weights)/len(newgrades)
# def avg(grades, weights):
# assert not len(grades) == 0, 'no grades data'
# newgrades = [convertLetterGrade(elt) for elt in grades]
# return dotProduct(newgrades, weights)/len(newgrades)
#
# def avg(grades, weights):
# assert not len(grades) == 0, 'no grades data'
# assert len(grades) == len(weights), 'wrong number grades'
# newgrades = [convertLetterGrade(elt) for elt in grades]
# result = dotProduct(newgrades, weights)/len(newgrades)
# assert 0.0 <= result <= 100.0, 'grade exceeds limits'
# return result
test = [[['fred', 'flintstone'], [10.0, 5.0, 85.0]],
[['barney', 'rubble'], [10.0, 8.0, 74.0]],
[['wilma', 'flintstone'], [8.0, 10.0, 96.0]],
[['dino'], []]]
weights = [.3, .2, .5]
weights1 = [.15, .1, .25, .25]
test1 = [[['fred', 'flintstone'], [10.0, 5.0, 85.0, 'D']],
[['barney', 'rubble'], [10.0, 8.0, 74.0, 'B']],
[['wilma', 'flintstone'], [8.0, 10.0, 96.0, 'A']],
[['dino'], []]]
test2 = [[['fred', 'flintstone'], [10.0, 5.0, 8500, 'D']],
[['barney', 'rubble'], [10.0, 8.0, 74.0, 'B']],
[['wilma', 'flintstone'], [8.0, 10.0, 96.0, 'A']]]
# Examples of calling getSubjectStats()
getSubjectStats(test, weights)