forked from drewmacleod/Project1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project1-206.py
119 lines (90 loc) · 3.62 KB
/
project1-206.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import filecmp
from dateutil.relativedelta import *
from datetime import date
def getData(file):
# get a list of dictionary objects from the file
#Input: file name
#Ouput: return a list of dictionary objects where
#the keys are from the first row in the data. and the values are each of the other rows
# added comment for git
# added a new comment
pass
def mySort(data,col):
# Sort based on key/column
#Input: list of dictionaries and col (key) to sort on
#Output: Return the first item in the sorted list as a string of just: firstName lastName
pass
def classSizes(data):
# Create a histogram
# Input: list of dictionaries
# Output: Return a list of tuples sorted by the number of students in that class in
# descending order
# [('Senior', 26), ('Junior', 25), ('Freshman', 21), ('Sophomore', 18)]
pass
def findMonth(a):
# Find the most common birth month form this data
# Input: list of dictionaries
# Output: Return the month (1-12) that had the most births in the data
pass
def mySortPrint(a,col,fileName):
#Similar to mySort, but instead of returning single
#Student, the sorted data is saved to a csv file.
# as fist,last,email
#Input: list of dictionaries, col (key) to sort by and output file name
#Output: No return value, but the file is written
pass
def findAge(a):
# def findAge(a):
# Input: list of dictionaries
# Output: Return the average age of the students and round that age to the nearest
# integer. You will need to work with the DOB and the current date to find the current
# age in years.
pass
################################################################
## DO NOT MODIFY ANY CODE BELOW THIS
################################################################
## We have provided simple test() function used in main() to print what each function returns vs. what it's supposed to return.
def test(got, expected, pts):
score = 0;
if got == expected:
score = pts
print(" OK ", end=" ")
else:
print (" XX ", end=" ")
print("Got: ",got, "Expected: ",expected)
return score
# Provided main() calls the above functions with interesting inputs, using test() to check if each result is correct or not.
def main():
total = 0
print("Read in Test data and store as a list of dictionaries")
data = getData('P1DataA.csv')
data2 = getData('P1DataB.csv')
total += test(type(data),type([]),50)
print()
print("First student sorted by First name:")
total += test(mySort(data,'First'),'Abbot Le',25)
total += test(mySort(data2,'First'),'Adam Rocha',25)
print("First student sorted by Last name:")
total += test(mySort(data,'Last'),'Elijah Adams',25)
total += test(mySort(data2,'Last'),'Elijah Adams',25)
print("First student sorted by Email:")
total += test(mySort(data,'Email'),'Hope Craft',25)
total += test(mySort(data2,'Email'),'Orli Humphrey',25)
print("\nEach grade ordered by size:")
total += test(classSizes(data),[('Junior', 28), ('Senior', 27), ('Freshman', 23), ('Sophomore', 22)],25)
total += test(classSizes(data2),[('Senior', 26), ('Junior', 25), ('Freshman', 21), ('Sophomore', 18)],25)
print("\nThe most common month of the year to be born is:")
total += test(findMonth(data),3,15)
total += test(findMonth(data2),3,15)
print("\nSuccessful sort and print to file:")
mySortPrint(data,'Last','results.csv')
if os.path.exists('results.csv'):
total += test(filecmp.cmp('outfile.csv', 'results.csv'),True,20)
print("\nTest of extra credit: Calcuate average age")
total += test(findAge(data), 40, 5)
total += test(findAge(data2), 42, 5)
print("Your final score is " + str(total))
# Standard boilerplate to call the main() function that tests all your code
if __name__ == '__main__':
main()