From 00c283a8ceb9c59ff717de2680414ca05a6b2e0f Mon Sep 17 00:00:00 2001 From: Marije-Kouyzer Date: Tue, 1 Dec 2020 14:34:52 +0100 Subject: [PATCH 01/10] test text --- test_file.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test_file.txt diff --git a/test_file.txt b/test_file.txt new file mode 100644 index 0000000..e69de29 From 8d5b0f8eb468714f9e80cb09a6d4b21ecb74e18d Mon Sep 17 00:00:00 2001 From: boattown Date: Tue, 1 Dec 2020 14:58:54 +0100 Subject: [PATCH 02/10] klara tries to add something to test-branch --- test_klara.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 test_klara.txt diff --git a/test_klara.txt b/test_klara.txt new file mode 100644 index 0000000..3bfdb05 --- /dev/null +++ b/test_klara.txt @@ -0,0 +1 @@ +test klara From a7ad4f46c7ef40f07db95d894c96e3bc8af6c314 Mon Sep 17 00:00:00 2001 From: Marije-Kouyzer Date: Mon, 14 Dec 2020 17:51:58 +0100 Subject: [PATCH 03/10] solutions of collections1 task --- tasks/collections1.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tasks/collections1.py b/tasks/collections1.py index 372c529..39e03a5 100644 --- a/tasks/collections1.py +++ b/tasks/collections1.py @@ -1,4 +1,9 @@ +""" +Group 1. Collaboration with Klara, Marije, Sarab. +""" + +import collections def get_word_counts(docs): @@ -10,5 +15,9 @@ def get_word_counts(docs): 1) you have to use Collections 2) you have 4 lines of code, including return statement """ - pass + return collections.Counter([w for doc in docs for w in doc]) + +if __name__ == "__main__": + docs = [['w1','w2'], ['w4','w2','w3','w1']] + print(get_word_counts(docs)) From 6d735cd200f5a06332b09e2adbd9fb926aeef31a Mon Sep 17 00:00:00 2001 From: saraby Date: Mon, 14 Dec 2020 18:19:43 +0100 Subject: [PATCH 04/10] commiting recursion1.py --- tasks/recursion1.py | 61 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 15 deletions(-) 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)) From 0348d803ad9bc7ce5a88f2458f4a38ac5df2fb5b Mon Sep 17 00:00:00 2001 From: boattown Date: Mon, 14 Dec 2020 18:51:00 +0100 Subject: [PATCH 05/10] Solution to dict_comp1.py --- tasks/dict_comp1.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) 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)) From 30b28e3f923097a7f3d09d11c47b76f8d1c6a97c Mon Sep 17 00:00:00 2001 From: boattown Date: Mon, 14 Dec 2020 18:53:10 +0100 Subject: [PATCH 06/10] Solution to data_generator.py --- tasks/data_generator.py | 44 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) 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)) From 0a2ae745f43a6af0a5ca8dd15f9e704a79feba96 Mon Sep 17 00:00:00 2001 From: Marije-Kouyzer Date: Wed, 16 Dec 2020 15:25:32 +0100 Subject: [PATCH 07/10] new version of collections1.py --- tasks/collections1.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/tasks/collections1.py b/tasks/collections1.py index 39e03a5..4b5c23e 100644 --- a/tasks/collections1.py +++ b/tasks/collections1.py @@ -7,17 +7,13 @@ 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 - """ + 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]) - -if __name__ == "__main__": - docs = [['w1','w2'], ['w4','w2','w3','w1']] - print(get_word_counts(docs)) + return collections.Counter([w for doc in docs for w in doc]) \ No newline at end of file From ac8b8e4061000fc92d3c6bed550e09d8fc94aad9 Mon Sep 17 00:00:00 2001 From: saraby Date: Wed, 16 Dec 2020 19:21:43 +0100 Subject: [PATCH 08/10] remove test files --- .gitignore | 129 ------------------------------------------------- test_file.txt | 0 test_klara.txt | 1 - 3 files changed, 130 deletions(-) delete mode 100644 .gitignore delete mode 100644 test_file.txt delete mode 100644 test_klara.txt diff --git a/.gitignore b/.gitignore deleted file mode 100644 index b6e4761..0000000 --- a/.gitignore +++ /dev/null @@ -1,129 +0,0 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -pip-wheel-metadata/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -.python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ diff --git a/test_file.txt b/test_file.txt deleted file mode 100644 index e69de29..0000000 diff --git a/test_klara.txt b/test_klara.txt deleted file mode 100644 index 3bfdb05..0000000 --- a/test_klara.txt +++ /dev/null @@ -1 +0,0 @@ -test klara From fac98c2a772dc874715d327964fdb092510dbcc1 Mon Sep 17 00:00:00 2001 From: saraby Date: Wed, 16 Dec 2020 19:23:48 +0100 Subject: [PATCH 09/10] Create .gitignore --- data/.gitignore | 129 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 data/.gitignore diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..b6e4761 --- /dev/null +++ b/data/.gitignore @@ -0,0 +1,129 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ From 6c17c31a497aa069c6ceebf04945a48cf8c9b45f Mon Sep 17 00:00:00 2001 From: saraby Date: Wed, 16 Dec 2020 19:24:48 +0100 Subject: [PATCH 10/10] redo --- data/.gitignore => .gitignore | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data/.gitignore => .gitignore (100%) diff --git a/data/.gitignore b/.gitignore similarity index 100% rename from data/.gitignore rename to .gitignore