diff --git a/tasks/collections1.py b/tasks/collections1.py index 372c529..4b5c23e 100644 --- a/tasks/collections1.py +++ b/tasks/collections1.py @@ -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]) \ No newline at end of file diff --git a/tasks/data_generator.py b/tasks/data_generator.py index 3ecc78f..62e7e87 100644 --- a/tasks/data_generator.py +++ b/tasks/data_generator.py @@ -1,7 +1,8 @@ +""" +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) @@ -9,7 +10,7 @@ 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: @@ -27,5 +28,42 @@ """ + class DataGenerator: - pass \ No newline at end of file + + 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)) diff --git a/tasks/dict_comp1.py b/tasks/dict_comp1.py index b8e3b85..6d84546 100644 --- a/tasks/dict_comp1.py +++ b/tasks/dict_comp1.py @@ -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 \ No newline at end of file + 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)) diff --git a/tasks/recursion1.py b/tasks/recursion1.py index 28c00a9..1bbbc84 100644 --- a/tasks/recursion1.py +++ b/tasks/recursion1.py @@ -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"), @@ -26,7 +25,7 @@ def construct_trees(nodes:list): "node2": { "node4": { - + } }, "node5": {}, @@ -43,12 +42,12 @@ 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! @@ -56,11 +55,43 @@ def construct_trees(nodes:list): """ - 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))