diff --git a/snippets/bifurcate.md b/snippets/bifurcate.md index 2538d8728..d430470dc 100644 --- a/snippets/bifurcate.md +++ b/snippets/bifurcate.md @@ -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] ] ``` diff --git a/snippets/bifurcate_by.md b/snippets/bifurcate_by.md index c27c9afe8..fd141bb81 100644 --- a/snippets/bifurcate_by.md +++ b/snippets/bifurcate_by.md @@ -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'] ] ``` diff --git a/snippets/check_prop.md b/snippets/check_prop.md index cb98054bf..9994f0a32 100644 --- a/snippets/check_prop.md +++ b/snippets/check_prop.md @@ -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 ``` diff --git a/snippets/chunk.md b/snippets/chunk.md index 9a59250e9..35a4150ee 100644 --- a/snippets/chunk.md +++ b/snippets/chunk.md @@ -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] ``` diff --git a/snippets/clamp_number.md b/snippets/clamp_number.md index d965655db..44cf448a5 100644 --- a/snippets/clamp_number.md +++ b/snippets/clamp_number.md @@ -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 diff --git a/snippets/difference_by.md b/snippets/difference_by.md index 5805b3366..3baa2308f 100644 --- a/snippets/difference_by.md +++ b/snippets/difference_by.md @@ -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 } ] -``` \ No newline at end of file +``` diff --git a/snippets/every_nth.md b/snippets/every_nth.md index a97a6ab46..f747b8e83 100644 --- a/snippets/every_nth.md +++ b/snippets/every_nth.md @@ -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 diff --git a/snippets/group_by.md b/snippets/group_by.md index c76c39254..6329a672d 100644 --- a/snippets/group_by.md +++ b/snippets/group_by.md @@ -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']} ``` diff --git a/snippets/has_duplicates.md b/snippets/has_duplicates.md index 540226cce..3fe9cff7f 100644 --- a/snippets/has_duplicates.md +++ b/snippets/has_duplicates.md @@ -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 ``` diff --git a/snippets/initialize_list_with_range.md b/snippets/initialize_list_with_range.md index 747eccdd2..c258a4329 100644 --- a/snippets/initialize_list_with_range.md +++ b/snippets/initialize_list_with_range.md @@ -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] ``` diff --git a/snippets/longest_item.md b/snippets/longest_item.md index 04cd56e40..03ac167f5 100644 --- a/snippets/longest_item.md +++ b/snippets/longest_item.md @@ -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 diff --git a/snippets/max_by.md b/snippets/max_by.md index d611f8d13..ae743fa89 100644 --- a/snippets/max_by.md +++ b/snippets/max_by.md @@ -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 diff --git a/snippets/min_by.md b/snippets/min_by.md index 9b0015ff8..6147cf953 100644 --- a/snippets/min_by.md +++ b/snippets/min_by.md @@ -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 diff --git a/snippets/most_frequent.md b/snippets/most_frequent.md index 426335470..dfdb0cf2c 100644 --- a/snippets/most_frequent.md +++ b/snippets/most_frequent.md @@ -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 ``` diff --git a/snippets/n_times_string.md b/snippets/n_times_string.md index 10082ae88..38fcce7b7 100644 --- a/snippets/n_times_string.md +++ b/snippets/n_times_string.md @@ -14,5 +14,4 @@ def n_times_string(s, n): ```py n_times_string('py', 4) #'pypypypy' - -``` \ No newline at end of file +``` diff --git a/snippets/shuffle.md b/snippets/shuffle.md index f6464b7be..abbd2b3b6 100644 --- a/snippets/shuffle.md +++ b/snippets/shuffle.md @@ -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] ``` diff --git a/snippets/sum_by.md b/snippets/sum_by.md index 8456c414f..2f8078c33 100644 --- a/snippets/sum_by.md +++ b/snippets/sum_by.md @@ -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 diff --git a/snippets/transpose.md b/snippets/transpose.md index 40f90b096..c9bb8fc59 100644 --- a/snippets/transpose.md +++ b/snippets/transpose.md @@ -10,7 +10,7 @@ Use `zip()` in combination with `list()` to create the transpose of the given tw ```py def transpose(lst): - return list(zip(*lst)) + return list(zip(*lst)) ``` ```py diff --git a/snippets/union.md b/snippets/union.md index 9250cc042..7261b660f 100644 --- a/snippets/union.md +++ b/snippets/union.md @@ -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)) ``` diff --git a/snippets/union_by.md b/snippets/union_by.md index f28886c89..acc120f63 100644 --- a/snippets/union_by.md +++ b/snippets/union_by.md @@ -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])) ``` @@ -17,4 +17,4 @@ def union_by(a,b,fn): ```py from math import floor union_by([2.1], [1.2, 2.3], floor) # [2.1, 1.2] -``` \ No newline at end of file +``` diff --git a/snippets/zip.md b/snippets/zip.md index ba8c94cac..a9fb819d2 100644 --- a/snippets/zip.md +++ b/snippets/zip.md @@ -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`).