Merge pull request #107 from 30-seconds/fixes

Resolves #99
Resolves #103
Resolves #104
This commit is contained in:
Angelos Chalaris
2019-09-22 15:44:31 +03:00
committed by GitHub
8 changed files with 19 additions and 32 deletions

View File

@ -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}
```
```

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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