From 75ccc39b967e1e3900a48951344b4fb178f30864 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sat, 14 Mar 2020 11:33:17 +0200 Subject: [PATCH 1/4] Add for_each and for_each_right --- for_each.md | 18 ++++++++++++++++++ for_each_right.md | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 for_each.md create mode 100644 for_each_right.md diff --git a/for_each.md b/for_each.md new file mode 100644 index 000000000..0e72dc6ae --- /dev/null +++ b/for_each.md @@ -0,0 +1,18 @@ +--- +title: for_each +tags: list,beginner +--- + +Executes the provided function once for each list element. + +Use a `for` loop to execute `fn` for each element in `itr`. + +```py +def for_each(itr, fn): + for el in itr: + fn(el) +``` + +```py +for_each([1, 2, 3], print) # 1 2 3 +``` diff --git a/for_each_right.md b/for_each_right.md new file mode 100644 index 000000000..c95f7497b --- /dev/null +++ b/for_each_right.md @@ -0,0 +1,18 @@ +--- +title: for_each_right +tags: list,beginner +--- + +Executes the provided function once for each list element, starting from the list's last element. + +Use a `for` loop in combination with slice notation to execute `fn` for each element in `itr`, starting from the last one. + +```py +def for_each_right(itr, fn): + for el in itr[::-1]: + fn(el) +``` + +```py +for_each_right([1, 2, 3], print) # 3 2 1 +``` From b047b8f546273b0dff5ef585ec00398b3450c9de Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sat, 14 Mar 2020 11:33:48 +0200 Subject: [PATCH 2/4] Add frequencies --- frequencies.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 frequencies.md diff --git a/frequencies.md b/frequencies.md new file mode 100644 index 000000000..f260e7353 --- /dev/null +++ b/frequencies.md @@ -0,0 +1,22 @@ +--- +title: frequencies +tags: list,intermediate +--- + +Returns a dictionary with the unique values of a list as keys and their frequencies as the values. + +Use a `for` loop to populate a dictionary, `f`, with the unique values in `lst` as keys, adding to existing keys every time the same value is encountered. + +```py +from functools import reduce + +def frequencies(lst): + f = {} + for x in lst: + f[x] = f[x] + 1 if x in f else 1 + return f +``` + +```py +frequencies(['a', 'b', 'a', 'c', 'a', 'a', 'b']) # { 'a': 4, 'b': 2, 'c': 1 } +``` From 53c0329a89ebda2b960da2b2d9ed956434a9f850 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sat, 14 Mar 2020 11:34:52 +0200 Subject: [PATCH 3/4] Add have_same_contents, includes_all and includes_any --- have_same_contents.md | 22 ++++++++++++++++++++++ includes_all.md | 21 +++++++++++++++++++++ includes_any.md | 21 +++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 have_same_contents.md create mode 100644 includes_all.md create mode 100644 includes_any.md diff --git a/have_same_contents.md b/have_same_contents.md new file mode 100644 index 000000000..d29afa78e --- /dev/null +++ b/have_same_contents.md @@ -0,0 +1,22 @@ +--- +title: have_same_contents +tags: list,intermediate +--- + +Returns `True` if two lists contain the same elements regardless of order, `False` otherwise. + +Use `set()` on combination of both lists to find the unique values. +Iterate over them with a `for` loop comparing the `count()` of each unique value in each array. +Return `False` if the counts do not match for any element, `True` otherwise. + +```py +def have_same_contents(a, b): + for v in set(a + b): + if a.count(v) != b.count(v): + return False + return True +``` + +```py +have_same_contents([1, 2, 4], [2, 4, 1]) # True +``` diff --git a/includes_all.md b/includes_all.md new file mode 100644 index 000000000..d2a3eab41 --- /dev/null +++ b/includes_all.md @@ -0,0 +1,21 @@ +--- +title: includes_all +tags: utility,intermediate +--- + +Returns `True` if all the elements in `values` are included in `lst`, `False` otherwise. + +Check if every value in `values` is contained in `lst` using a `for` loop, returning `False` if any one value is not found, `True` otherwise. + +```py +def includes_all(lst, values): + for v in values: + if v not in lst: + return False + return True +``` + +```py +includes_all([1, 2, 3, 4], [1, 4]) # True +includes_all([1, 2, 3, 4], [1, 5]) # False +``` diff --git a/includes_any.md b/includes_any.md new file mode 100644 index 000000000..12c511973 --- /dev/null +++ b/includes_any.md @@ -0,0 +1,21 @@ +--- +title: includes_any +tags: list,intermediate +--- + +Returns `True` if any the elements in `values` are included in `lst`, `False` otherwise. + +Check if any value in `values` is contained in `lst` using a `for` loop, returning `True` if any one value is found, `False` otherwise. + +```py +def includes_any(lst, values): + for v in values: + if v in lst: + return True + return False +``` + +```py +includes_any([1, 2, 3, 4], [2, 9]) # True +includes_any([1, 2, 3, 4], [8, 9]) # False +``` From 223785a49ff4066f31897c199d41e9177ed10922 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sat, 14 Mar 2020 11:40:50 +0200 Subject: [PATCH 4/4] Apply suggestions from code review Co-Authored-By: Angelos Chalaris --- for_each.md | 2 +- for_each_right.md | 2 +- have_same_contents.md | 4 ++-- includes_any.md | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/for_each.md b/for_each.md index 0e72dc6ae..d4b36999d 100644 --- a/for_each.md +++ b/for_each.md @@ -14,5 +14,5 @@ def for_each(itr, fn): ``` ```py -for_each([1, 2, 3], print) # 1 2 3 +for_each([1, 2, 3], print) # 1 2 3 ``` diff --git a/for_each_right.md b/for_each_right.md index c95f7497b..64bb2e075 100644 --- a/for_each_right.md +++ b/for_each_right.md @@ -14,5 +14,5 @@ def for_each_right(itr, fn): ``` ```py -for_each_right([1, 2, 3], print) # 3 2 1 +for_each_right([1, 2, 3], print) # 3 2 1 ``` diff --git a/have_same_contents.md b/have_same_contents.md index d29afa78e..15f8baa57 100644 --- a/have_same_contents.md +++ b/have_same_contents.md @@ -5,8 +5,8 @@ tags: list,intermediate Returns `True` if two lists contain the same elements regardless of order, `False` otherwise. -Use `set()` on combination of both lists to find the unique values. -Iterate over them with a `for` loop comparing the `count()` of each unique value in each array. +Use `set()` on the combination of both lists to find the unique values. +Iterate over them with a `for` loop comparing the `count()` of each unique value in each list. Return `False` if the counts do not match for any element, `True` otherwise. ```py diff --git a/includes_any.md b/includes_any.md index 12c511973..459953b11 100644 --- a/includes_any.md +++ b/includes_any.md @@ -3,7 +3,7 @@ title: includes_any tags: list,intermediate --- -Returns `True` if any the elements in `values` are included in `lst`, `False` otherwise. +Returns `True` if any element in `values` is included in `lst`, `False` otherwise. Check if any value in `values` is contained in `lst` using a `for` loop, returning `True` if any one value is found, `False` otherwise.