From 90d36d46ca5894a5501892e9373a05bcabc187a7 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 22 Sep 2019 15:24:39 +0300 Subject: [PATCH 1/8] Update group_by.md --- snippets/group_by.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/group_by.md b/snippets/group_by.md index 6bd8ec4d5..c76c39254 100644 --- a/snippets/group_by.md +++ b/snippets/group_by.md @@ -5,7 +5,7 @@ tags: list,object,beginner Groups the elements of a list based on the given function. -Use `list()` in combination with `map()` and `fn` to map the values of the list to the keys of an object. +Use `map()` and `fn` to map the values of the list to the keys of an object. Use list comprehension to map each element to the appropriate `key`. ```py From 3e901677d7e61847d499b76d8d7efdf988b727ed Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 22 Sep 2019 15:29:50 +0300 Subject: [PATCH 2/8] Update none.md --- snippets/none.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/snippets/none.md b/snippets/none.md index b9644e197..b12512d2b 100644 --- a/snippets/none.md +++ b/snippets/none.md @@ -5,15 +5,11 @@ tags: list,function,intermediate Returns `False` if the provided function returns `True` for at least one element in the list, `True` otherwise. -Iterate over the elements of the list to test if every element in the list returns `False` based on `fn`. -Omit the seconds argument, `fn`, to check if all elements are `False`. +Use `all()` in combination with `map()` and `fn` to check if `fn` returns `False` for all the elements in the list. ```py -def none(lst, fn=lambda x: not not x): - for el in lst: - if fn(el): - return False - return True +def none(lst, fn=lambda x: x): + return all(map(lambda x: not fn(x), lst)) ``` ```py From 10422852cc5e61c0afda6dd76c473292a138e358 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 22 Sep 2019 15:31:11 +0300 Subject: [PATCH 3/8] Update some.md --- snippets/some.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/snippets/some.md b/snippets/some.md index 3e4769a72..6d6e16ef1 100644 --- a/snippets/some.md +++ b/snippets/some.md @@ -5,15 +5,11 @@ tags: list,function,intermediate Returns `True` if the provided function returns `True` for at least one element in the list, `False` otherwise. -Iterate over the elements of the list to test if every element in the list returns `True` based on `fn`. -Omit the seconds argument, `fn`, to check if all elements are `True`. +Use `any()` in combination with `map()` and `fn` to check if `fn` returns `True` for any element in the list. ```py -def some(lst, fn=lambda x: not not x): - for el in lst: - if fn(el): - return True - return False +def some(lst, fn=lambda x: x): + return any(map(fn, lst)) ``` ```py From 93823602f7a012bd6424795fb3e11b08406662f6 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 22 Sep 2019 15:32:10 +0300 Subject: [PATCH 4/8] Resolves #104 --- snippets/every.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/snippets/every.md b/snippets/every.md index 322d8b081..dde0f1a03 100644 --- a/snippets/every.md +++ b/snippets/every.md @@ -5,15 +5,11 @@ tags: list,function,intermediate Returns `True` if the provided function returns `True` for every element in the list, `False` otherwise. -Iterate over the elements of the list to test if every element in the list returns `True` based on `fn`. -Omit the seconds argument, `fn`, to check if all elements are `True`. +Use `all()` in combination with `map` and `fn` to check if `fn` returns `True` for all elements in the list. ```py -def every(lst, fn=lambda x: not not x): - for el in lst: - if not fn(el): - return False - return True +def every(lst, fn=lambda x: x): + return all(map(fn, lst)) ``` ```py From 5ad7ee960b53d464b67f669d41538c53203c0586 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 22 Sep 2019 15:34:52 +0300 Subject: [PATCH 5/8] Update min_by.md --- snippets/min_by.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/min_by.md b/snippets/min_by.md index 810306b1d..9b0015ff8 100644 --- a/snippets/min_by.md +++ b/snippets/min_by.md @@ -5,11 +5,11 @@ tags: math,list,function,beginner Returns the minimum value of a list, after mapping each element to a value using the provided function. -Use `map()` with `fn` to map each element to a value using the provided function, convert to a `list` and use `min()` to return the minimum value. +Use `map()` with `fn` to map each element to a value using the provided function, use `min()` to return the minimum value. ```py def min_by(lst, fn): - return min(list(map(fn,lst))) + return min(map(fn,lst)) ``` ```py From 159cc26814efe653401e2ad0242c2086e97eb2e7 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 22 Sep 2019 15:35:10 +0300 Subject: [PATCH 6/8] Update max_by.md --- snippets/max_by.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/max_by.md b/snippets/max_by.md index dce10cb38..d611f8d13 100644 --- a/snippets/max_by.md +++ b/snippets/max_by.md @@ -5,11 +5,11 @@ tags: math,list,function,beginner Returns the maximum value of a list, after mapping each element to a value using the provided function. -Use `map()` with `fn` to map each element to a value using the provided function, convert to a `list` and use `max()` to return the maximum value. +Use `map()` with `fn` to map each element to a value using the provided function, use `max()` to return the maximum value. ```py def max_by(lst, fn): - return max(list(map(fn,lst))) + return max(map(fn,lst)) ``` ```py From 9b6cec690fa5741d53a5f0651d4cbc24511d1935 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 22 Sep 2019 15:36:06 +0300 Subject: [PATCH 7/8] Resolves #103 --- snippets/sum_by.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/sum_by.md b/snippets/sum_by.md index 90de69d0c..8456c414f 100644 --- a/snippets/sum_by.md +++ b/snippets/sum_by.md @@ -5,11 +5,11 @@ tags: math,list,function,beginner Returns the sum of a list, after mapping each element to a value using the provided function. -Use `map()` with `fn` to map each element to a value using the provided function, convert to a `list` and use `sum()` to return the sum of the values. +Use `map()` with `fn` to map each element to a value using the provided function, use `sum()` to return the sum of the values. ```py def sum_by(lst, fn): - return sum(list(map(fn,lst))) + return sum(map(fn,lst)) ``` ```py From efeabbc40e1189da1c7ce01a752929e3ede0f18b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 22 Sep 2019 15:41:44 +0300 Subject: [PATCH 8/8] Resolvs #99 --- snippets/count_by.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/snippets/count_by.md b/snippets/count_by.md index f53b6d67a..9d50bf8a7 100644 --- a/snippets/count_by.md +++ b/snippets/count_by.md @@ -12,13 +12,12 @@ Iterate over the map and increase the element count each time it occurs. def count_by(arr, fn=lambda x: x): key = {} for el in map(fn, arr): - key[el] = 0 if el not in key else key[el] - key[el] += 1 + key[el] = 1 if el not in key else key[el] + 1 return key ``` ```py from math import floor -count_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2} +count_by([6.1, 4.2, 6.3], floor) # {6: 2, 4: 1} count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1} -``` \ No newline at end of file +```