xs = [10,7,8,5,2,9]
min(xs)
2
shift = xs.index(min(xs))
print(shift)
4
# dictionary is a set of key-value pairs
# keys are usually strings or numbers
# values can be any type
d = {} # empty dictionary
with open("grades.txt") as f:
content = f.read()
data = content.split('\n')
#print(data)
for x in data:
s = x.split(':')
#print(s)
if s[0] not in d:
d[s[0]] = [(s[1],int(s[2]),s[3])]
else:
d[s[0]].append((s[1],int(s[2]),s[3]))
print(d)
{'Jones': [('CSC 1301', 4, 'A'), ('CSC 1302', 4, 'A'), ('CSC 2720', 3, 'B')], 'Smith': [('CSC 1301', 4, 'B'), ('CSC 1302', 4, 'B'), ('CSC 2720', 3, 'A')], 'Blake': [('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B')]}
def num_grade(letter_grade):
nd = {'A':4, 'B':3, 'C':2, 'D':1, 'F':0}
return nd[letter_grade]
def gpa(d, sname):
if sname in d:
courses = d[sname]
ncredits = 0
npoints = 0
for course in courses:
ncredits = ncredits + course[1]
npoints = npoints + course[1]*num_grade(course[2])
return npoints/ncredits
else:
return None
print(gpa(d,"Sunderraman"))
None
list(d.keys())
['Jones', 'Smith', 'Blake']
list(d.values())
[[('CSC 1301', 4, 'A'), ('CSC 1302', 4, 'A'), ('CSC 2720', 3, 'B')], [('CSC 1301', 4, 'B'), ('CSC 1302', 4, 'B'), ('CSC 2720', 3, 'A')], [('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B')]]
list(d.items())
[('Jones', [('CSC 1301', 4, 'A'), ('CSC 1302', 4, 'A'), ('CSC 2720', 3, 'B')]), ('Smith', [('CSC 1301', 4, 'B'), ('CSC 1302', 4, 'B'), ('CSC 2720', 3, 'A')]), ('Blake', [('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B')])]