From 4874a2d22d791aa23beaaa06b0a44c8e2d272381 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 16 Mar 2020 19:48:15 +0200 Subject: [PATCH 1/4] Add is_contained_in --- snippets/is_contained_in.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/is_contained_in.md diff --git a/snippets/is_contained_in.md b/snippets/is_contained_in.md new file mode 100644 index 000000000..93550ddf9 --- /dev/null +++ b/snippets/is_contained_in.md @@ -0,0 +1,20 @@ +--- +title: is_contained_in +tags: list,intermediate +--- + +Returns `True` if the elements of the first λιστ are contained in the second one regardless of order, `False` otherwise. + +Use `count()` to check if any value in `a` has more occurences than it has in `b`, returing `False` if any such value is found, `True` otherwise. + +```py +def is_contained_in(a, b): + for v in set(a): + if a.count(v) > b.count(v): + return False + return True +``` + +```py +is_contained_in([1, 4], [2, 4, 1]) # True +``` From 3de4296dc8be2950e9310ecec9b64c421d0118ec Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 16 Mar 2020 19:51:03 +0200 Subject: [PATCH 2/4] Add map_object --- snippets/map_object.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/map_object.md diff --git a/snippets/map_object.md b/snippets/map_object.md new file mode 100644 index 000000000..5eebb5843 --- /dev/null +++ b/snippets/map_object.md @@ -0,0 +1,20 @@ +--- +title: map_object +tags: list,intermediate +--- + +Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the value as the key and the mapped value. + +Use a `for` loop to iterate over the list's values, assigning the values produced by `fn` to each key of the dictionary. + +```py +def map_object(itr, fn): + ret = {} + for x in itr: + ret[x] = fn(x) + return ret +``` + +```py +map_object([1,2,3], lambda x: x * x) # { 1: 1, 2: 4, 3: 9 } +``` From d5878dbb143c6d98ee782c72fad4ca5e29348007 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 16 Mar 2020 19:52:44 +0200 Subject: [PATCH 3/4] Add take, take_right --- snippets/take.md | 18 ++++++++++++++++++ snippets/take_right.md | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 snippets/take.md create mode 100644 snippets/take_right.md diff --git a/snippets/take.md b/snippets/take.md new file mode 100644 index 000000000..6b1f77b7b --- /dev/null +++ b/snippets/take.md @@ -0,0 +1,18 @@ +--- +title: take +tags: list,beginner +--- + +Returns a list with `n` elements removed from the beginning. + +Use slice notation to create a slice of the list with `n` elements taken from the beginning. + +```py +def take(itr, n = 1): + return itr[:n] +``` + +```py +take([1, 2, 3], 5) # [1, 2, 3] +take([1, 2, 3], 0) # [] +``` diff --git a/snippets/take_right.md b/snippets/take_right.md new file mode 100644 index 000000000..0e68fcbb8 --- /dev/null +++ b/snippets/take_right.md @@ -0,0 +1,18 @@ +--- +title: take_right +tags: list,beginner +--- + +Returns a list with `n` elements removed from the end. + +Use slice notation to create a slice of the list with `n` elements taken from the end. + +```py +def take_right(itr, n = 1): + return itr[-n:] +``` + +```py +take_right([1, 2, 3], 2) # [2, 3] +take_right([1, 2, 3]) # [3] +``` From 82d99f98cb0b59f2e767a132ac24b43a70ade92b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 16 Mar 2020 22:00:46 +0200 Subject: [PATCH 4/4] Apply suggestions from code review Co-Authored-By: Isabelle Viktoria Maciohsek --- snippets/is_contained_in.md | 6 ++++-- snippets/map_object.md | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/snippets/is_contained_in.md b/snippets/is_contained_in.md index 93550ddf9..1491bcb65 100644 --- a/snippets/is_contained_in.md +++ b/snippets/is_contained_in.md @@ -3,9 +3,11 @@ title: is_contained_in tags: list,intermediate --- -Returns `True` if the elements of the first λιστ are contained in the second one regardless of order, `False` otherwise. +Returns `True` if the elements of the first list are contained in the second one regardless of order, `False` otherwise. + + +Use `count()` to check if any value in `a` has more occurences than it has in `b`, returning `False` if any such value is found, `True` otherwise. -Use `count()` to check if any value in `a` has more occurences than it has in `b`, returing `False` if any such value is found, `True` otherwise. ```py def is_contained_in(a, b): diff --git a/snippets/map_object.md b/snippets/map_object.md index 5eebb5843..57eae334d 100644 --- a/snippets/map_object.md +++ b/snippets/map_object.md @@ -3,7 +3,8 @@ title: map_object tags: list,intermediate --- -Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the value as the key and the mapped value. +Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value. + Use a `for` loop to iterate over the list's values, assigning the values produced by `fn` to each key of the dictionary.