-
Notifications
You must be signed in to change notification settings - Fork 0
/
cohort_data.py
220 lines (160 loc) · 6.56 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
def unique_houses(filename):
"""TODO: Create a set of student houses.
Iterates over the cohort_data.txt file to look for all of the included house names
and creates a set called 'houses' that holds those names.
ex. houses = set([ "Hufflepuff",
"Slytherin",
"Ravenclaw",
"Gryffindor",
"Dumbledore's Army",
"Order of the Phoenix"
])
"""
houses = set()
cohort_data = open(filename)
for line in cohort_data:
line = line.rstrip()
fellow = line.split("|")
house = fellow[2]
if house != '':
houses.add(house)# Code goes here
return houses
cohort_data.close()
def sort_by_cohort(filename):
"""TODO: Sort students by cohort.
Iterates over the data to create a list for each cohort, ordering students
alphabetically by first name and tas separately. Returns list of lists.
ex. winter_15 = ["alice tsao", "amanda gilmore", "anne vetto", "..." ]
ex. all_students = [winter_15, spring_15, summer_15, tas]
"""
cohort_data = open(filename)
all_students = []
winter_15 = []
spring_15 = []
summer_15 = []
tas = []
for line in cohort_data:
line = line.rstrip()
fellow = line.split("|")
student_name = "{} {}".format(fellow[0],fellow[1])
if fellow[4] == "Spring 2015":
spring_15.append(student_name)
elif fellow[4] == "Winter 2015":
winter_15.append(student_name)
elif fellow[4] == "Summer 2015":
summer_15.append(student_name)
elif fellow[4] == "TA":
tas.append(student_name)
all_students = [spring_15, winter_15, summer_15, tas]
cohort_data.close()
return all_students
def students_by_house(filename):
"""TODO: Sort students by house.
Iterate over the data to create a list for each house, and sort students
into their appropriate houses by last name. Sort TAs into a list called "tas"
and instructors in to a list called "instructors".
Return all lists in one list of lists.
ex. hufflepuff = ["Gaikwad", "Le", "..." ]
ex. tas = ["Bryant", "Lefevre", "..."]
ex. all_students = [ hufflepuff,
gryffindor,
ravenclaw,
slytherin,
dumbledores_army,
order_of_the_phoenix,
tas,
instructors
]
"""
gryffindor = []
hufflepuff = []
slytherin = []
dumbledores_army = []
order_of_the_phoenix = []
ravenclaw = []
tas = []
instructors = []
all_students = [gryffindor, hufflepuff, slytherin, dumbledores_army, order_of_the_phoenix, ravenclaw, tas, instructors]
cohort_data = open(filename)
for line in cohort_data:
line = line.rstrip()
fellow = line.split("|")
last_name = fellow[1]
house = fellow [2]
instructor_status = fellow[4]
if house == "Gryffindor":
gryffindor.append(last_name)
elif house == "Slytherin":
slytherin.append(last_name)
elif house == "Hufflepuff":
hufflepuff.append(last_name)
elif house == "Ravenclaw":
ravenclaw.append(last_name)
elif house == "Dumbledore's Army":
dumbledores_army.append(last_name)
elif house == "Order of the Phoenix":
order_of_the_phoenix.append(last_name)
elif house == "" and instructor_status == "TA":
tas.append(last_name)
elif house == "" and instructor_status == "I":
instructors.append(last_name)
cohort_data.close()
return all_students
students_by_house("cohort_data.txt")
def all_students_tuple_list(filename):
"""TODO: Create a list of tuples of student data.
Iterates over the data to create a big list of tuples that individually
hold all the data for each person. (full_name, house, advisor, cohort)
ex. all_people = [
("Alice Tsao", "Slytherin", "Kristen", "Winter 2015"),
("Amanda Gilmore", "Hufflepuff", "Meggie", "Winter 2015"),
# ...
]
"""
student_list = []
cohort_data = open(filename)
for line in cohort_data:
line = line.rstrip()
fellow = line.split("|")
student_info = [fellow[0]+" "+fellow[1],fellow[2],fellow[3],fellow[4]]
student_list.append(tuple(student_info))
cohort_data.close()
return student_list
all_students_tuple_list("cohort_data.txt")
def find_cohort_by_student_name(student_list):
"""TODO: Given full name, return student's cohort.
Use the above list of tuples generated by the preceding function to make a small
function that, given a first and last name, returns that student's cohort, or returns
'Student not found.' when appropriate. """
# Code goes here
return "Student not found."
##########################################################################################
# Further Study Questions
def find_name_duplicates(filename):
"""TODO: Using set operations, make a set of student first names that have duplicates.
Iterates over the data to find any first names that exist across multiple cohorts.
Uses set operations (set math) to create a set of these names.
NOTE: Do not include staff -- or do, if you want a greater challenge.
ex. duplicate_names = set(["Sarah"])
"""
duplicate_names = set()
# Code goes here
return duplicate_names
def find_house_members_by_student_name(student_list):
"""TODO: Create a function that prompts the user for a 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, print students that are in both
that student's cohort and that student's house."""
# Code goes here
return
#########################################################################################
# Here is some useful code to run these functions!
# print unique_houses("cohort_data.txt")
# print sort_by_cohort("cohort_data.txt")
# print students_by_house("cohort_data.txt")
# all_students_data = all_students_tuple_list("cohort_data.txt")
# print all_students_data
# find_cohort_by_student_name(all_students_data)
# print find_name_duplicates("cohort_data.txt")
# find_house_members_by_student_name(all_students_data)