Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group 1 #8

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
21 changes: 13 additions & 8 deletions tasks/collections1.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"""
Group 1. Collaboration with Klara, Marije, Sarab.

"""

import collections

def get_word_counts(docs):

"""
use the Collections python standard librar to count the words in the documents
docs = [[w1,w2], [w4,w2,w3,w1] ....]
"""
use the Collections python standard librar to count the words in the documents
docs = [[w1,w2], [w4,w2,w3,w1] ....]

restrictions:
1) you have to use Collections
2) you have 4 lines of code, including return statement
"""
pass
restrictions:
1) you have to use Collections
2) you have 4 lines of code, including return statement
"""

return collections.Counter([w for doc in docs for w in doc])
44 changes: 41 additions & 3 deletions tasks/data_generator.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

"""

Group 1. Collaboration with Klara, Marije, Sarab.

"""
create a class that given a list of text files will at each iteration return
the first line of the text only if the file is not suppose to be ignored (see example files below)

example:

lets assume we have the following files and that the first line of each file is the name of the file

files = ["file1.txt", "file2.txt", "ingore_this_file1.txt", "file3.txt", "ingore_this_file1.txt", "file4.txt"]
files = ["file1.txt", "file2.txt", "ignore_this_file1.txt", "file3.txt", "ignore_this_file1.txt", "file4.txt"]


following code should run:
Expand All @@ -27,5 +28,42 @@

"""


class DataGenerator:
pass

def __init__(self, data):
self.data = data
self.current = 0
self.end = len(self.data)

def __iter__(self):
for filename in self.data:
with open(filename) as f:
first_line = f.readline() # reads only the first line of the file
if "ignore" not in first_line: # only the files that should not be ignored
self.current +=1
yield first_line # gives back the first line of the file
else:
self.current += 1

def __next__(self):
if self.current == self.end:
raise StopIteration # stops the iteration when there are no files left
else:
if "ignore" in self.data[self.current]:
self.current += 1
return next(self) # if the current file should be ignored it continues to the next one
else:
filename = self.data[self.current]
with open(filename) as f:
first_line = f.readline()
return first_line

if __name__ == '__main__':
files = ["file1.txt", "file2.txt", "ignore_this_file1.txt", "file3.txt", "ignore_this_file2.txt", "file4.txt"]
my_generator = DataGenerator(files)
for data in my_generator:
print(data)
if "3" in data: # tests to see if files with ignore are ignored until you reach file3
break
print(next(my_generator))
17 changes: 12 additions & 5 deletions tasks/dict_comp1.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
"""
Group 1. Collaboration with Klara, Marije, Sarab.

"""


def merge_dicts(dict1,dict2,dict3):
"""
Change the for loop to a one line dict comprehension
"""

new_dict = {}
for d in [dict1,dict2,dict3]:
new_dict.update(d)

new_dict = {k: v for d in [dict1,dict2,dict3] for k, v in d.items()}

return new_dict
return new_dict

if __name__ == "__main__":
dict1 = {'1':1, '2':2, '3':3}
dict2 = {'a':'a', 'b':'b', 'c':'c'}
dict3 = {'w':'w', 'z':'z', 'y':'y'}
print(merge_dicts(dict1,dict2,dict3))
61 changes: 46 additions & 15 deletions tasks/recursion1.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import json



def construct_trees(nodes:list):

def construct_trees(nodes: list):
"""
given the following list where the first item in the tuple is the node_id and
given the following list where the first item in the tuple is the node_id and
the second item is the node_id of the parent; (NODE_ID, PARENT_NODE_ID)

nodes = [
("node1", "node1"),
("node2", "node1"),
Expand All @@ -26,7 +25,7 @@ def construct_trees(nodes:list):
"node2": {
"node4":
{

}
},
"node5": {},
Expand All @@ -43,24 +42,56 @@ def construct_trees(nodes:list):
}

}

}

Restrictions
1) place_node() should be a recusive function

Tips!
- let construct_trees() run until all nodes are places, or until there are no more
nodes to place!
- you might need a special condition to deal with the root of trees, e.g, nodes that have themselves as parents.

"""

def place_node():
#FIX THIS FUCNTION


#FIX THIS FUCNTION
pass

"""
Group 1. Collaboration with Klara, Marije, Sarab.
"""

def place_node(d: dict, node):
node_id, parent_id = node
if node_id == parent_id: # checks if this is a root
d[parent_id] = {} # place root nodes
return True
elif parent_id in d.keys(): # checks if parent node is already in the tree/subtrees
d[parent_id][node_id] = {} # places the nodes in the correct place
return True
for k in d:
# checks all the keys of the subtree until parent_id is found
if place_node(d[k], node):
return True
return False

trees = {}
while len(nodes) > 0:
node = nodes.pop(0)
if not place_node(trees, node): # checks if it is possible to place node now
nodes.append(node) # puts back nodes that weren't placed yet
return trees


if __name__ == '__main__':
nodes = [
("node1", "node1"),
("node2", "node1"),
("node3", "node8"),
("node4", "node2"),
("node5", "node1"),
("node6", "node7"),
("node7", "node7"),
("node8", "node9"),
("node9", "node6"),
]

print(json.dumps(construct_trees(nodes), indent=12, sort_keys=True))