diff --git a/README.md b/README.md index 4af29ee3b..58b04c709 100644 --- a/README.md +++ b/README.md @@ -295,8 +295,7 @@ 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 ``` @@ -305,7 +304,7 @@ def count_by(arr, fn=lambda x: x): ```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} ``` @@ -419,15 +418,11 @@ difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x 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)) ```
@@ -508,7 +503,7 @@ flatten([[1,2,3,4],[5,6,7,8]]) # [1, 2, 3, 4, 5, 6, 7, 8] 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 @@ -805,15 +800,11 @@ min_n([1, 2, 3], 2) # [1,2] 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)) ```
@@ -928,15 +919,11 @@ similarity([1, 2, 3], [1, 2, 4]) # [1, 2] 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)) ```
@@ -1453,11 +1440,11 @@ lcm([1, 3, 4], 5) # 60 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)) ```
@@ -1474,11 +1461,11 @@ max_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 8 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)) ```
@@ -1519,11 +1506,11 @@ rads_to_degrees(math.pi / 2) # 90.0 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)) ```
diff --git a/snippet_data/snippetList.json b/snippet_data/snippetList.json index 0649ed1e4..e6aba8791 100644 --- a/snippet_data/snippetList.json +++ b/snippet_data/snippetList.json @@ -228,7 +228,7 @@ ] }, "meta": { - "hash": "e78cb9229a2bc4c882fa7d901dbdee6ca9c33bde348650210afb210d3b98e1f6" + "hash": "bcbf6bf4eea87d50914f20640482d6e25198357ac3ec5155276664905ac1e5e0" } }, { @@ -329,7 +329,7 @@ "type": "snippetListing", "title": "every", "attributes": { - "text": "Returns `True` if the provided function returns `True` for every element in the list, `False` otherwise.\n\nIterate over the elements of the list to test if every element in the list returns `True` based on `fn`.\nOmit the seconds argument, `fn`, to check if all elements are `True`.\n\n", + "text": "Returns `True` if the provided function returns `True` for every element in the list, `False` otherwise.\n\nUse `all()` in combination with `map` and `fn` to check if `fn` returns `True` for all elements in the list.\n\n", "tags": [ "list", "function", @@ -337,7 +337,7 @@ ] }, "meta": { - "hash": "8356afa6609f21bef9f48c7fb4b82553cae27d67c4bb4e31bfda98ee814cf269" + "hash": "40b8ce30ab95e515b6814e602a7f0755124b792d6f9c664f58e0ee61c27148c1" } }, { @@ -437,7 +437,7 @@ "type": "snippetListing", "title": "group_by", "attributes": { - "text": "Groups the elements of a list based on the given function.\n\nUse `list()` in combination with `map()` and `fn` to map the values of the list to the keys of an object.\nUse list comprehension to map each element to the appropriate `key`.\n\n", + "text": "Groups the elements of a list based on the given function.\n\nUse `map()` and `fn` to map the values of the list to the keys of an object.\nUse list comprehension to map each element to the appropriate `key`.\n\n", "tags": [ "list", "object", @@ -445,7 +445,7 @@ ] }, "meta": { - "hash": "b3d1a5b7cd2f3b25f9d89efeba7f4eb7f9e4e6d81e108d57f94770c8f979428e" + "hash": "51e158e03a090b891d6d1646644bff5163751b25a7ee3e72280c04570bff2429" } }, { @@ -778,7 +778,7 @@ "type": "snippetListing", "title": "max_by", "attributes": { - "text": "Returns the maximum value of a list, after mapping each element to a value using the provided function.\n\nUse `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.\n\n", + "text": "Returns the maximum value of a list, after mapping each element to a value using the provided function.\n\nUse `map()` with `fn` to map each element to a value using the provided function, use `max()` to return the maximum value.\n\n", "tags": [ "math", "list", @@ -787,7 +787,7 @@ ] }, "meta": { - "hash": "2b9a695e5e8982d8bf6739fe682834dc715c4e5d62c00c433a137e6c659847eb" + "hash": "aca578491d9c055c793cf782db88f7e0c1de093c3e9ad09a72a59e75055b7581" } }, { @@ -811,7 +811,7 @@ "type": "snippetListing", "title": "min_by", "attributes": { - "text": "Returns the minimum value of a list, after mapping each element to a value using the provided function.\n\nUse `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.\n\n", + "text": "Returns the minimum value of a list, after mapping each element to a value using the provided function.\n\nUse `map()` with `fn` to map each element to a value using the provided function, use `min()` to return the minimum value.\n\n", "tags": [ "math", "list", @@ -820,7 +820,7 @@ ] }, "meta": { - "hash": "d203738604555c011870d9d43d6030846fd71d5421eb477d349e295f28c32b92" + "hash": "fb061014007b8c476b42e8c7f8619a6a203f89e6141960a80292dc362ad05f80" } }, { @@ -844,7 +844,7 @@ "type": "snippetListing", "title": "none", "attributes": { - "text": "Returns `False` if the provided function returns `True` for at least one element in the list, `True` otherwise.\n\nIterate over the elements of the list to test if every element in the list returns `False` based on `fn`.\nOmit the seconds argument, `fn`, to check if all elements are `False`.\n\n", + "text": "Returns `False` if the provided function returns `True` for at least one element in the list, `True` otherwise.\n\nUse `all()` in combination with `map()` and `fn` to check if `fn` returns `False` for all the elements in the list.\n\n", "tags": [ "list", "function", @@ -852,7 +852,7 @@ ] }, "meta": { - "hash": "2f345a768997fb0523dfbb775281ca0338a849032b5d8671d313c08f46f9b26e" + "hash": "5d0682964ee65801c79ed3f165c5c699d61176928fb5bb59e0b02717293b5253" } }, { @@ -968,7 +968,7 @@ "type": "snippetListing", "title": "some", "attributes": { - "text": "Returns `True` if the provided function returns `True` for at least one element in the list, `False` otherwise.\n\nIterate over the elements of the list to test if every element in the list returns `True` based on `fn`.\nOmit the seconds argument, `fn`, to check if all elements are `True`.\n\n", + "text": "Returns `True` if the provided function returns `True` for at least one element in the list, `False` otherwise.\n\nUse `any()` in combination with `map()` and `fn` to check if `fn` returns `True` for any element in the list.\n\n", "tags": [ "list", "function", @@ -976,7 +976,7 @@ ] }, "meta": { - "hash": "5052bf5e5a38a8a928ea6b3e42cecda2027d2123dd5e47764b354eafd3310dee" + "hash": "b104995e92213cbfeac2dce438e9d6f8fd13e18b8fb106789efa06f9285da2bb" } }, { @@ -1015,7 +1015,7 @@ "type": "snippetListing", "title": "sum_by", "attributes": { - "text": "Returns the sum of a list, after mapping each element to a value using the provided function.\n\nUse `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.\n\n", + "text": "Returns the sum of a list, after mapping each element to a value using the provided function.\n\nUse `map()` with `fn` to map each element to a value using the provided function, use `sum()` to return the sum of the values.\n\n", "tags": [ "math", "list", @@ -1024,7 +1024,7 @@ ] }, "meta": { - "hash": "99f5487bf6ed90107254caf6ac90c2f7930266203ae33e9b60de6d8ed1a2ba45" + "hash": "aa4e4a8d9b99ed0e2d04af5430613f1155329592f3d4f7c2bf76a659a6df7c17" } }, { diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index 2e4a9cd14..40346ad1a 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -294,8 +294,8 @@ "fileName": "count_by.md", "text": "Groups the elements of a list based on the given function and returns the count of elements in each group.\n\nUse `map()` to map the values of the given list using the given function.\nIterate over the map and increase the element count each time it occurs.\n\n", "codeBlocks": { - "code": "def count_by(arr, fn=lambda x: x):\n key = {}\n for el in map(fn, arr):\n key[el] = 0 if el not in key else key[el]\n key[el] += 1\n return key", - "example": "from math import floor\ncount_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2}\ncount_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}" + "code": "def count_by(arr, fn=lambda x: x):\n key = {}\n for el in map(fn, arr):\n key[el] = 1 if el not in key else key[el] + 1\n return key", + "example": "from math import floor\ncount_by([6.1, 4.2, 6.3], floor) # {6: 2, 4: 1}\ncount_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}" }, "tags": [ "list", @@ -303,7 +303,7 @@ ] }, "meta": { - "hash": "e78cb9229a2bc4c882fa7d901dbdee6ca9c33bde348650210afb210d3b98e1f6" + "hash": "bcbf6bf4eea87d50914f20640482d6e25198357ac3ec5155276664905ac1e5e0" } }, { @@ -435,9 +435,9 @@ "type": "snippet", "attributes": { "fileName": "every.md", - "text": "Returns `True` if the provided function returns `True` for every element in the list, `False` otherwise.\n\nIterate over the elements of the list to test if every element in the list returns `True` based on `fn`.\nOmit the seconds argument, `fn`, to check if all elements are `True`.\n\n", + "text": "Returns `True` if the provided function returns `True` for every element in the list, `False` otherwise.\n\nUse `all()` in combination with `map` and `fn` to check if `fn` returns `True` for all elements in the list.\n\n", "codeBlocks": { - "code": "def every(lst, fn=lambda x: not not x):\n for el in lst:\n if not fn(el):\n return False\n return True", + "code": "def every(lst, fn=lambda x: x):\n return all(map(fn, lst))", "example": "every([4, 2, 3], lambda x: x > 1) # True\nevery([1, 2, 3]) # True" }, "tags": [ @@ -447,7 +447,7 @@ ] }, "meta": { - "hash": "8356afa6609f21bef9f48c7fb4b82553cae27d67c4bb4e31bfda98ee814cf269" + "hash": "40b8ce30ab95e515b6814e602a7f0755124b792d6f9c664f58e0ee61c27148c1" } }, { @@ -578,7 +578,7 @@ "type": "snippet", "attributes": { "fileName": "group_by.md", - "text": "Groups the elements of a list based on the given function.\n\nUse `list()` in combination with `map()` and `fn` to map the values of the list to the keys of an object.\nUse list comprehension to map each element to the appropriate `key`.\n\n", + "text": "Groups the elements of a list based on the given function.\n\nUse `map()` and `fn` to map the values of the list to the keys of an object.\nUse list comprehension to map each element to the appropriate `key`.\n\n", "codeBlocks": { "code": "def group_by(lst, fn):\n return {key : [el for el in lst if fn(el) == key] for key in map(fn,lst)}", "example": "import math\ngroup_by([6.1, 4.2, 6.3], math.floor) # {4: [4.2], 6: [6.1, 6.3]}\ngroup_by(['one', 'two', 'three'], len) # {3: ['one', 'two'], 5: ['three']}" @@ -590,7 +590,7 @@ ] }, "meta": { - "hash": "b3d1a5b7cd2f3b25f9d89efeba7f4eb7f9e4e6d81e108d57f94770c8f979428e" + "hash": "51e158e03a090b891d6d1646644bff5163751b25a7ee3e72280c04570bff2429" } }, { @@ -1029,9 +1029,9 @@ "type": "snippet", "attributes": { "fileName": "max_by.md", - "text": "Returns the maximum value of a list, after mapping each element to a value using the provided function.\n\nUse `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.\n\n", + "text": "Returns the maximum value of a list, after mapping each element to a value using the provided function.\n\nUse `map()` with `fn` to map each element to a value using the provided function, use `max()` to return the maximum value.\n\n", "codeBlocks": { - "code": "def max_by(lst, fn):\n return max(list(map(fn,lst)))", + "code": "def max_by(lst, fn):\n return max(map(fn,lst))", "example": "max_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 8" }, "tags": [ @@ -1042,7 +1042,7 @@ ] }, "meta": { - "hash": "2b9a695e5e8982d8bf6739fe682834dc715c4e5d62c00c433a137e6c659847eb" + "hash": "aca578491d9c055c793cf782db88f7e0c1de093c3e9ad09a72a59e75055b7581" } }, { @@ -1072,9 +1072,9 @@ "type": "snippet", "attributes": { "fileName": "min_by.md", - "text": "Returns the minimum value of a list, after mapping each element to a value using the provided function.\n\nUse `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.\n\n", + "text": "Returns the minimum value of a list, after mapping each element to a value using the provided function.\n\nUse `map()` with `fn` to map each element to a value using the provided function, use `min()` to return the minimum value.\n\n", "codeBlocks": { - "code": "def min_by(lst, fn):\n return min(list(map(fn,lst)))", + "code": "def min_by(lst, fn):\n return min(map(fn,lst))", "example": "min_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 2" }, "tags": [ @@ -1085,7 +1085,7 @@ ] }, "meta": { - "hash": "d203738604555c011870d9d43d6030846fd71d5421eb477d349e295f28c32b92" + "hash": "fb061014007b8c476b42e8c7f8619a6a203f89e6141960a80292dc362ad05f80" } }, { @@ -1115,9 +1115,9 @@ "type": "snippet", "attributes": { "fileName": "none.md", - "text": "Returns `False` if the provided function returns `True` for at least one element in the list, `True` otherwise.\n\nIterate over the elements of the list to test if every element in the list returns `False` based on `fn`.\nOmit the seconds argument, `fn`, to check if all elements are `False`.\n\n", + "text": "Returns `False` if the provided function returns `True` for at least one element in the list, `True` otherwise.\n\nUse `all()` in combination with `map()` and `fn` to check if `fn` returns `False` for all the elements in the list.\n\n", "codeBlocks": { - "code": "def none(lst, fn=lambda x: not not x):\n for el in lst:\n if fn(el):\n return False\n return True", + "code": "def none(lst, fn=lambda x: x):\n return all(map(lambda x: not fn(x), lst))", "example": "none([0, 1, 2, 0], lambda x: x >= 2 ) # False\nnone([0, 0, 0]) # True" }, "tags": [ @@ -1127,7 +1127,7 @@ ] }, "meta": { - "hash": "2f345a768997fb0523dfbb775281ca0338a849032b5d8671d313c08f46f9b26e" + "hash": "5d0682964ee65801c79ed3f165c5c699d61176928fb5bb59e0b02717293b5253" } }, { @@ -1279,9 +1279,9 @@ "type": "snippet", "attributes": { "fileName": "some.md", - "text": "Returns `True` if the provided function returns `True` for at least one element in the list, `False` otherwise.\n\nIterate over the elements of the list to test if every element in the list returns `True` based on `fn`.\nOmit the seconds argument, `fn`, to check if all elements are `True`.\n\n", + "text": "Returns `True` if the provided function returns `True` for at least one element in the list, `False` otherwise.\n\nUse `any()` in combination with `map()` and `fn` to check if `fn` returns `True` for any element in the list.\n\n", "codeBlocks": { - "code": "def some(lst, fn=lambda x: not not x):\n for el in lst:\n if fn(el):\n return True\n return False", + "code": "def some(lst, fn=lambda x: x):\n return any(map(fn, lst))", "example": "some([0, 1, 2, 0], lambda x: x >= 2 ) # True\nsome([0, 0, 1, 0]) # True" }, "tags": [ @@ -1291,7 +1291,7 @@ ] }, "meta": { - "hash": "5052bf5e5a38a8a928ea6b3e42cecda2027d2123dd5e47764b354eafd3310dee" + "hash": "b104995e92213cbfeac2dce438e9d6f8fd13e18b8fb106789efa06f9285da2bb" } }, { @@ -1341,9 +1341,9 @@ "type": "snippet", "attributes": { "fileName": "sum_by.md", - "text": "Returns the sum of a list, after mapping each element to a value using the provided function.\n\nUse `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.\n\n", + "text": "Returns the sum of a list, after mapping each element to a value using the provided function.\n\nUse `map()` with `fn` to map each element to a value using the provided function, use `sum()` to return the sum of the values.\n\n", "codeBlocks": { - "code": "def sum_by(lst, fn):\n return sum(list(map(fn,lst)))", + "code": "def sum_by(lst, fn):\n return sum(map(fn,lst))", "example": "sum_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 20" }, "tags": [ @@ -1354,7 +1354,7 @@ ] }, "meta": { - "hash": "99f5487bf6ed90107254caf6ac90c2f7930266203ae33e9b60de6d8ed1a2ba45" + "hash": "aa4e4a8d9b99ed0e2d04af5430613f1155329592f3d4f7c2bf76a659a6df7c17" } }, {