Update formatting
Update formatting in every_nth Update formatting in shuffle Update formatting in has_duplicates Update formatting in group_by Update formatting in sum_by Update formatting in zip Update formatting in longest_item Update formatting in bifurcate_by Update formatting in difference_by Update formatting in clamp_number Update formatting in min_by Update formatting in max_by Update formatting in union Update formatting in n_times_string Update formatting in check_prop Update formatting in chunk Update formatting in transpose Update formatting in bifurcate Update formatting in union_by Update formatting in initialize_list_with_range Update formatting in most_frequent
This commit is contained in:
@ -11,8 +11,8 @@ Use list comprehension and `enumerate()` to add elements to groups, based on `fi
|
||||
```py
|
||||
def bifurcate(lst, filter):
|
||||
return [
|
||||
[x for i,x in enumerate(lst) if filter[i] == True],
|
||||
[x for i,x in enumerate(lst) if filter[i] == False]
|
||||
[x for i, x in enumerate(lst) if filter[i] == True],
|
||||
[x for i, x in enumerate(lst) if filter[i] == False]
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
@ -17,5 +17,8 @@ def bifurcate_by(lst, fn):
|
||||
```
|
||||
|
||||
```py
|
||||
bifurcate_by(['beep', 'boop', 'foo', 'bar'], lambda x: x[0] == 'b') # [ ['beep', 'boop', 'bar'], ['foo'] ]
|
||||
bifurcate_by(
|
||||
['beep', 'boop', 'foo', 'bar'],
|
||||
lambda x: x[0] == 'b'
|
||||
) # [ ['beep', 'boop', 'bar'], ['foo'] ]
|
||||
```
|
||||
|
||||
@ -14,10 +14,7 @@ def check_prop(fn, prop):
|
||||
|
||||
```py
|
||||
check_age = check_prop(lambda x: x >= 18, 'age')
|
||||
user = {
|
||||
'name': 'Mark',
|
||||
'age': 18
|
||||
}
|
||||
user = {'name': 'Mark', 'age': 18}
|
||||
|
||||
check_age(user) # True
|
||||
```
|
||||
|
||||
@ -19,5 +19,5 @@ def chunk(lst, size):
|
||||
```
|
||||
|
||||
```py
|
||||
chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]
|
||||
chunk([1, 2, 3, 4, 5], 2) # [[1,2],[3,4],5]
|
||||
```
|
||||
|
||||
@ -10,7 +10,7 @@ Otherwise, return the nearest number in the range.
|
||||
|
||||
```py
|
||||
def clamp_number(num,a,b):
|
||||
return max(min(num, max(a,b)),min(a,b))
|
||||
return max(min(num, max(a, b)), min(a, b))
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -15,6 +15,6 @@ def difference_by(a, b, fn):
|
||||
|
||||
```py
|
||||
from math import floor
|
||||
difference_by([2.1, 1.2], [2.3, 3.4],floor) # [1.2]
|
||||
difference_by([2.1, 1.2], [2.3, 3.4], floor) # [1.2]
|
||||
difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x: 2 } ]
|
||||
```
|
||||
@ -9,7 +9,7 @@ Use `[nth-1::nth]` to create a new list that contains every nth element of the g
|
||||
|
||||
```py
|
||||
def every_nth(lst, nth):
|
||||
return lst[nth-1::nth]
|
||||
return lst[nth - 1::nth]
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -10,11 +10,11 @@ Use list comprehension to map each element to the appropriate `key`.
|
||||
|
||||
```py
|
||||
def group_by(lst, fn):
|
||||
return {key : [el for el in lst if fn(el) == key] for key in map(fn,lst)}
|
||||
return {key : [el for el in lst if fn(el) == key] for key in map(fn, lst)}
|
||||
```
|
||||
|
||||
```py
|
||||
import math
|
||||
group_by([6.1, 4.2, 6.3], math.floor) # {4: [4.2], 6: [6.1, 6.3]}
|
||||
from math import floor
|
||||
group_by([6.1, 4.2, 6.3], floor) # {4: [4.2], 6: [6.1, 6.3]}
|
||||
group_by(['one', 'two', 'three'], len) # {3: ['one', 'two'], 5: ['three']}
|
||||
```
|
||||
|
||||
@ -13,8 +13,8 @@ def has_duplicates(lst):
|
||||
```
|
||||
|
||||
```py
|
||||
x = [1,2,3,4,5,5]
|
||||
y = [1,2,3,4,5]
|
||||
x = [1, 2, 3, 4, 5, 5]
|
||||
y = [1, 2, 3, 4, 5]
|
||||
has_duplicates(x) # True
|
||||
has_duplicates(y) # False
|
||||
```
|
||||
|
||||
@ -10,12 +10,12 @@ Omit `start` to use the default value of `0`.
|
||||
Omit `step` to use the default value of `1`.
|
||||
|
||||
```py
|
||||
def initialize_list_with_range(end, start = 0, step = 1):
|
||||
def initialize_list_with_range(end, start=0, step=1):
|
||||
return list(range(start, end + 1, step))
|
||||
```
|
||||
|
||||
```py
|
||||
initialize_list_with_range(5) # [0, 1, 2, 3, 4, 5]
|
||||
initialize_list_with_range(7,3) # [3, 4, 5, 6, 7]
|
||||
initialize_list_with_range(9,0,2) # [0, 2, 4, 6, 8]
|
||||
initialize_list_with_range(7, 3) # [3, 4, 5, 6, 7]
|
||||
initialize_list_with_range(9, 0, 2) # [0, 2, 4, 6, 8]
|
||||
```
|
||||
|
||||
@ -10,7 +10,7 @@ Use `max()` with `len` as the `key` to return the item with the greatest length.
|
||||
|
||||
```py
|
||||
def longest_item(*args):
|
||||
return max(args, key = len)
|
||||
return max(args, key=len)
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -9,7 +9,7 @@ Use `map()` with `fn` to map each element to a value using the provided function
|
||||
|
||||
```py
|
||||
def max_by(lst, fn):
|
||||
return max(map(fn,lst))
|
||||
return max(map(fn, lst))
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -9,7 +9,7 @@ Use `map()` with `fn` to map each element to a value using the provided function
|
||||
|
||||
```py
|
||||
def min_by(lst, fn):
|
||||
return min(map(fn,lst))
|
||||
return min(map(fn, lst))
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -9,9 +9,9 @@ Use `set(list)` to get the unique values in the `list` combined with `max()` to
|
||||
|
||||
```py
|
||||
def most_frequent(list):
|
||||
return max(set(list), key = list.count)
|
||||
return max(set(list), key=list.count)
|
||||
```
|
||||
|
||||
```py
|
||||
most_frequent([1,2,1,2,3,2,1,4,2]) #2
|
||||
most_frequent([1, 2, 1, 2, 3, 2, 1, 4, 2]) #2
|
||||
```
|
||||
|
||||
@ -14,5 +14,4 @@ def n_times_string(s, n):
|
||||
|
||||
```py
|
||||
n_times_string('py', 4) #'pypypypy'
|
||||
|
||||
```
|
||||
@ -23,5 +23,5 @@ def shuffle(lst):
|
||||
|
||||
```py
|
||||
foo = [1,2,3]
|
||||
shuffle(foo) # [2,3,1] , foo = [1,2,3]
|
||||
shuffle(foo) # [2,3,1], foo = [1,2,3]
|
||||
```
|
||||
|
||||
@ -9,7 +9,7 @@ Use `map()` with `fn` to map each element to a value using the provided function
|
||||
|
||||
```py
|
||||
def sum_by(lst, fn):
|
||||
return sum(map(fn,lst))
|
||||
return sum(map(fn, lst))
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -8,7 +8,7 @@ Returns every element that exists in any of the two lists once.
|
||||
Create a `set` with all values of `a` and `b` and convert to a `list`.
|
||||
|
||||
```py
|
||||
def union(a,b):
|
||||
def union(a, b):
|
||||
return list(set(a + b))
|
||||
```
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ Create a `set` by applying `fn` to each element in `a`, then use list comprehens
|
||||
Finally, create a `set` from the previous result and `a` and transform it into a `list`
|
||||
|
||||
```py
|
||||
def union_by(a,b,fn):
|
||||
def union_by(a, b, fn):
|
||||
_a = set(map(fn, a))
|
||||
return list(set(a + [item for item in b if fn(item) not in _a]))
|
||||
```
|
||||
|
||||
@ -5,7 +5,6 @@ tags: list,math,intermediate
|
||||
|
||||
Creates a list of elements, grouped based on the position in the original lists.
|
||||
|
||||
|
||||
Use `max` combined with `list comprehension` to get the length of the longest list in the arguments.
|
||||
Loop for `max_length` times grouping elements.
|
||||
If lengths of `lists` vary, use `fill_value` (defaults to `None`).
|
||||
|
||||
Reference in New Issue
Block a user