-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvillager_data.py
More file actions
197 lines (140 loc) · 5.28 KB
/
villager_data.py
File metadata and controls
197 lines (140 loc) · 5.28 KB
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
"""Functions to parse a file containing villager data."""
def all_species(filename):
"""Return a set of unique species in the given file.
Arguments:
- filename (str): the path to a data file
Return:
- set[str]: a set of strings
"""
species = set()
# TODO: replace this with your code
village_file = open(filename)
for line in village_file:
split_line = line.split('|')
species.add(split_line[1])
return species
def get_villagers_by_species(filename, search_string="All"):
"""Return a list of villagers' names by species.
Arguments:
- filename (str): the path to a data file
- search_string (str): optional, the name of a species
Return:
- list[str]: a list of names
"""
villagers = []
# TODO: replace this with your code
# Open csv file
village_file = open(filename)
# If search string is all, return all names
if search_string == "All":
for line in village_file:
line_split = line.split('|')
villagers.append(line_split[0])
# Else only return names of specific species
else:
for line in village_file:
line_split = line.split('|')
if search_string.lower() == line_split[1].lower():
villagers.append(line_split[0])
return sorted(villagers)
def all_names_by_hobby(filename):
"""Return a list of lists containing villagers' names, grouped by hobby.
Arguments:
- filename (str): the path to a data file
Return:
- list[list[str]]: a list of lists containing names
"""
# TODO: replace this with your code
villagers_by_hobby = []
hobbies = set()
hobbies_and_names = ()
with open(filename) as village_file:
# Make a set of the hobbies on the csv file
for line in village_file:
line_split = line.split('|')
hobbies.add(line_split[3])
# Make a tuple of the hobbies and names of villagers
hobbies_and_names = hobbies_and_names + (line_split[3],
line_split[0])
for hobby in hobbies:
villagers = []
for i in range(0, len(hobbies_and_names), 2):
# If hobby is same as hobbies set, add villagers name to list
if hobby.lower() == hobbies_and_names[i].lower():
villagers.append(hobbies_and_names[i + 1])
# Add villagers list to the villagers_by_hobby list
villagers_by_hobby.append(villagers)
return villagers_by_hobby
def all_data(filename):
"""Return all the data in a file.
Each line in the file is a tuple of (name, species, personality, hobby,
saying).
Arguments:
- filename (str): the path to a data file
Return:
- list[tuple[str]]: a list of tuples containing strings
"""
all_data = []
# TODO: replace this with your code
with open(filename) as village_file:
for line in village_file:
line_split = line.split("|")
villager = tuple(line_split)
all_data.append(villager)
return all_data
def find_motto(filename, villager_name):
"""Return the villager's motto.
Return None if you're not able to find a villager with the
given name.
Arguments:
- filename (str): the path to a data file
- villager_name (str): a villager's name
Return:
- str: the villager's motto or None
"""
# TODO: replace this with your code
# Find out if all names are unique
# names = []
# with open('villagers.csv') as v_f:
# for line in v_f:
# line_split = line.split('|')
# names.append(line_split[0])
# print(f"len names: {len(names)}")
# print(f"len names: {names}")
# print(f"set names: {len(set(names))}")
# print(f"set names: {set(names)}")
with open(filename) as village_file:
for line in village_file:
line_split = line.split('|')
# If villager_name is found on the list, return their hobby
if line_split[0].lower() == villager_name.lower():
return line_split[-1]
# If the name is not on the list, return none
return None
def find_likeminded_villagers(filename, villager_name):
"""Return a set of villagers with the same personality as the given villager.
Arguments:
- filename (str): the path to a data file
- villager_name (str): a villager's name
Return:
- set[str]: a set of names
For example:
>>> find_likeminded_villagers('villagers.csv', 'Wendy')
{'Bella', ..., 'Carmen'}
"""
# TODO: replace this with your code
# Store the personality of the villager
personality = ""
likeminded_villagers = []
file_data = all_data(filename)
# Find the personality of the input villager
for line in file_data:
if line[0].lower() == villager_name.lower():
personality = line[2]
break
# Make a list of all other villagers with same personality
for line in file_data:
if (line[2] == personality
and not line[0].lower() == villager_name.lower()):
likeminded_villagers.append(line[0])
return likeminded_villagers