-
Notifications
You must be signed in to change notification settings - Fork 0
/
cohort_data.py
252 lines (172 loc) · 12.6 KB
/
cohort_data.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""Functions to parse a file containing student data."""
def unique_houses(filename):
"""TODO: Return a set of student houses.
Iterate over the cohort_data.txt file to look for all of the included house names
and create a set called "houses" that holds those names.
For example:
>>> unique_houses("cohort_data.txt")
set(['Gryffindor', 'Hufflepuff', "Dumbledore's Army", 'Ravenclaw', 'Slytherin'])
"""
the_file = open(filename)
houses = set()
# houses = {'Gryffindor'}
# houses = set(['Gryffindor'])
for line in the_file:
student = line.split("|")
house = student[2]
if house != "":
houses.add(house)
return houses
def sort_by_cohort(filename):
"""TODO: Return a list of all cohort lists, including ghosts but not instructors.
Sort students by cohort, skipping instructors.
Iterate over the data to create a list for each cohort. Puts ghosts into a
separate list called "ghosts".
For example:
>>> sort_by_cohort("cohort_data.txt")
[['Harry Potter', 'Mandy Brocklehurst', 'Ron Weasley', 'Oliver Wood', 'Colin Creevey', 'Cho Chang', 'Michael Corner', 'Draco Malfoy', 'Seamus Finnigan', 'Eddie Carmichael', 'Theodore Nott', 'Terence Higgs', 'Hermione Granger', 'Penelope Clearwater', 'Angelina Johnson', 'Dennis Creevey'], ['Neville Longbottom', 'Cedric Diggory', 'Pansy Parkinson', 'Anthony Goldstein', 'Padma Patil', 'Luna Lovegood', 'Eleanor Branstone', 'Lee Jordan', 'Marietta Edgecombe', 'Andrew Kirke', 'Ginny Weasley', 'Mary Macdonald', 'Blaise Zabini', 'Natalie McDonald', 'Adrian Pucey', 'Hannah Abbott', 'Graham Pritchard', 'Susan Bones', 'Roger Davies', 'Owen Cauldwell'], ['Laura Madley', 'Orla Quirke', 'Parvati Patil', 'Eloise Midgeon', 'Zacharias Smith', 'Cormac McLaggen', 'Lisa Turpin', 'Demelza Robins', 'Ernie Macmillan', 'Millicent Bullstrode', 'Percy Weasley', 'Jimmy Peakes', 'Justin Finch-Fletchley', 'Miles Bletchley', 'Malcolm Baddock'], ['Marcus Belby', 'Euan Abercrombie', 'Vincent Crabbe', 'Ritchie Coote', 'Katie Bell', 'Terry Boot', 'Lavender Brown', 'Gregory Goyle', 'Marcus Flint', 'Dean Thomas', 'Jack Sloper', 'Rose Zeller', 'Stewart Ackerley', 'Fred Weasley', 'George Weasley', 'Romilda Vane', 'Alicia Spinnet', 'Kevin Whitby'], ['Friendly Friar', 'Grey Lady', 'Nearly Headless Nick', 'Bloody Baron']]
"""
winter_16 = []
spring_16 = []
summer_16 = []
fall_15 = []
ghosts = []
the_file = open(filename)
for line in the_file:
line = line.rstrip()
student = line.split("|")
# student = line.rstrip().split("|")
cohort = student[4]
name = student[0] + " " + student[1]
if cohort == "Winter 2016":
winter_16.append(name)
elif cohort == "Spring 2016":
spring_16.append(name)
elif cohort == "Summer 2016":
summer_16.append(name)
elif cohort == "Fall 2015":
fall_15.append(name)
elif cohort == "G":
ghosts.append(name)
all_students = [fall_15, winter_16, spring_16, summer_16, ghosts]
return all_students
def hogwarts_by_house(filename):
"""TODO: Sort students into lists by house and return all lists in one list.
Iterate over the data to create a list for each house, and sorts students
into their appropriate houses by last name. Sorts ghosts into a list called
"ghosts" and instructors into a list called "instructors".
For example:
>>> hogwarts_by_house("cohort_data.txt")
[['Abercrombie', 'Bell', 'Brown', 'Coote', 'Finnigan', 'Granger', 'Johnson', 'Jordan', 'Kirke', 'Longbottom', 'Macdonald', 'McDonald', 'McLaggen', 'Patil', 'Peakes', 'Potter', 'Robins', 'Sloper', 'Thomas', 'Vane', 'Weasley', 'Weasley', 'Weasley', 'Weasley', 'Weasley', 'Wood'], ['Baddock', 'Bletchley', 'Bullstrode', 'Crabbe', 'Flint', 'Goyle', 'Higgs', 'Malfoy', 'Parkinson', 'Pritchard', 'Pucey', 'Zabini'], ['Bones', 'Branstone', 'Cauldwell', 'Diggory', 'Finch-Fletchley', 'Macmillan', 'Madley', 'Midgeon', 'Smith', 'Whitby', 'Zeller'], ['Ackerley', 'Belby', 'Boot', 'Brocklehurst', 'Carmichael', 'Clearwater', 'Corner', 'Davies', 'Goldstein', 'Lovegood', 'Patil', 'Quirke', 'Turpin'], ['Abbott', 'Chang', 'Creevey', 'Creevey', 'Edgecombe', 'Nott', 'Spinnet'], ['Baron', 'Friar', 'Lady', 'Nick'], ['Flitwick', 'McGonagall', 'Snape', 'Sprout']]
"""
gryffindor = []
hufflepuff = []
slytherin = []
dumbledores_army = []
ravenclaw = []
ghosts = []
instructors = []
the_file = open(filename)
for line in the_file:
student = line.rstrip().split("|")
house = student[2]
name = student[1]
title = student[4]
if house == "Gryffindor":
gryffindor.append(name)
elif house == "Hufflepuff":
hufflepuff.append(name)
elif house == "Slytherin":
slytherin.append(name)
elif house == "Dumbledore's Army":
dumbledores_army.append(name)
elif house == "Ravenclaw":
ravenclaw.append(name)
if title == "G":
ghosts.append(name)
elif title == "I":
instructors.append(name)
all_students = [gryffindor,
hufflepuff,
slytherin,
dumbledores_army,
ravenclaw,
ghosts,
instructors]
# for group in all_students:
# group.sort()
all_students.sort()
return all_students
def all_students_tuple_list(filename):
"""TODO: Return a list of tuples of student data.
Iterate over the data to create a big list of tuples that individually
hold all the data for each person. (full_name, house, advisor, cohort)
For example:
>>> all_students_data = all_students_tuple_list("cohort_data.txt")
>>> print all_students_data
[('Harry Potter', 'Gryffindor', 'McGonagall', 'Fall 2015'), ('Laura Madley', 'Hufflepuff', 'Sprout', 'Spring 2016'), ('Orla Quirke', 'Ravenclaw', '', 'Spring 2016'), ('Marcus Belby', 'Ravenclaw', 'Flitwick', 'Summer 2016'), ('Euan Abercrombie', 'Gryffindor', 'McGonagall', 'Summer 2016'), ('Neville Longbottom', 'Gryffindor', 'McGonagall', 'Winter 2016'), ('Vincent Crabbe', 'Slytherin', 'Snape', 'Summer 2016'), ('Parvati Patil', 'Gryffindor', 'McGonagall', 'Spring 2016'), ('Mandy Brocklehurst', 'Ravenclaw', 'Flitwick', 'Fall 2015'), ('Ritchie Coote', 'Gryffindor', 'McGonagall', 'Summer 2016'), ('Eloise Midgeon', 'Hufflepuff', 'Sprout', 'Spring 2016'), ('Zacharias Smith', 'Hufflepuff', 'Sprout', 'Spring 2016'), ('Katie Bell', 'Gryffindor', 'McGonagall', 'Summer 2016'), ('Cedric Diggory', 'Hufflepuff', 'Sprout', 'Winter 2016'), ('Ron Weasley', 'Gryffindor', 'McGonagall', 'Fall 2015'), ('Cormac McLaggen', 'Gryffindor', 'McGonagall', 'Spring 2016'), ('Lisa Turpin', 'Ravenclaw', 'Flitwick', 'Spring 2016'), ('Oliver Wood', 'Gryffindor', 'McGonagall', 'Fall 2015'), ('Pansy Parkinson', 'Slytherin', 'Snape', 'Winter 2016'), ('Demelza Robins', 'Gryffindor', 'McGonagall', 'Spring 2016'), ('Terry Boot', 'Ravenclaw', 'Flitwick', 'Summer 2016'), ('Lavender Brown', 'Gryffindor', 'McGonagall', 'Summer 2016'), ('Anthony Goldstein', 'Ravenclaw', 'Flitwick', 'Winter 2016'), ('Ernie Macmillan', 'Hufflepuff', 'Sprout', 'Spring 2016'), ('Colin Creevey', "Dumbledore's Army", 'McGonagall', 'Fall 2015'), ('Padma Patil', 'Ravenclaw', 'Flitwick', 'Winter 2016'), ('Cho Chang', "Dumbledore's Army", 'Flitwick', 'Fall 2015'), ('Gregory Goyle', 'Slytherin', 'Snape', 'Summer 2016'), ('Michael Corner', 'Ravenclaw', 'Flitwick', 'Fall 2015'), ('Luna Lovegood', 'Ravenclaw', 'Flitwick', 'Winter 2016'), ('Eleanor Branstone', 'Hufflepuff', 'Sprout', 'Winter 2016'), ('Draco Malfoy', 'Slytherin', 'Snape', 'Fall 2015'), ('Marcus Flint', 'Slytherin', 'Snape', 'Summer 2016'), ('Lee Jordan', 'Gryffindor', 'McGonagall', 'Winter 2016'), ('Marietta Edgecombe', "Dumbledore's Army", 'Flitwick', 'Winter 2016'), ('Andrew Kirke', 'Gryffindor', 'McGonagall', 'Winter 2016'), ('Ginny Weasley', 'Gryffindor', 'McGonagall', 'Winter 2016'), ('Mary Macdonald', 'Gryffindor', 'McGonagall', 'Winter 2016'), ('Blaise Zabini', 'Slytherin', 'Snape', 'Winter 2016'), ('Millicent Bullstrode', 'Slytherin', 'Snape', 'Spring 2016'), ('Seamus Finnigan', 'Gryffindor', 'McGonagall', 'Fall 2015'), ('Eddie Carmichael', 'Ravenclaw', 'Flitwick', 'Fall 2015'), ('Dean Thomas', 'Gryffindor', 'McGonagall', 'Summer 2016'), ('Percy Weasley', 'Gryffindor', 'McGonagall', 'Spring 2016'), ('Jack Sloper', 'Gryffindor', 'McGonagall', 'Summer 2016'), ('Theodore Nott', "Dumbledore's Army", 'Snape', 'Fall 2015'), ('Terence Higgs', 'Slytherin', 'Snape', 'Fall 2015'), ('Jimmy Peakes', 'Gryffindor', 'McGonagall', 'Spring 2016'), ('Natalie McDonald', 'Gryffindor', 'McGonagall', 'Winter 2016'), ('Justin Finch-Fletchley', 'Hufflepuff', 'Sprout', 'Spring 2016'), ('Rose Zeller', 'Hufflepuff', 'Sprout', 'Summer 2016'), ('Miles Bletchley', 'Slytherin', 'Snape', 'Spring 2016'), ('Stewart Ackerley', 'Ravenclaw', 'Flitwick', 'Summer 2016'), ('Adrian Pucey', 'Slytherin', 'Snape', 'Winter 2016'), ('Fred Weasley', 'Gryffindor', 'McGonagall', 'Summer 2016'), ('Hannah Abbott', "Dumbledore's Army", 'Sprout', 'Winter 2016'), ('Graham Pritchard', 'Slytherin', 'Snape', 'Winter 2016'), ('George Weasley', 'Gryffindor', 'McGonagall', 'Summer 2016'), ('Hermione Granger', 'Gryffindor', 'McGonagall', 'Fall 2015'), ('Penelope Clearwater', 'Ravenclaw', 'Flitwick', 'Fall 2015'), ('Malcolm Baddock', 'Slytherin', 'Snape', 'Spring 2016'), ('Angelina Johnson', 'Gryffindor', 'McGonagall', 'Fall 2015'), ('Susan Bones', 'Hufflepuff', 'Sprout', 'Winter 2016'), ('Dennis Creevey', "Dumbledore's Army", 'McGonagall', 'Fall 2015'), ('Roger Davies', 'Ravenclaw', 'Flitwick', 'Winter 2016'), ('Romilda Vane', 'Gryffindor', 'McGonagall', 'Summer 2016'), ('Alicia Spinnet', "Dumbledore's Army", 'McGonagall', 'Summer 2016'), ('Kevin Whitby', 'Hufflepuff', 'Sprout', 'Summer 2016'), ('Owen Cauldwell', 'Hufflepuff', 'Sprout', 'Winter 2016')]
"""
student_list = []
# Code goes here
return student_list
def find_cohort_by_student_name(student_list):
"""TODO: Given full name, return student's cohort.
Use list of tuples generated by all_students_tuple_list() to make a small
function that, given a first and last name from the command line, return
that student's cohort, or returns "Student not found." when appropriate.
NOTE: This function isn't included in doctests. Test it by uncommenting the
function call at the bottom of the file.
For example:
Who are you looking for? Harry Potter
'Harry Potter was in the Fall 2015 cohort.'
Who are you looking for? Tom Riddle
'Student not found.'
"""
# Code goes here
return "Student not found."
##########################################################################################
# Further Study Questions
def find_name_duplicates(filename):
"""TODO: Return a set of student last names that have duplicates.
Iterate over the data to find any last names that exist across all cohorts.
Use set operations (set math) to create and return a set of these names.
For example:
>>> find_name_duplicates("cohort_data.txt")
set(['Weasley'])
"""
duplicate_names = set()
# Code goes here
return duplicate_names
def find_house_members_by_student_name(student_list):
"""TODO: Prompt user for a student. Display everyone in their house and cohort.
Prompt the user for the name via the command line and when given a name,
print a statement of everyone in their house in their cohort.
Use the list of tuples generated by all_students_tuple_list() to make a
small function that, when given a student's first and last name, prints
students that are in both that student's cohort and that student's house.
NOTE: This function isn't included in doctests. Test it by uncommenting the
function call at the bottom of the file.
For example:
Choose a student: Hermione Granger
Hermione Granger was in house Gryffindor in the Fall 2015 cohort.
The following students are also in their house:
Seamus Finnigan
Angelina Johnson
Harry Potter
Ron Weasley
Oliver Wood
"""
# Code goes here
return
#############################################################################
# Here is some useful code to run these functions without doctests!
# find_cohort_by_student_name(all_students_data)
# find_house_members_by_student_name(all_students_data)
##############################################################################
# END OF MAIN EXERCISE. Yay! You did it! You Rock!
#
if __name__ == "__main__":
import doctest
result = doctest.testmod()
if result.failed == 0:
print("ALL TESTS PASSED")