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 +``` 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 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 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 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 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 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 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