diff --git a/.travis.yml b/.travis.yml index 9ddeaec2f..34b4dba24 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,12 @@ -sudo: false -language: python -cache: pip -python: - - "3.6" +language: node_js +cache: + directories: + - node_modules +node_js: +- lts/* script: - - python scripts/lint.py - - python scripts/readme.py - - python website/main.py - - python scripts/auto-lint.py +- npm run extractor +- npm run builder after_success: - - sed -i -e 's/\r$//' .travis/push.sh - - .travis/push.sh -env: - global: - - secure: RucoyHikFKD7yQq7FBHxOpwYSHSFYUO7QS/5VEXvU55AMnHwB97aI0DcQSMa+XHaSFVsqd4fR7nAP+5H6GMW/lUhIdeXIGzehgBTfuNeQmng2djGgS1lWY9fEOsn2XEL7JlXMi2P5YdZDpOAfiiLqT3W8EaCWDdV60tkizbSQhig2R3exI/649AjmGkIws+NqoYqrEfNpnTvgxJkp2jNuKfBkr0aaVdYuxdI6Kf2KnipEeuKsKJFds+tTjduEUKTg7I8lNSB+tQ9wIHNTDZffZrzODzE2esAZtnflxhkGQ6q7fW8DEj0rheuer+yD4WBWfph1CIxTL6B3VZgT6XQXCu09XzqgRUack0KIS6SBRKjRYJymH3eKNlxZGPpk4s90bX0Qo0a0vvcT4g/iejyVb917pcn2LjRZmmsFQUfJOcCJgU6EUvqNpfM9SWV8fJhaPOacvUnzDxFav3eRdDHaZYgXf0tzJfLAjsTv7rFbLZEnpqtvyKbHrXYLf9sICyPlHbCy4L5KAfguu735v0YPXko5Aabl6PvGcfafyLxVUb/0Y5ot3pLtGVJflfHeqYz8qbkoqp5RovSvTXntx/vVlx20TSE/rQP2l6JUNt98sGFJ+yOZ3SkMnyMdjE1YqeEngxZdzukec2SM3PtnaWxSbxV8Ue1XnM8D5nzhTf4UI8= +- chmod +x .travis/push.sh +- .travis/push.sh \ No newline at end of file diff --git a/.travis/push.sh b/.travis/push.sh index e6a4e9bc2..b797b8420 100755 --- a/.travis/push.sh +++ b/.travis/push.sh @@ -1,17 +1,15 @@ #!/bin/bash setup_git() { - git config --global user.email "mst10041967@gmail.com" - git config --global user.name "Rohit Tanwar" + git config --global user.email "30secondsofcode@gmail.com" + git config --global user.name "30secondsofcode" } commit_website_files() { if [ $TRAVIS_EVENT_TYPE != "pull_request" ]; then if [ $TRAVIS_BRANCH == "master" ]; then - git checkout master echo "Committing to master branch..." - git add -A - echo "All files added" - git status + git checkout master + git add * if [ $TRAVIS_EVENT_TYPE == "cron" ]; then git commit --message "Travis build: $TRAVIS_BUILD_NUMBER [cron]" elif [ $TRAVIS_EVENT_TYPE == "api" ]; then @@ -19,8 +17,6 @@ commit_website_files() { else git commit --message "Travis build: $TRAVIS_BUILD_NUMBER" fi - echo "Files commited" - git status fi fi } @@ -29,12 +25,7 @@ upload_files() { if [ $TRAVIS_EVENT_TYPE != "pull_request" ]; then if [ $TRAVIS_BRANCH == "master" ]; then echo "Pushing to master branch..." - git push --force "https://${GH_TOKEN}@github.com/kriadmin/30-seconds-of-python-code.git" master > /dev/null 2>&1 - echo "Pushing done" - echo "Pushing to website" - git subtree push --prefix website "https://${GH_TOKEN}@github.com/kriadmin/30-seconds-of-python-code.git" website - echo "Pushed to master branch" - git status + git push --force --quiet "https://${GH_TOKEN}@github.com/30-seconds/30-seconds-of-python-code.git" master > /dev/null 2>&1 fi fi } diff --git a/README.md b/README.md index 29283316a..a8a9753b3 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,10 @@ #### Related projects +* [30 Seconds of code](https://github.com/30-seconds/30-seconds-of-code) * [30 Seconds of CSS](https://30-seconds.github.io/30-seconds-of-css/) * [30 Seconds of Interviews](https://30secondsofinterviews.org/) * [30 Seconds of React](https://github.com/30-seconds/30-seconds-of-react) -* [30 Seconds of Python](https://github.com/30-seconds/30-seconds-of-python-code) * [30 Seconds of PHP](https://github.com/30-seconds/30-seconds-of-php-code) * [30 Seconds of Knowledge](https://chrome.google.com/webstore/detail/30-seconds-of-knowledge/mmgplondnjekobonklacmemikcnhklla) * [30 Seconds of Kotlin](https://github.com/IvanMwiruki/30-seconds-of-kotlin) _(unofficial)_ @@ -59,7 +59,11 @@ * [`similarity`](#similarity) * [`some`](#some) * [`spread`](#spread) +* [`symmetric_difference`](#symmetric_difference) +* [`symmetric_difference_by`](#symmetric_difference_by) * [`tail`](#tail) +* [`union`](#union) +* [`union_by`](#union_by) * [`unique_elements`](#unique_elements) * [`zip`](#zip) @@ -85,6 +89,7 @@ * [`max_by`](#max_by) * [`min_by`](#min_by) * [`rads_to_degrees`](#rads_to_degrees) +* [`sum_by`](#sum_by) @@ -105,13 +110,16 @@ View contents * [`byte_size`](#byte_size) +* [`camel`](#camel) * [`capitalize`](#capitalize) * [`capitalize_every_word`](#capitalize_every_word) * [`decapitalize`](#decapitalize) * [`is_anagram`](#is_anagram) * [`is_lower_case`](#is_lower_case) * [`is_upper_case`](#is_upper_case) +* [`kebab`](#kebab) * [`palindrome`](#palindrome) +* [`snake`](#snake) * [`split_lines`](#split_lines) @@ -137,18 +145,17 @@ Check if all elements in a list are equal. Use `[1:]` and `[:-1]` to compare all the values in the given list. -```python ```py -def all_equal(lst): +def all_equal(lst): return lst[1:] == lst[:-1] ``` -```
Examples -```python -undefined +```py +all_equal([1, 2, 3, 4, 5, 6]) # False +all_equal([1, 1, 1, 1]) # True ```
@@ -160,18 +167,19 @@ Returns `True` if all the values in a flat list are unique, `False` otherwise. Use `set()` on the given list to remove duplicates, compare its length with the length of the list. -```python ```py -def all_unique(lst): +def all_unique(lst): return len(lst) == len(set(lst)) ``` -```
Examples -```python -undefined +```py +x = [1,2,3,4,5,6] +y = [1,2,2,3,4,5] +all_unique(x) # True +all_unique(y) # False ```
@@ -184,21 +192,19 @@ If an element in `filter` is `True`, the corresponding element in the collection Use list comprehension and `enumerate()` to add elements to groups, based on `filter`. -```python ```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] +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] ] ``` -```
Examples -```python -undefined +```py +bifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True]) # [ ['beep', 'boop', 'bar'], ['foo'] ] ```
@@ -211,21 +217,19 @@ If the function returns `True`, the element belongs to the first group; otherwis Use list comprehension to add elements to groups, based on `fn`. -```python ```py -def bifurcate_by(lst, fn): - return [ - [x for x in lst if fn(x)], - [x for x in lst if not fn(x)] +def bifurcate_by(lst, fn): + return [ + [x for x in lst if fn(x)], + [x for x in lst if not fn(x)] ] ``` -```
Examples -```python -undefined +```py +bifurcate_by(['beep', 'boop', 'foo', 'bar'], lambda x: x[0] == 'b') # [ ['beep', 'boop', 'bar'], ['foo'] ] ```
@@ -239,22 +243,20 @@ Use `list()` and `range()` to create a list of the desired `size`. Use `map()` on the list and fill it with splices of the given list. Finally, return use created list. -```python ```py -from math import ceil - -def chunk(lst, size): - return list( - map(lambda x: lst[x * size:x * size + size], +from math import ceil + +def chunk(lst, size): + return list( + map(lambda x: lst[x * size:x * size + size], list(range(0, ceil(len(lst) / size))))) ``` -```
Examples -```python -undefined +```py +chunk([1,2,3,4,5],2) # [[1,2],[3,4],5] ```
@@ -266,18 +268,16 @@ Removes falsey values from a list. Use `filter()` to filter out falsey values (`False`, `None`, `0`, and `""`). -```python ```py -def compact(lst): +def compact(lst): return list(filter(bool, lst)) ``` -```
Examples -```python -undefined +```py +compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ] ```
@@ -290,22 +290,22 @@ Groups the elements of a list based on the given function and returns the count Use `map()` to map the values of the given list using the given function. Iterate over the map and increase the element count each time it occurs. -```python ```py -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 +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 return key ``` -```
Examples -```python -undefined +```py +from math import floor +count_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2} +count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1} ```
@@ -317,18 +317,16 @@ Counts the occurrences of a value in a list. Increment a counter for every item in the list that has the given value and is of the same type. -```python ```py -def count_occurrences(lst, val): +def count_occurrences(lst, val): return len([x for x in lst if x == val and type(x) == type(val)]) ``` -```
Examples -```python -undefined +```py +count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3 ```
@@ -343,30 +341,28 @@ Define a function, `spread`, that uses either `list.extend()` or `list.append()` Use `list.extend()` with an empty list and the `spread` function to flatten a list. Recursively flatten each element that is a list. -```python ```py -def spread(arg): - ret = [] - for i in arg: - if isinstance(i, list): - ret.extend(i) - else: - ret.append(i) - return ret - -def deep_flatten(lst): - result = [] - result.extend( - spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst)))) +def spread(arg): + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) + return ret + +def deep_flatten(lst): + result = [] + result.extend( + spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst)))) return result ``` -```
Examples -```python -undefined +```py +deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5] ```
@@ -378,19 +374,17 @@ Returns the difference between two iterables. Create a `set` from `b`, then use list comprehension on `a` to only keep values not contained in the previously created set, `_b`. -```python ```py -def difference(a, b): - _b = set(b) +def difference(a, b): + _b = set(b) return [item for item in a if item not in _b] ``` -```
Examples -```python -undefined +```py +difference([1, 2, 3], [1, 2, 4]) # [3] ```
@@ -402,19 +396,19 @@ Returns the difference between two lists, after applying the provided function t Create a `set` by applying `fn` to each element in `b`, then use list comprehension in combination with `fn` on `a` to only keep values not contained in the previously created set, `_b`. -```python ```py -def difference_by(a, b, fn): - _b = set(map(fn, b)) +def difference_by(a, b, fn): + _b = set(map(fn, b)) return [item for item in a if fn(item) not in _b] ``` -```
Examples -```python -undefined +```py +from math import floor +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 } ] ```
@@ -427,21 +421,20 @@ Returns `True` if the provided function returns `True` for every element in the 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`. -```python ```py -def every(lst, fn=lambda x: not not x): - for el in lst: - if not fn(el): - return False +def every(lst, fn=lambda x: not not x): + for el in lst: + if not fn(el): + return False return True ``` -```
Examples -```python -undefined +```py +every([4, 2, 3], lambda x: x > 1) # True +every([1, 2, 3]) # True ```
@@ -453,18 +446,16 @@ Returns every nth element in a list. Use `[1::nth]` to create a new list that contains every nth element of the given list. -```python ```py -def every_nth(lst, nth): +def every_nth(lst, nth): return lst[1::nth] ``` -```
Examples -```python -undefined +```py +every_nth([1, 2, 3, 4, 5, 6], 2) # [ 2, 4, 6 ] ```
@@ -476,18 +467,16 @@ Filters out the non-unique values in a list. Use list comprehension and `list.count()` to create a list containing only the unique values. -```python ```py -def filter_non_unique(lst): +def filter_non_unique(lst): return [item for item in lst if lst.count(item) == 1] ``` -```
Examples -```python -undefined +```py +filter_non_unique([1, 2, 2, 3, 4, 4, 5]) # [1, 3, 5] ```
@@ -500,21 +489,21 @@ 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 list comprehension to map each element to the appropriate `key`. -```python ```py -def group_by(lst, fn): - groups = {} - for key in list(map(fn,lst)): - groups[key] = [item for item in lst if fn(item) == key] +def group_by(lst, fn): + groups = {} + for key in list(map(fn,lst)): + groups[key] = [item for item in lst if fn(item) == key] return groups ``` -```
Examples -```python -undefined +```py +import math +group_by([6.1, 4.2, 6.3], math.floor); # {4: [4.2], 6: [6.1, 6.3]} +group_by(['one', 'two', 'three'], 'length'); # {3: ['one', 'two'], 5: ['three']} ```
@@ -526,18 +515,19 @@ Returns `True` if there are duplicate values in a flast list, `False` otherwise. Use `set()` on the given list to remove duplicates, compare its length with the length of the list. -```python ```py -def has_duplicates(lst): +def has_duplicates(lst): return len(lst) != len(set(lst)) ``` -```
Examples -```python -undefined +```py +x = [1,2,3,4,5,5] +y = [1,2,3,4,5] +has_duplicates(x) # True +has_duplicates(y) # False ```
@@ -549,18 +539,16 @@ Returns the head of a list. use `lst[0]` to return the first element of the passed list. -```python ```py -def head(lst): +def head(lst): return lst[0] ``` -```
Examples -```python -undefined +```py +head([1, 2, 3]); # 1 ```
@@ -572,18 +560,16 @@ Returns all the elements of a list except the last one. Use `lst[0:-1]` to return all but the last element of the list. -```python ```py -def initial(lst): +def initial(lst): return lst[0:-1] ``` -```
Examples -```python -undefined +```py +initial([1, 2, 3]); # [1,2] ```
@@ -598,18 +584,16 @@ If `val` is not provided, default to `None`. Explain briefly how the snippet works. -```python ```py -def initialize_2d_list(w,h, val = None): +def initialize_2d_list(w,h, val = None): return [[val for x in range(w)] for y in range(h)] ``` -```
Examples -```python -undefined +```py +initialize_2d_list(2, 2, 0) # [[0,0], [0,0]] ```
@@ -623,18 +607,18 @@ Use list comprehension and `range()` to generate a list of the appropriate lengt Omit `start` to use the default value of `0`. Omit `step` to use the default value of `1`. -```python ```py -def initialize_list_with_range(end, start = 0, step = 1): +def initialize_list_with_range(end, start = 0, step = 1): return [x for x in range(start, end + 1, step)] ``` -```
Examples -```python -undefined +```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] ```
@@ -647,18 +631,16 @@ Initializes and fills a list with the specified value. Use list comprehension and `range()` to generate a list of length equal to `n`, filled with the desired values. Omit `val` to use the default value of `0`. -```python ```py -def initialize_list_with_values(n, val = 0): +def initialize_list_with_values(n, val = 0): return [val for x in range(n)] ``` -```
Examples -```python -undefined +```py +initialize_list_with_values(5, 2) # [2, 2, 2, 2, 2] ```
@@ -670,19 +652,17 @@ Returns a list of elements that exist in both lists. Create a `set` from `b`, then use list comprehension on `a` to only keep values contained in both lists. -```python ```py -def intersection(a, b): - _b = set(b) +def intersection(a, b): + _b = set(b) return [item for item in a if item in _b] ``` -```
Examples -```python -undefined +```py +intersection([1, 2, 3], [4, 3, 2]) # [2, 3] ```
@@ -694,19 +674,18 @@ Returns a list of elements that exist in both lists, after applying the provided Create a `set` by applying `fn` to each element in `b`, then use list comprehension in combination with `fn` on `a` to only keep values contained in both lists. -```python ```py -def intersection_by(a, b, fn): - _b = set(map(fn, b)) +def intersection_by(a, b, fn): + _b = set(map(fn, b)) return [item for item in a if fn(item) in _b] ``` -```
Examples -```python -undefined +```py +from math import floor +intersection_by([2.1, 1.2], [2.3, 3.4],floor) # [2.1] ```
@@ -718,18 +697,16 @@ Returns the last element in a list. use `lst[-1]` to return the last element of the passed list. -```python ```py -def last(lst): +def last(lst): return lst[-1] ``` -```
Examples -```python -undefined +```py +last([1, 2, 3]) # 3 ```
@@ -742,18 +719,18 @@ If multiple objects have the same length, the first one will be returned. Use `max()` with `len` as the `key` to return the item with the greatest length. -```python ```py -def longest_item(*args): +def longest_item(*args): return max(args, key = len) ``` -```
Examples -```python -undefined +```py +longest_item('this', 'is', 'a', 'testcase') # 'testcase' +longest_item([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]) # [1, 2, 3, 4, 5] +longest_item([1, 2, 3], 'foobar') # 'foobar' ```
@@ -767,18 +744,17 @@ If `n` is greater than or equal to the provided list's length, then return the o Use `sorted() to sort the list, `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element list. -```python ```py -def max_n(lst, n=1): +def max_n(lst, n=1): return sorted(lst, reverse=True)[:n] ``` -```
Examples -```python -undefined +```py +max_n([1, 2, 3]) # [3] +max_n([1, 2, 3], 2) # [3,2] ```
@@ -792,18 +768,17 @@ If `n` is greater than or equal to the provided list's length, then return the o Use `sorted() to sort the list, `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element list. -```python ```py -def min_n(lst, n=1): +def min_n(lst, n=1): return sorted(lst, reverse=False)[:n] ``` -```
Examples -```python -undefined +```py +min_n([1, 2, 3]) # [1] +min_n([1, 2, 3], 2) # [1,2] ```
@@ -816,21 +791,20 @@ Returns `False` if the provided function returns `True` for at least one element 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`. -```python ```py -def none(lst, fn=lambda x: not not x): - for el in lst: - if fn(el): - return False +def none(lst, fn=lambda x: not not x): + for el in lst: + if fn(el): + return False return True ``` -```
Examples -```python -undefined +```py +none([0, 1, 2, 0], lambda x: x >= 2 ) # False +none([0, 0, 0]) # True ```
@@ -844,18 +818,17 @@ Use `lst[offset:]` and `lst[:offset]` to get the two slices of the list and comb Explain briefly how the snippet works. -```python ```py -def offset(lst, offset): +def offset(lst, offset): return lst[offset:] + lst[:offset] ``` -```
Examples -```python -undefined +```py +offset([1, 2, 3, 4, 5], 2) # [3, 4, 5, 1, 2] +offset([1, 2, 3, 4, 5], -2) # [4, 5, 1, 2, 3] ```
@@ -867,20 +840,18 @@ Returns a random element from an array. Use `randint()` to generate a random number that corresponds to an index in the list, return the element at that index. -```python ```py -from random import randint - -def sample(lst): +from random import randint + +def sample(lst): return lst[randint(0, len(lst) - 1)] ``` -```
Examples -```python -undefined +```py +sample([3, 7, 9, 11]) # 9 ```
@@ -892,27 +863,26 @@ Randomizes the order of the values of an list, returning a new list. Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) to reorder the elements of the list. -```python ```py -from copy import deepcopy -from random import randint - -def shuffle(lst): - temp_lst = deepcopy(lst) - m = len(temp_lst) - while (m): - m -= 1 - i = randint(0, m) - temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m] +from copy import deepcopy +from random import randint + +def shuffle(lst): + temp_lst = deepcopy(lst) + m = len(temp_lst) + while (m): + m -= 1 + i = randint(0, m) + temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m] return temp_lst ``` -```
Examples -```python -undefined +```py +foo = [1,2,3] +shuffle(foo) # [2,3,1] , foo = [1,2,3] ```
@@ -924,18 +894,16 @@ Returns a list of elements that exist in both lists. Use list comprehension on `a` to only keep values contained in both lists. -```python ```py -def similarity(a, b): +def similarity(a, b): return [item for item in a if item in b] ``` -```
Examples -```python -undefined +```py +similarity([1, 2, 3], [1, 2, 4]) # [1, 2] ```
@@ -948,21 +916,20 @@ Returns `True` if the provided function returns `True` for at least one element 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`. -```python ```py -def some(lst, fn=lambda x: not not x): - for el in lst: - if fn(el): - return True +def some(lst, fn=lambda x: not not x): + for el in lst: + if fn(el): + return True return False ``` -```
Examples -```python -undefined +```py +some([0, 1, 2, 0], lambda x: x >= 2 ) # True +some([0, 0, 1, 0]) # True ```
@@ -974,24 +941,67 @@ Flattens a list, by spreading its elements into a new list. Loop over elements, use `list.extend()` if the element is a list, `list.append()` otherwise. -```python ```py -def spread(arg): - ret = [] - for i in arg: - if isinstance(i, list): - ret.extend(i) - else: - ret.append(i) +def spread(arg): + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) return ret ``` -```
Examples -```python -undefined +```py +spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9] +``` +
+ +
[⬆ Back to top](#contents) + +### symmetric_difference + +Returns the symmetric difference between two iterables, without filtering out duplicate values. + +Create a `set` from each list, then use list comprehension on each one to only keep values not contained in the previously created set of the other. + +```py +def symmetric_difference(a, b): + _a, _b = set(a), set(b) + return [item for item in a if item not in _b] + [item for item in b if item not in _a] +``` + +
+Examples + +```py +symmetric_difference([1, 2, 3], [1, 2, 4]) # [3, 4] +``` +
+ +
[⬆ Back to top](#contents) + +### symmetric_difference_by + +Returns the symmetric difference between two lists, after applying the provided function to each list element of both. + +Create a `set` by applying `fn` to each element in every list, then use list comprehension in combination with `fn` on each one to only keep values not contained in the previously created set of the other. + +```py +def symmetric_difference_by(a, b, fn): + _a, _b = set(map(fn, a)), set(map(fn, b)) + return [item for item in a if fn(item) not in _b] + [item for item in b if fn(item) not in _a] +``` + +
+Examples + +```py +from math import floor +symmetric_difference_by([2.1, 1.2], [2.3, 3.4],floor) # [1.2, 3.4] ```
@@ -1003,18 +1013,62 @@ Returns all elements in a list except for the first one. Return `lst[1:]` if the list's length is more than `1`, otherwise, return the whole list. -```python ```py -def tail(lst): +def tail(lst): return lst[1:] if len(lst) > 1 else lst ``` -```
Examples -```python -undefined +```py +tail([1, 2, 3]); # [2,3] +tail([1]); # [1] +``` +
+ +
[⬆ Back to top](#contents) + +### union + +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): + return list(set(a + b)) +``` + +
+Examples + +```py +union([1, 2, 3], [4, 3, 2]) # [1,2,3,4] +``` +
+ +
[⬆ Back to top](#contents) + +### union_by + +Returns every element that exists in any of the two lists once, after applying the provided function to each element of both. + +Create a `set` by applying `fn` to each element in `a`, then use list comprehension in combination with `fn` on `b` to only keep values not contained in the previously created set, `_a`. +Finally, create a `set` from the previous result and `a` and transform it into a `list` + +```py +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])) +``` + +
+Examples + +```py +from math import floor +union_by([2.1], [1.2, 2.3], floor) # [2.1, 1.2] ```
@@ -1026,18 +1080,16 @@ Returns the unique elements in a given list. Create a `set` from the list to discard duplicated values, then return a `list` from it. -```python ```py -def unique_elements(li): +def unique_elements(li): return list(set(li)) ``` -```
Examples -```python -undefined +```py +unique_elements([1, 2, 2, 3, 4, 3]) # [1, 2, 3, 4] ```
@@ -1052,24 +1104,24 @@ Use `max` combined with `list comprehension` to get the length of the longest li Loop for `max_length` times grouping elements. If lengths of `lists` vary, use `fill_value` (defaults to `None`). -```python ```py -def zip(*args, fillvalue=None): - max_length = max([len(lst) for lst in args]) - result = [] - for i in range(max_length): - result.append([ - args[k][i] if i < len(args[k]) else fillvalue for k in range(len(args)) - ]) +def zip(*args, fillvalue=None): + max_length = max([len(lst) for lst in args]) + result = [] + for i in range(max_length): + result.append([ + args[k][i] if i < len(args[k]) else fillvalue for k in range(len(args)) + ]) return result ``` -```
Examples -```python -undefined +```py +zip(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]] +zip(['a'], [1, 2], [True, False]) # [['a', 1, True], [None, 2, False]] +zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]] ```
@@ -1086,18 +1138,17 @@ Returns the average of two or more numbers. Use `sum()` to sum all of the `args` provided, divide by `len(args)`. -```python ```py -def average(*args): +def average(*args): return sum(args, 0.0) / len(args) ``` -```
Examples -```python -undefined +```py +average(*[1, 2, 3]) # 2.0 +average(1, 2, 3) # 2.0 ```
@@ -1110,18 +1161,16 @@ Returns the average of a list, after mapping each element to a value using the p Use `map()` to map each element to the value returned by `fn`. Use `sum()` to sum all of the mapped values, divide by `len(lst)`. -```python ```py -def average_by(lst, fn=lambda x: x): +def average_by(lst, fn=lambda x: x): return sum(map(fn, lst), 0.0) / len(lst) ``` -```
Examples -```python -undefined +```py +average_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda x: x['n']) # 5.0 ```
@@ -1134,18 +1183,17 @@ Clamps `num` within the inclusive range specified by the boundary values `a` and If `num` falls within the range, return `num`. Otherwise, return the nearest number in the range. -```python ```py -def clamp_number(num,a,b): +def clamp_number(num,a,b): return max(min(num, max(a,b)),min(a,b)) ``` -```
Examples -```python -undefined +```py +clamp_number(2, 3, 5) # 3 +clamp_number(1, -1, -5) # -1 ```
@@ -1157,18 +1205,16 @@ Converts a number to an array of digits. Use `map()` combined with `int` on the string representation of `n` and return a list from the result. -```python ```py -def digitize(n): +def digitize(n): return list(map(int, str(n))) ``` -```
Examples -```python -undefined +```py +digitize(123) # [1, 2, 3] ```
@@ -1183,21 +1229,19 @@ If `num` is less than or equal to `1`, return `1`. Otherwise, return the product of `num` and the factorial of `num - 1`. Throws an exception if `num` is a negative or a floating point number. -```python ```py -def factorial(num): - if not ((num >= 0) & (num % 1 == 0)): - raise Exception( - f"Number( {num} ) can't be floating point or negative ") +def factorial(num): + if not ((num >= 0) & (num % 1 == 0)): + raise Exception( + f"Number( {num} ) can't be floating point or negative ") return 1 if num == 0 else num * factorial(num - 1) ``` -```
Examples -```python -undefined +```py +factorial(6) # 720 ```
@@ -1210,8 +1254,7 @@ Generates an array, containing the Fibonacci sequence, up until the nth term. Starting with `0` and `1`, use `list.apoend() to add the sum of the last two numbers of the list to the end of the list, until the length of the list reaches `n`. If `n` is less or equal to `0`, return a list containing `0`. -```python -```py +```py def fibonacci(n): if n <= 0: return [0] @@ -1221,15 +1264,14 @@ def fibonacci(n): next_value = sequence[len(sequence) - 1] + sequence[len(sequence) - 2] sequence.append(next_value) - return sequence -``` + return sequence ```
Examples -```python -undefined +```py +fibonacci(7) # [0, 1, 1, 2, 3, 5, 8, 13] ```
@@ -1241,21 +1283,19 @@ Calculates the greatest common divisor of a list of numbers. Use `reduce()` and `math.gcd` over the given list. -```python ```py -from functools import reduce -import math - -def gcd(numbers): +from functools import reduce +import math + +def gcd(numbers): return reduce(math.gcd, numbers) ``` -```
Examples -```python -undefined +```py +gcd([8,36,28]) # 4 ```
@@ -1268,20 +1308,21 @@ Checks if the given number falls within the given range. Use arithmetic comparison to check if the given number is in the specified range. If the second parameter, `end`, is not specified, the range is considered to be from `0` to `start`. -```python ```py -def in_range(n, start, end = 0): - if (start > end): - end, start = start, end +def in_range(n, start, end = 0): + if (start > end): + end, start = start, end return start <= n <= end ``` -```
Examples -```python -undefined +```py +in_range(3, 2, 5); # True +in_range(3, 4); # True +in_range(2, 3, 5); # False +in_range(3, 2); # False ```
@@ -1293,18 +1334,16 @@ Checks if the first numeric argument is divisible by the second one. Use the modulo operator (`%`) to check if the remainder is equal to `0`. -```python ```py -def is_divisible(dividend, divisor): +def is_divisible(dividend, divisor): return dividend % divisor == 0 ``` -```
Examples -```python -undefined +```py +is_divisible(6, 3) # True ```
@@ -1317,18 +1356,16 @@ Returns `True` if the given number is even, `False` otherwise. Checks whether a number is odd or even using the modulo (`%`) operator. Returns `True` if the number is even, `False` if the number is odd. -```python ```py -def is_even(num): +def is_even(num): return num % 2 == 0 ``` -```
Examples -```python -undefined +```py +is_even(3) # False ```
@@ -1341,18 +1378,16 @@ Returns `True` if the given number is odd, `False` otherwise. Checks whether a number is even or odd using the modulo (`%`) operator. Returns `True` if the number is odd, `False` if the number is even. -```python ```py -def is_odd(num): +def is_odd(num): return num % 2 == `0` ``` -```
Examples -```python -undefined +```py +is_odd(3) # True ```
@@ -1365,36 +1400,35 @@ Returns the least common multiple of two or more numbers. Define a function, `spread`, that uses either `list.extend()` or `list.append()` on each element in a list to flatten it. Use `math.gcd()` and `lcm(x,y) = x * y / gcd(x,y)` to determine the least common multiple. -```python ```py -from functools import reduce -import math - -def spread(arg): - ret = [] - for i in arg: - if isinstance(i, list): - ret.extend(i) - else: - ret.append(i) - return ret - -def lcm(*args): - numbers = [] - numbers.extend(spread(list(args))) - - def _lcm(x, y): - return int(x * y / math.gcd(x, y)) - +from functools import reduce +import math + +def spread(arg): + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) + return ret + +def lcm(*args): + numbers = [] + numbers.extend(spread(list(args))) + + def _lcm(x, y): + return int(x * y / math.gcd(x, y)) + return reduce((lambda x, y: _lcm(x, y)), numbers) ``` -```
Examples -```python -undefined +```py +lcm(12, 7) # 84 +lcm([1, 3, 4], 5) # 60 ```
@@ -1404,20 +1438,18 @@ undefined 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, convert to a `list` and use `max()` to return the maximum value. -```python ```py -def max_by(lst, fn): +def max_by(lst, fn): return max(list(map(fn,lst))) ``` -```
Examples -```python -undefined +```py +max_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 8 ```
@@ -1427,20 +1459,18 @@ undefined 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, convert to a `list` and use `min()` to return the minimum value. -```python ```py -def min_by(lst, fn): +def min_by(lst, fn): return min(list(map(fn,lst))) ``` -```
Examples -```python -undefined +```py +min_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 2 ```
@@ -1452,20 +1482,40 @@ Converts an angle from radians to degrees. Use `math.pi` and the radian to degree formula to convert the angle from radians to degrees. -```python ```py -import math - -def rads_to_degrees(rad): +import math + +def rads_to_degrees(rad): return (rad * 180.0) / math.pi ``` -```
Examples -```python -undefined +```py +import math +rads_to_degrees(math.pi / 2) # 90.0 +``` +
+ +
[⬆ Back to top](#contents) + +### sum_by + +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. + +```py +def sum_by(lst, fn): + return sum(list(map(fn,lst))) +``` + +
+Examples + +```py +sum_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 20 ```
@@ -1483,18 +1533,21 @@ Returns a flat list of all the keys in a flat dictionary. Use `dict.keys()` to return the keys in the given dictionary. Return a `list()` of the previous result. -```python ```py -def keys_only(flat_dict): +def keys_only(flat_dict): return list(flat_dict.keys()) ``` -```
Examples -```python -undefined +```py +ages = { + "Peter": 10, + "Isabel": 11, + "Anna": 9, +} +keys_only(ages) # ['Peter', 'Isabel', 'Anna'] ```
@@ -1506,21 +1559,24 @@ Creates an object with the same keys as the provided object and values generated Use `dict.keys()` to iterate over the object's keys, assigning the values produced by `fn` to each key of a new object. -```python ```py -def map_values(obj, fn): - ret = {} - for key in obj.keys(): - ret[key] = fn(obj[key]) +def map_values(obj, fn): + ret = {} + for key in obj.keys(): + ret[key] = fn(obj[key]) return ret ``` -```
Examples -```python -undefined +```py +users = { + 'fred': { 'user': 'fred', 'age': 40 }, + 'pebbles': { 'user': 'pebbles', 'age': 1 } +} + +map_values(users, lambda u : u['age']) # {'fred': 40, 'pebbles': 1} ```
@@ -1533,18 +1589,21 @@ Returns a flat list of all the values in a flat dictionary. Use `dict.values()` to return the values in the given dictionary. Return a `list()` of the previous result. -```python ```py -def values_only(dict): +def values_only(dict): return list(flat_dict.values()) ``` -```
Examples -```python -undefined +```py +ages = { + "Peter": 10, + "Isabel": 11, + "Anna": 9, +} +values_only(ages) # [10, 11, 9] ```
@@ -1561,18 +1620,47 @@ Returns the length of a string in bytes. Use `string.encode('utf-8')` to encode the given string and return its length. -```python ```py -def byte_size(string): +def byte_size(string): return len(string.encode('utf-8')) ``` -```
Examples -```python -undefined +```py +byte_size('😀') # 4 +byte_size('Hello World') # 11 +``` +
+ +
[⬆ Back to top](#contents) + +### camel + +Converts a string to camelcase. + +Break the string into words and combine them capitalizing the first letter of each word, using a regexp. + +```py +import re + +def camel(str): + s = re.sub(r"(\s|_|-)+","", + re.sub(r"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+", + lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(),str) + ) + return s[0].lower() + s[1:] +``` + +
+Examples + +```py +camel('some_database_field_name'); # 'someDatabaseFieldName' +camel('Some label that needs to be camelized'); # 'someLabelThatNeedsToBeCamelized' +camel('some-javascript-property'); # 'someJavascriptProperty' +camel('some-mixed_string with spaces_underscores-and-hyphens'); # 'someMixedStringWithSpacesUnderscoresAndHyphens' ```
@@ -1585,18 +1673,17 @@ Capitalizes the first letter of a string. Capitalize the first letter of the string and then add it with rest of the string. Omit the `lower_rest` parameter to keep the rest of the string intact, or set it to `True` to convert to lowercase. -```python ```py -def capitalize(string, lower_rest=False): +def capitalize(string, lower_rest=False): return string[:1].upper() + (string[1:].lower() if lower_rest else string[1:]) ``` -```
Examples -```python -undefined +```py +capitalize('fooBar') # 'FooBar' +capitalize('fooBar', True) # 'Foobar' ```
@@ -1608,18 +1695,16 @@ Capitalizes the first letter of every word in a string. Use `string.title()` to capitalize first letter of every word in the string. -```python ```py -def capitalize_every_word(string): +def capitalize_every_word(string): return string.title() ``` -```
Examples -```python -undefined +```py +capitalize_every_word('hello world!') # 'Hello World!' ```
@@ -1632,18 +1717,17 @@ Decapitalizes the first letter of a string. Decapitalize the first letter of the string and then add it with rest of the string. Omit the `upper_rest` parameter to keep the rest of the string intact, or set it to `True` to convert to uppercase. -```python ```py -def decapitalize(string, upper_rest=False): +def decapitalize(string, upper_rest=False): return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:]) ``` -```
Examples -```python -undefined +```py +decapitalize('FooBar') # 'fooBar' +decapitalize('FooBar', True) # 'fOOBAR' ```
@@ -1657,23 +1741,21 @@ Use `str.replace()` to remove spaces from both strings. Compare the lengths of the two strings, return `False` if they are not equal. Use `sorted()` on both strings and compare the results. -```python ```py -def is_anagram(str1, str2): - _str1, _str2 = str1.replace(" ", ""), str2.replace(" ", "") - - if len(_str1) != len(_str2): - return False - else: +def is_anagram(str1, str2): + _str1, _str2 = str1.replace(" ", ""), str2.replace(" ", "") + + if len(_str1) != len(_str2): + return False + else: return sorted(_str1.lower()) == sorted(_str2.lower()) ``` -```
Examples -```python -undefined +```py +is_anagram("anagram", "Nag a ram") # True ```
@@ -1685,18 +1767,18 @@ Checks if a string is lower case. Convert the given string to lower case, using `str.lower()` and compare it to the original. -```python ```py -def is_lower_case(string): +def is_lower_case(string): return string == string.lower() ``` -```
Examples -```python -undefined +```py +is_lower_case('abc') # True +is_lower_case('a3@$') # True +is_lower_case('Ab4') # False ```
@@ -1708,18 +1790,47 @@ Checks if a string is upper case. Convert the given string to upper case, using `str.upper()` and compare it to the original. -```python ```py -def is_upper_case(string): +def is_upper_case(string): return string == string.upper() ``` -```
Examples -```python -undefined +```py +is_upper_case('ABC') # True +is_upper_case('a3@$') # False +is_upper_case('aB4') # False +``` +
+ +
[⬆ Back to top](#contents) + +### kebab + +Converts a string to kebab case. + +Break the string into words and combine them adding `-` as a separator, using a regexp. + +```py +import re + +def kebab(str): + return re.sub(r"(\s|_|-)+","-", + re.sub(r"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+", + lambda mo: mo.group(0).lower(),str) + ) +``` + +
+Examples + +```py +kebab('camelCase'); # 'camel-case' +kebab('some text'); # 'some-text' +kebab('some-mixed_string With spaces_underscores-and-hyphens'); # 'some-mixed-string-with-spaces-underscores-and-hyphens' +kebab('AllThe-small Things'); # "all-the-small-things" ```
@@ -1732,21 +1843,48 @@ Returns `True` if the given string is a palindrome, `False` otherwise. Use `str.lower()` and `re.sub()` to convert to lowercase and remove non-alphanumeric characters from the given string. Then, compare the new string with its reverse. -```python ```py -from re import sub - -def palindrome(string): - s = sub('[\W_]', '', string.lower()) +from re import sub + +def palindrome(string): + s = sub('[\W_]', '', string.lower()) return s == s[::-1] ``` -```
Examples -```python -undefined +```py +palindrome('taco cat') # True +``` +
+ +
[⬆ Back to top](#contents) + +### snake + +Converts a string to snake case. + +Break the string into words and combine them adding `_-_` as a separator, using a regexp. + +```py +import re + +def snake(str): + return re.sub(r"(\s|_|-)+","-", + re.sub(r"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+", + lambda mo: mo.group(0).lower(),str) + ) +``` + +
+Examples + +```py +snake('camelCase'); # 'camel_case' +snake('some text'); # 'some_text' +snake('some-mixed_string With spaces_underscores-and-hyphens'); # 'some_mixed_string_with_spaces_underscores_and_hyphens' +snake('AllThe-small Things'); # "all_the_smal_things" ```
@@ -1758,18 +1896,16 @@ Splits a multiline string into a list of lines. Use `str.split()` and `'\n'` to match line breaks and create a list. -```python ```py -def split_lines(str): +def split_lines(str): str.split('\n') ``` -```
Examples -```python -undefined +```py +split_lines('This\nis a\nmultiline\nstring.\n') # 'This\nis a\nmultiline\nstring.\n' ```
@@ -1786,18 +1922,17 @@ Casts the provided value as an array if it's not one. Use `isinstance()` to check if the given value is a list and return it as-is or encapsulated in a list accordingly. -```python ```py -def cast_list(val): +def cast_list(val): return val if isinstance(val, list) else [val] ``` -```
Examples -```python -undefined +```py +cast_list('foo'); # ['foo'] +cast_list([1]); # [1] ```
@@ -1805,10 +1940,10 @@ undefined ## Collaborators -| [](https://github.com/Chalarangelo)
[Angelos Chalaris](https://github.com/Chalarangelo) | [](https://github.com/kriadmin)
[Rohit Tanwar](https://github.com/kriadmin) | [](https://github.com/fejes713)
[Stefan Feješ](https://github.com/fejes713) | +| [](https://github.com/Chalarangelo)
[Angelos Chalaris](https://github.com/Chalarangelo) | [](https://github.com/fejes713)
[Stefan Feješ](https://github.com/fejes713) | [](https://github.com/kriadmin)
[Rohit Tanwar](https://github.com/kriadmin) | +| --- | --- | --- | ## Credits -*Icons made by [Smashicons](https://www.flaticon.com/authors/smashicons) from [www.flaticon.com](https://www.flaticon.com/) is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/).* *This README is built using [markdown-builder](https://github.com/30-seconds/markdown-builder).* diff --git a/advanced.svg b/advanced.svg new file mode 100644 index 000000000..428312a3c --- /dev/null +++ b/advanced.svg @@ -0,0 +1 @@ +advancedadvanced diff --git a/assets/30s-icon.png b/assets/30s-icon.png index e96a17570..f61b70f1a 100644 Binary files a/assets/30s-icon.png and b/assets/30s-icon.png differ diff --git a/assets/logo.png b/assets/logo.png index 631b8b740..c1e1bd331 100644 Binary files a/assets/logo.png and b/assets/logo.png differ diff --git a/config.js b/config.js index da6c6e3d6..a8a1da6df 100644 --- a/config.js +++ b/config.js @@ -1,6 +1,6 @@ module.exports = { // Project metadata - name: `30 seconds of python code`, + name: `30 seconds of python`, description: `A curated collection of useful Python snippets that you can understand in 30 seconds or less.`, shortName: `30s`, repositoryUrl: `https://github.com/30-seconds/30-seconds-of-python-code`, @@ -11,5 +11,5 @@ module.exports = { pagePath: `src/docs/pages`, staticPartsPath: `src/static-parts`, // General information - language: `python`, + language: `py`, }; diff --git a/icon.png b/icon.png index 631b8b740..c1e1bd331 100644 Binary files a/icon.png and b/icon.png differ diff --git a/scripts/build.js b/scripts/build.js index e71e9cb70..04023e0d4 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -116,7 +116,7 @@ try { output += misc.collapsible( 'Examples', - `\`\`\`${config.language}\n${snippet.attributes.codeBlocks.examples}\n\`\`\``, + `\`\`\`${config.language}\n${snippet.attributes.codeBlocks.example}\n\`\`\``, ); output += diff --git a/snippet_data/snippetList.json b/snippet_data/snippetList.json index 01fdb01a5..f3b6f849d 100644 --- a/snippet_data/snippetList.json +++ b/snippet_data/snippetList.json @@ -12,7 +12,7 @@ ] }, "meta": { - "hash": "6bf29cb176e4e9ce664ad04c6d262b6848f26639624dbb812eb1074d1c68b82a" + "hash": "650f05b1f5d4420f6e44546b34fbb1dc381e9500b4c64adf7598651d5c188223" } }, { @@ -27,7 +27,7 @@ ] }, "meta": { - "hash": "ca101c9e623105dc857f1484fcea9211dda0dffc224834a5609d125257a7040f" + "hash": "9b1bdbb130a4fa6301e994359bf162bec262a35fdfd3f00519f7f6a59f1137c0" } }, { @@ -43,7 +43,7 @@ ] }, "meta": { - "hash": "78bfdea5946774504eea1ba100974ad48ce8cb0a1ce1404cee8d885f35bb93a1" + "hash": "ff65e9a114f47afb53e56a54588e418ef886f8bc508ed32543c97bd1d5966aae" } }, { @@ -60,7 +60,7 @@ ] }, "meta": { - "hash": "8de876b55fd8b75ec395a5d70c29d83c7515d3ae2d59b9c7848e66e6cb854af0" + "hash": "360d3b11343373558e25575401add239d4ab85154b72bfae347b19a487df35f3" } }, { @@ -75,7 +75,7 @@ ] }, "meta": { - "hash": "22c836b50fafc995904cc69096235ef9fb4c77a6faf38c4c5a4a88b2ed2126a8" + "hash": "49c7d4e1f7175d460074d776cfb0b534ddf753001b66d0e3cacc4177c59e994c" } }, { @@ -91,7 +91,7 @@ ] }, "meta": { - "hash": "2e62d552ca03ff9bc39f920f4e24745945729ba35acc8ba75267b315a9b43563" + "hash": "0975a7d514bc4f5c1c13731165b1a43e43b9dc0eff289fffdfdd4d67af92e23b" } }, { @@ -106,7 +106,23 @@ ] }, "meta": { - "hash": "ff655042992e2a6cded2c39439eca6a6542e4d7b4d019c9c8721fa61be6ccdb8" + "hash": "a80c7f1115762fffeaee2b4d75e36ce88dd288bdb4a99ae9c68c038223cbe00e" + } + }, + { + "id": "camel", + "type": "snippetListing", + "title": "camel", + "attributes": { + "text": "Converts a string to camelcase.\n\nBreak the string into words and combine them capitalizing the first letter of each word, using a regexp.\n\n", + "tags": [ + "string", + "regexp", + "intermediate" + ] + }, + "meta": { + "hash": "e003dc4980414b0731c01d4b4bf3f08774c3588de95bf38c17ad0e08bb1bd838" } }, { @@ -121,7 +137,7 @@ ] }, "meta": { - "hash": "f4b0ecfe5d6eb18a65699fbe706737723c2c0de6a16e07e4c7686e9ecbad29c5" + "hash": "830372b0e6a7a008c1ee9ec4ef11c6063ff14b94b09b4b081d220079fa89e2aa" } }, { @@ -136,7 +152,7 @@ ] }, "meta": { - "hash": "ec399b1f2bcb0888956d1ecb40fd509f22ba902cd7f3c53af02729d52f021f86" + "hash": "699eb144ef3a23fbc4375096ac28535f809b4fc5bd4a493dfbf0f7e8df61a446" } }, { @@ -152,7 +168,7 @@ ] }, "meta": { - "hash": "4a9cb79c384099543163d3b1b7e2fa389a4cc373b2b76b17563a4597ed29a4c7" + "hash": "9403a0321a9fcb907a18cadf8283be3ecf89a7a51e8679978e26cfb050918ced" } }, { @@ -167,7 +183,7 @@ ] }, "meta": { - "hash": "f8c9cdb2261bfe2932bc7d3d11853a3d42d468a88ad515e9f15d9abffe9b30a6" + "hash": "3a27343fc9afa6666e7ab4b015ebe883c083fbd37841058a37bb0e143d11b1ff" } }, { @@ -182,7 +198,7 @@ ] }, "meta": { - "hash": "7e3dc4519a629ec87f72e5b495227733735c9c2262e5f6a89264191967cfad31" + "hash": "20549e07442c96474df3db5824f7ac9eb8f1763234ef4abb0b500845cb71383d" } }, { @@ -197,7 +213,7 @@ ] }, "meta": { - "hash": "6b98c58b6aecf1b58ed36f55a407bf2f2d68938723d37472fe43d9e652d93fe6" + "hash": "2cef075bcbeb781fdfd9a2284f04281922c50c79273e90b1d6fa858b06c8fb60" } }, { @@ -212,7 +228,7 @@ ] }, "meta": { - "hash": "e78cb9229a2bc4c882fa7d901dbdee6ca9c33bde348650210afb210d3b98e1f6" + "hash": "b5b6656857a0c11b88b579c1f91451b78f7f2bd5c9eb209df7f15d4885d6bbc0" } }, { @@ -227,7 +243,7 @@ ] }, "meta": { - "hash": "eee88217431699369070beb38ca0265d963c78321bfd98de9e3a38533c6ee90e" + "hash": "9bd71bbccb9b18b35856360e4ccc7ae477ec7cd7e8363fc851ec3eac49813334" } }, { @@ -242,7 +258,7 @@ ] }, "meta": { - "hash": "aea9a271d47a5606912e2482b8cc6bfb7b7382c4d0c86545b194cc0ad5f342b5" + "hash": "05d4132e66af5a615c220781d247b7050445e001e76455343760a85e62edd106" } }, { @@ -258,7 +274,7 @@ ] }, "meta": { - "hash": "a100d5704afe48a7dce26871bc50c993670d7388d8e4958c02c07efe07884167" + "hash": "3f0192a6f20dd5c0d4635f3e6f736fefbfe433f2cf4f36b3da12f8467f7704c0" } }, { @@ -273,7 +289,7 @@ ] }, "meta": { - "hash": "479fcdba73e7429ccb82649a354159ad9008c63f852c960d6733c10ecafceae3" + "hash": "4b7b9afe16e5ffb48c84f267364ff5041598e59dce5fb7f3f165ad9835fb5175" } }, { @@ -289,7 +305,7 @@ ] }, "meta": { - "hash": "a240b4f3152f847cb5f3877ab2fa1f4b75259c5ce11e28a773ad34bdba14f0a8" + "hash": "67fcb3f28272e5c105374ce43a42382eba8ddb1c327989c102f82cc441ea0fe1" } }, { @@ -305,7 +321,7 @@ ] }, "meta": { - "hash": "576de2896d2565d44da5d42b4b57b37cf1b2e5fc69103ab48a9ed532040ffaf2" + "hash": "be2a8632262614ee14126e69efedbfb6517fdcc5c3453f859d8cd161d44e8b6e" } }, { @@ -321,7 +337,7 @@ ] }, "meta": { - "hash": "8356afa6609f21bef9f48c7fb4b82553cae27d67c4bb4e31bfda98ee814cf269" + "hash": "1a444727bf46de6441f3ebc87508c8cd2b1280dc1d0659d00e46a012e703d9d6" } }, { @@ -336,7 +352,7 @@ ] }, "meta": { - "hash": "d5c51547b66fcd9d42def2a6d92ba98429a12c6bec56a0dca8e4c0289525d5de" + "hash": "b0e4ab30cc730950401ab4291ce404b37132bc32bdeebbac1de871c581a55919" } }, { @@ -352,7 +368,7 @@ ] }, "meta": { - "hash": "3241103df06912913bbcfb51e03c1d355a7329e367aa9d4f6a770683bc196df8" + "hash": "e22b08efd49d4bba2cef3f7d2315e55a36223d7c861c19eaa23ca1b2ba72dc5b" } }, { @@ -383,7 +399,7 @@ ] }, "meta": { - "hash": "05679ae115d276830ec769a04b0800d92eabc6a09678fc1a9cf6013c813cc650" + "hash": "1bbae73c12dc8ee2bf80e93b2328d8f5138e603fa0ea381999fc388a25cdb107" } }, { @@ -398,7 +414,7 @@ ] }, "meta": { - "hash": "dbb01e7253dbb6f3e2f53271fc14fb7b3ee9816fe56266f78a54b4ca21c94cd7" + "hash": "ab1de2550801b4c4c2ab773912ea60553a9ef46d5928c879964432825aa6e7e3" } }, { @@ -414,7 +430,7 @@ ] }, "meta": { - "hash": "e103229c295cbfcbf26b5529b068e382bb0a1a04e080311e46302721841bbfe0" + "hash": "592f01a019379e6e1eeca55406a923930ab77133a5653febb585847ed387df58" } }, { @@ -429,7 +445,7 @@ ] }, "meta": { - "hash": "762663e25e978ee96720d6fd977e4ca42b328150fa654d0e600c636284552712" + "hash": "8aa2f1de87b94cb1eb6cdc0c048d051aae9d5561b8e6ca92b66c4543c8e24cd5" } }, { @@ -444,7 +460,7 @@ ] }, "meta": { - "hash": "6c86b52c2c7a56a4527114736664a3720b2026d2419b87daf36de6877ad0e4b0" + "hash": "079183a15856f39a61ea1c375a272565e3f70826f2579eaa66683f06aca7205b" } }, { @@ -459,7 +475,7 @@ ] }, "meta": { - "hash": "f65d098189837551aef365c68bd9389a0c1e84e89233fa5a886e889ce6981f9f" + "hash": "d3d40fb3ff5df9a9bb70b825b91952ca16ed4033fae94a1ea0eac7f64566a989" } }, { @@ -474,7 +490,7 @@ ] }, "meta": { - "hash": "cf0229dd484711e5847d85952841a13800bb5e0767c1de40e219f63cfed3e0f1" + "hash": "792086091d9aa9180f447ee3fb0d606f2dbc38c2b917d1c7a610d32071d40cc2" } }, { @@ -489,7 +505,7 @@ ] }, "meta": { - "hash": "a848fc33363ab533f745e081d885b4c981716ae063867ea9a2b471fd8fc92ac7" + "hash": "e141f00ba64f6308c0563ff6132191a798a14fe7783be884ab46bf698892c8cc" } }, { @@ -504,7 +520,7 @@ ] }, "meta": { - "hash": "b68fef168bb92503e54278a95753fd48b3cb9f5151e17d36ce69c7eae298683c" + "hash": "729784a5d236cb13fde71cab421a1487481d34f26e8acc3c923695ff972a965a" } }, { @@ -519,7 +535,7 @@ ] }, "meta": { - "hash": "87604b968fccbc3d60399619b388f5939d646059db467fc2c86f99138f68b952" + "hash": "a86132d77e383748415b694deff0f452f1ee22763a47ee8fa95a938faceec926" } }, { @@ -534,7 +550,7 @@ ] }, "meta": { - "hash": "a332cce7b7d7e303469e8c39c4926bf92ecb12847c3932985cc633e3a85a23bb" + "hash": "50509c3103ce76585b558ecd98a8833568795ecb260e5b9bf955245078a49af8" } }, { @@ -550,7 +566,7 @@ ] }, "meta": { - "hash": "eb4bff1a92a7848c8184531fee590e8f060a6d4f6ffe214aec4ccda7a5e18031" + "hash": "cde08163638452f63975589a3341c69e4621b364db22f6ab3d205bd895ad8b9a" } }, { @@ -565,7 +581,7 @@ ] }, "meta": { - "hash": "3080e22832c2a393f1546f4cff18b15b701fbe27dc2368fd26700a05f2f109a2" + "hash": "47c878d10f937760d9096ccedb10357df3ba1e87333fdc8256c9a6938994eef8" } }, { @@ -580,7 +596,7 @@ ] }, "meta": { - "hash": "e6272becc36195e4b76f62baa61d43d9de5472201d1fb589e18bd5f47d6d57b0" + "hash": "0502ded5a0a750f7d4f03940d046a9f41ad0189a79ad7f42d9fe8ce9333b81e8" } }, { @@ -595,7 +611,7 @@ ] }, "meta": { - "hash": "354d3fcfa67b5df106c0ff528246bfacd2d7cec4942a7c3a904d87e76d7dae11" + "hash": "0579f16d399d4ed57a52e6575633fe924d1b269268c2b1afe830f34ff0cf9a09" } }, { @@ -611,7 +627,7 @@ ] }, "meta": { - "hash": "ed6b313aaf87ba7ea9515679eea19f395f7bbd90b9f8b85ef54ca527115fa690" + "hash": "d84277f13e3250c2e9d8a6afd693d52a7383f29ea1fdf71bd5dd1f3d8d3a5baf" } }, { @@ -626,7 +642,7 @@ ] }, "meta": { - "hash": "2670b3500855c72812593fd30692701798be1bb3a7a68c2bb65a273fdee5c95a" + "hash": "b112c57fecf98765d44027b570c3f9b9fb304d450584bdee477b558861380ab9" } }, { @@ -642,7 +658,23 @@ ] }, "meta": { - "hash": "106225722752b199f974c06aec91721344499e46dbf643e9f2ce990ee8c2d3c1" + "hash": "fd01ad582630e68200df9317aa68877d4942412fab8ce123533460f4937203f2" + } + }, + { + "id": "kebab", + "type": "snippetListing", + "title": "kebab", + "attributes": { + "text": "Converts a string to kebab case.\n\nBreak the string into words and combine them adding `-` as a separator, using a regexp.\n\n", + "tags": [ + "string", + "regexp", + "intermediate" + ] + }, + "meta": { + "hash": "35f4a0ae7433cef616f137b696b909d7f5046685e64af70d59934f4d34e6c5d7" } }, { @@ -658,7 +690,7 @@ ] }, "meta": { - "hash": "4e4a5b4892fcccb2982e7ae352f9d61fa0234c209431c9461ea47d503feaaf49" + "hash": "f8cf88c53dd53b702bc504a2d36f9c9bddbe3bfdc0c2946909b9fd6de2370122" } }, { @@ -673,7 +705,7 @@ ] }, "meta": { - "hash": "7fc339fbdb18e993fc93950b307fe2ec792c3f010f5dee0971d02288af16d67b" + "hash": "7cefc731b24f3979e93354686c1ca91adebd7cfadc8cd903cbb784730fcce0d3" } }, { @@ -690,7 +722,7 @@ ] }, "meta": { - "hash": "a0cf166fd37f16c6bcaef46345ff11af0dadb488160168a0915e665550f7a669" + "hash": "0365d1bab90dcf808af1c31671e3bb712b4fa0a975e95fb97450a1db6eb8e7f2" } }, { @@ -707,7 +739,7 @@ ] }, "meta": { - "hash": "dd1a2d1300f23bb8f8618f9ab20dad11062f4476be80081e3cefc535717da818" + "hash": "fea8c001eb0d4a9c2bcd37b3e5fe5b636fec2fe0e937f1195bf1fec357d42e53" } }, { @@ -723,7 +755,7 @@ ] }, "meta": { - "hash": "91f93e124e2f1fb9d25b62db478f91503330947a7c212dc97215dec8c4b225fe" + "hash": "8478ad6fd73b9c7c15d7243b0be989ada0159c694471a520076f953ebf986258" } }, { @@ -731,7 +763,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, convert to a `list` and use `max()` to return the maximum value.\n\n", "tags": [ "math", "list", @@ -740,7 +772,7 @@ ] }, "meta": { - "hash": "b6758923ca701c9248e39ad2fd866c00af60dcdb670eb9845729b50bc2c59131" + "hash": "3538fe0c61f5ba76adc478a7a03603e227ae49209ff345f1a65e8163844e7024" } }, { @@ -756,7 +788,7 @@ ] }, "meta": { - "hash": "572897e2101454c75554f182d31b02182f296be88e10d8b41c15c7ac1e74ce25" + "hash": "779eb91b49e8f15d476fa067c4892bac0c63c962afa82ab42e7ef416c4a3d5a2" } }, { @@ -764,7 +796,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, convert to a `list` and use `min()` to return the minimum value.\n\n", "tags": [ "math", "list", @@ -773,7 +805,7 @@ ] }, "meta": { - "hash": "9b63955e5c0c0723f55213a88f052e97a297f042b162dc4b6507a87317615c9c" + "hash": "72812142d33c4328a3c69764b1ddde2ba4a64e0a6c10107b38835e1ef9d0655a" } }, { @@ -789,7 +821,7 @@ ] }, "meta": { - "hash": "c920b58f01c63346e65eab83a60c4635d4c104e41767b9d41ad94029e662b902" + "hash": "653535ca2eb51b2f792c529c4e8e96f99dcaceaf63b72c486c9b072fb5ea1a41" } }, { @@ -805,7 +837,7 @@ ] }, "meta": { - "hash": "2f345a768997fb0523dfbb775281ca0338a849032b5d8671d313c08f46f9b26e" + "hash": "50d683ab59844b8de41fee0fb2ed3267e68c5c3020265b51b2f49f75bc3bd85e" } }, { @@ -820,7 +852,7 @@ ] }, "meta": { - "hash": "86c03a0d8ea80a203414f79670fec491b232acd5448108c1858058df68833f42" + "hash": "e86853e988f5c40cd8ef95ef80da49fe9e40626ea754db4a83f5b68d9713afe9" } }, { @@ -835,7 +867,7 @@ ] }, "meta": { - "hash": "e707d6b2f27bcc3dda322b114199b2b22ea916871b1c657c43648ecb5b21240b" + "hash": "2da6734174e371587baeebba432bc0f441f024211e900c4852fca7eeb08f89df" } }, { @@ -850,7 +882,7 @@ ] }, "meta": { - "hash": "c3008a44f2eb31adaca3e4f91c29a56a955e3105f9c187aa055fc55c35874b41" + "hash": "b5eac6740b6b78892d5978c26e44dc2747898c179f6d1e2c6f181f716ed4ee97" } }, { @@ -866,7 +898,7 @@ ] }, "meta": { - "hash": "68c3a2f20c4969324199dc6005be573d95ec4d08747586bf26d0bf06b571c94c" + "hash": "d8cf3ead7fde16fd0886baa5469ef55844848d25629c4fff011d1e27496b75ad" } }, { @@ -882,7 +914,7 @@ ] }, "meta": { - "hash": "8c6e1dafadd78f04b11d412b7b8db01e1275339adf906e2637129d761117e480" + "hash": "e50c5b8daa558ff5ea7f2516a3e382f4e91bcf8ab5999041376020080dd916c0" } }, { @@ -897,7 +929,23 @@ ] }, "meta": { - "hash": "51e5a4fc0b3376f24804b51f71c3daed109b1e60535aa39bca44c1918fedaacf" + "hash": "54faf1a5edfa2e6c1508635fa2492ad22978ac988b250acedd2cdb64670fc019" + } + }, + { + "id": "snake", + "type": "snippetListing", + "title": "snake", + "attributes": { + "text": "Converts a string to snake case.\n\nBreak the string into words and combine them adding `_-_` as a separator, using a regexp.\n\n", + "tags": [ + "string", + "regexp", + "intermediate" + ] + }, + "meta": { + "hash": "c2a90128c4edf395cf659da0b2ad9ddccbf13eb19c900d4a8734bc98613096d8" } }, { @@ -913,7 +961,7 @@ ] }, "meta": { - "hash": "5052bf5e5a38a8a928ea6b3e42cecda2027d2123dd5e47764b354eafd3310dee" + "hash": "aada6496416fff036ff40ae4a8555d4bc27d56a0ce869d0d4366c410beada2bc" } }, { @@ -928,7 +976,7 @@ ] }, "meta": { - "hash": "9c8d9abb223e26561b4278e5b29c373a9196297143a504d353c7fbcd8c5102b2" + "hash": "aa12646b6b9629fe38eb7b04bdc5a9fbff64b012d5b65fcac1e8d0a42a14fd05" } }, { @@ -944,7 +992,55 @@ ] }, "meta": { - "hash": "db09a105c21df08d2d580e95dd9d3f895a38e2e9b29818f78a671203ab94df38" + "hash": "5e9cf0033df73310e23800b6d41677a8607342717d23ef661f17f559cae7a8d8" + } + }, + { + "id": "sum_by", + "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", + "tags": [ + "math", + "list", + "function", + "beginner" + ] + }, + "meta": { + "hash": "d97b4f2c69fafe781e6a0a72f2e41a2041ae960b2cb078b40a89ebba7464adab" + } + }, + { + "id": "symmetric_difference", + "type": "snippetListing", + "title": "symmetric_difference", + "attributes": { + "text": "Returns the symmetric difference between two iterables, without filtering out duplicate values.\n\nCreate a `set` from each list, then use list comprehension on each one to only keep values not contained in the previously created set of the other.\n\n", + "tags": [ + "list", + "beginner" + ] + }, + "meta": { + "hash": "b1b608eac070e1c51ded6494ba0a5291e3f2afaced5ea026201dac0a1ebdbe73" + } + }, + { + "id": "symmetric_difference_by", + "type": "snippetListing", + "title": "symmetric_difference_by", + "attributes": { + "text": "Returns the symmetric difference between two lists, after applying the provided function to each list element of both.\n\nCreate a `set` by applying `fn` to each element in every list, then use list comprehension in combination with `fn` on each one to only keep values not contained in the previously created set of the other.\n\n", + "tags": [ + "list", + "function", + "intermediate" + ] + }, + "meta": { + "hash": "d6b4f42c3d08fee38e8674c6059e01deffeb74d943e9d89e0836a39fe19309cc" } }, { @@ -959,7 +1055,38 @@ ] }, "meta": { - "hash": "f06acf219bcdfbacb9e86b01f91c7d68f6f8fa8a5a043c37cf33de4969c62eae" + "hash": "819f20907ca5f906b185988f32e06c1c39e9a39786dac76a3421c55c7f5327b1" + } + }, + { + "id": "union", + "type": "snippetListing", + "title": "union", + "attributes": { + "text": "Returns every element that exists in any of the two lists once.\n\nCreate a `set` with all values of `a` and `b` and convert to a `list`.\n\n", + "tags": [ + "list", + "beginner" + ] + }, + "meta": { + "hash": "cf3bd4a569cce91769a1e4781a4317d93a023ec04658389532b00572fc3d2b7a" + } + }, + { + "id": "union_by", + "type": "snippetListing", + "title": "union_by", + "attributes": { + "text": "Returns every element that exists in any of the two lists once, after applying the provided function to each element of both.\n\nCreate a `set` by applying `fn` to each element in `a`, then use list comprehension in combination with `fn` on `b` to only keep values not contained in the previously created set, `_a`.\nFinally, create a `set` from the previous result and `a` and transform it into a `list`\n\n", + "tags": [ + "list", + "function", + "intermediate" + ] + }, + "meta": { + "hash": "0da21f57e2c6eb4efedcbcf555b00843c03df3f5c505de421a4b4d226a1f081e" } }, { @@ -974,7 +1101,7 @@ ] }, "meta": { - "hash": "1f28461c8bbf4ce0195c756b2f013e519a43fbdef8b6a3640eafab2a6cb20bc9" + "hash": "3a1e3b5eb2b223a3b7b8cdcf96b961724d72ddd7d9e276f9fcd730572bd1bba2" } }, { @@ -990,7 +1117,7 @@ ] }, "meta": { - "hash": "15dec93f30e55d25c07f9d5456d5e96f9b6a5a794d9a0874a39be0359a42c10f" + "hash": "be7662f58cb7e4a29f719939f58214192107a86d764cd2ef9fbf583ee6dc5f59" } }, { @@ -1006,7 +1133,7 @@ ] }, "meta": { - "hash": "8c0dea46f260c7edfa363a0a295683d858c2011c9220b2d4fc53025a684388f0" + "hash": "44281b36c4ae79373b6266790741f21225e47c989e3bbfe232b939f5b69270c6" } } ], diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index e985ca005..e877dedf8 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -8,8 +8,8 @@ "fileName": "all_equal.md", "text": "Check if all elements in a list are equal.\n\nUse `[1:]` and `[:-1]` to compare all the values in the given list.\n\n", "codeBlocks": { - "code": "```py\ndef all_equal(lst):\n return lst[1:] == lst[:-1]\n```", - "example": "```py\nall_equal([1, 2, 3, 4, 5, 6]) # False\nall_equal([1, 1, 1, 1]) # True\n```" + "code": "def all_equal(lst):\r\n return lst[1:] == lst[:-1]", + "example": "all_equal([1, 2, 3, 4, 5, 6]) # False\r\nall_equal([1, 1, 1, 1]) # True" }, "tags": [ "list", @@ -17,7 +17,7 @@ ] }, "meta": { - "hash": "6bf29cb176e4e9ce664ad04c6d262b6848f26639624dbb812eb1074d1c68b82a" + "hash": "650f05b1f5d4420f6e44546b34fbb1dc381e9500b4c64adf7598651d5c188223" } }, { @@ -28,8 +28,8 @@ "fileName": "all_unique.md", "text": "Returns `True` if all the values in a flat list are unique, `False` otherwise.\n\nUse `set()` on the given list to remove duplicates, compare its length with the length of the list.\n\n", "codeBlocks": { - "code": "```py\ndef all_unique(lst):\n return len(lst) == len(set(lst))\n```", - "example": "```py\nx = [1,2,3,4,5,6]\ny = [1,2,2,3,4,5]\nall_unique(x) # True\nall_unique(y) # False\n```" + "code": "def all_unique(lst):\r\n return len(lst) == len(set(lst))", + "example": "x = [1,2,3,4,5,6]\r\ny = [1,2,2,3,4,5]\r\nall_unique(x) # True\r\nall_unique(y) # False" }, "tags": [ "list", @@ -37,7 +37,7 @@ ] }, "meta": { - "hash": "ca101c9e623105dc857f1484fcea9211dda0dffc224834a5609d125257a7040f" + "hash": "9b1bdbb130a4fa6301e994359bf162bec262a35fdfd3f00519f7f6a59f1137c0" } }, { @@ -48,8 +48,8 @@ "fileName": "average.md", "text": "Returns the average of two or more numbers.\n\nUse `sum()` to sum all of the `args` provided, divide by `len(args)`.\n\n", "codeBlocks": { - "code": "```py\ndef average(*args):\n return sum(args, 0.0) / len(args)\n```", - "example": "```py\naverage(*[1, 2, 3]) # 2.0\naverage(1, 2, 3) # 2.0\n```" + "code": "def average(*args):\r\n return sum(args, 0.0) / len(args)", + "example": "average(*[1, 2, 3]) # 2.0\r\naverage(1, 2, 3) # 2.0" }, "tags": [ "math", @@ -58,7 +58,7 @@ ] }, "meta": { - "hash": "78bfdea5946774504eea1ba100974ad48ce8cb0a1ce1404cee8d885f35bb93a1" + "hash": "ff65e9a114f47afb53e56a54588e418ef886f8bc508ed32543c97bd1d5966aae" } }, { @@ -69,8 +69,8 @@ "fileName": "average_by.md", "text": "Returns the average of a list, after mapping each element to a value using the provided function.\n\nUse `map()` to map each element to the value returned by `fn`.\nUse `sum()` to sum all of the mapped values, divide by `len(lst)`.\n\n", "codeBlocks": { - "code": "```py\ndef average_by(lst, fn=lambda x: x):\n return sum(map(fn, lst), 0.0) / len(lst)\n```", - "example": "```py\naverage_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda x: x['n']) # 5.0\n```" + "code": "def average_by(lst, fn=lambda x: x):\r\n return sum(map(fn, lst), 0.0) / len(lst)", + "example": "average_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda x: x['n']) # 5.0" }, "tags": [ "math", @@ -80,7 +80,7 @@ ] }, "meta": { - "hash": "8de876b55fd8b75ec395a5d70c29d83c7515d3ae2d59b9c7848e66e6cb854af0" + "hash": "360d3b11343373558e25575401add239d4ab85154b72bfae347b19a487df35f3" } }, { @@ -91,8 +91,8 @@ "fileName": "bifurcate.md", "text": "Splits values into two groups. \nIf an element in `filter` is `True`, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.\n\nUse list comprehension and `enumerate()` to add elements to groups, based on `filter`.\n\n", "codeBlocks": { - "code": "```py\ndef bifurcate(lst, filter):\n return [\n [x for i,x in enumerate(lst) if filter[i] == True],\n [x for i,x in enumerate(lst) if filter[i] == False]\n ]\n```", - "example": "```py\nbifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True]) # [ ['beep', 'boop', 'bar'], ['foo'] ]\n```" + "code": "def bifurcate(lst, filter):\r\n return [\r\n [x for i,x in enumerate(lst) if filter[i] == True],\r\n [x for i,x in enumerate(lst) if filter[i] == False]\r\n ]", + "example": "bifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True]) # [ ['beep', 'boop', 'bar'], ['foo'] ]" }, "tags": [ "list", @@ -100,7 +100,7 @@ ] }, "meta": { - "hash": "22c836b50fafc995904cc69096235ef9fb4c77a6faf38c4c5a4a88b2ed2126a8" + "hash": "49c7d4e1f7175d460074d776cfb0b534ddf753001b66d0e3cacc4177c59e994c" } }, { @@ -111,8 +111,8 @@ "fileName": "bifurcate_by.md", "text": "Splits values into two groups according to a function, which specifies which group an element in the input list belongs to. \nIf the function returns `True`, the element belongs to the first group; otherwise, it belongs to the second group.\n\nUse list comprehension to add elements to groups, based on `fn`.\n\n", "codeBlocks": { - "code": "```py\ndef bifurcate_by(lst, fn):\n return [\n [x for x in lst if fn(x)],\n [x for x in lst if not fn(x)]\n ]\n```", - "example": "```py\nbifurcate_by(['beep', 'boop', 'foo', 'bar'], lambda x: x[0] == 'b') # [ ['beep', 'boop', 'bar'], ['foo'] ]\n```" + "code": "def bifurcate_by(lst, fn):\r\n return [\r\n [x for x in lst if fn(x)],\r\n [x for x in lst if not fn(x)]\r\n ]", + "example": "bifurcate_by(['beep', 'boop', 'foo', 'bar'], lambda x: x[0] == 'b') # [ ['beep', 'boop', 'bar'], ['foo'] ]" }, "tags": [ "list", @@ -121,7 +121,7 @@ ] }, "meta": { - "hash": "2e62d552ca03ff9bc39f920f4e24745945729ba35acc8ba75267b315a9b43563" + "hash": "0975a7d514bc4f5c1c13731165b1a43e43b9dc0eff289fffdfdd4d67af92e23b" } }, { @@ -132,8 +132,8 @@ "fileName": "byte_size.md", "text": "Returns the length of a string in bytes.\n\nUse `string.encode('utf-8')` to encode the given string and return its length.\n\n", "codeBlocks": { - "code": "```py\ndef byte_size(string):\n return len(string.encode('utf-8'))\n```", - "example": "```py\nbyte_size('😀') # 4\nbyte_size('Hello World') # 11\n```" + "code": "def byte_size(string):\r\n return len(string.encode('utf-8'))", + "example": "byte_size('😀') # 4\r\nbyte_size('Hello World') # 11" }, "tags": [ "string", @@ -141,7 +141,28 @@ ] }, "meta": { - "hash": "ff655042992e2a6cded2c39439eca6a6542e4d7b4d019c9c8721fa61be6ccdb8" + "hash": "a80c7f1115762fffeaee2b4d75e36ce88dd288bdb4a99ae9c68c038223cbe00e" + } + }, + { + "id": "camel", + "title": "camel", + "type": "snippet", + "attributes": { + "fileName": "camel.md", + "text": "Converts a string to camelcase.\n\nBreak the string into words and combine them capitalizing the first letter of each word, using a regexp.\n\n", + "codeBlocks": { + "code": "import re\r\n\r\ndef camel(str):\r\n s = re.sub(r\"(\\s|_|-)+\",\"\",\r\n re.sub(r\"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+\",\r\n lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(),str)\r\n )\r\n return s[0].lower() + s[1:]", + "example": "camel('some_database_field_name'); # 'someDatabaseFieldName'\r\ncamel('Some label that needs to be camelized'); # 'someLabelThatNeedsToBeCamelized'\r\ncamel('some-javascript-property'); # 'someJavascriptProperty'\r\ncamel('some-mixed_string with spaces_underscores-and-hyphens'); # 'someMixedStringWithSpacesUnderscoresAndHyphens'" + }, + "tags": [ + "string", + "regexp", + "intermediate" + ] + }, + "meta": { + "hash": "e003dc4980414b0731c01d4b4bf3f08774c3588de95bf38c17ad0e08bb1bd838" } }, { @@ -152,8 +173,8 @@ "fileName": "capitalize.md", "text": "Capitalizes the first letter of a string.\n\nCapitalize the first letter of the string and then add it with rest of the string. \nOmit the `lower_rest` parameter to keep the rest of the string intact, or set it to `True` to convert to lowercase.\n\n", "codeBlocks": { - "code": "```py\ndef capitalize(string, lower_rest=False):\n return string[:1].upper() + (string[1:].lower() if lower_rest else string[1:])\n```", - "example": "```py\ncapitalize('fooBar') # 'FooBar'\ncapitalize('fooBar', True) # 'Foobar'\n```" + "code": "def capitalize(string, lower_rest=False):\r\n return string[:1].upper() + (string[1:].lower() if lower_rest else string[1:])", + "example": "capitalize('fooBar') # 'FooBar'\r\ncapitalize('fooBar', True) # 'Foobar'" }, "tags": [ "string", @@ -161,7 +182,7 @@ ] }, "meta": { - "hash": "f4b0ecfe5d6eb18a65699fbe706737723c2c0de6a16e07e4c7686e9ecbad29c5" + "hash": "830372b0e6a7a008c1ee9ec4ef11c6063ff14b94b09b4b081d220079fa89e2aa" } }, { @@ -172,8 +193,8 @@ "fileName": "capitalize_every_word.md", "text": "Capitalizes the first letter of every word in a string.\n\nUse `string.title()` to capitalize first letter of every word in the string.\n\n", "codeBlocks": { - "code": "```py\ndef capitalize_every_word(string):\n return string.title()\n```", - "example": "```py\ncapitalize_every_word('hello world!') # 'Hello World!'\n```" + "code": "def capitalize_every_word(string):\r\n return string.title()", + "example": "capitalize_every_word('hello world!') # 'Hello World!'" }, "tags": [ "string", @@ -181,7 +202,7 @@ ] }, "meta": { - "hash": "ec399b1f2bcb0888956d1ecb40fd509f22ba902cd7f3c53af02729d52f021f86" + "hash": "699eb144ef3a23fbc4375096ac28535f809b4fc5bd4a493dfbf0f7e8df61a446" } }, { @@ -192,8 +213,8 @@ "fileName": "cast_list.md", "text": "Casts the provided value as an array if it's not one.\n\nUse `isinstance()` to check if the given value is a list and return it as-is or encapsulated in a list accordingly.\n\n", "codeBlocks": { - "code": "```py\ndef cast_list(val):\n return val if isinstance(val, list) else [val]\n```", - "example": "```py\ncast_list('foo'); # ['foo']\ncast_list([1]); # [1]\n```" + "code": "def cast_list(val):\r\n return val if isinstance(val, list) else [val]", + "example": "cast_list('foo'); # ['foo']\r\ncast_list([1]); # [1]" }, "tags": [ "utility", @@ -202,7 +223,7 @@ ] }, "meta": { - "hash": "4a9cb79c384099543163d3b1b7e2fa389a4cc373b2b76b17563a4597ed29a4c7" + "hash": "9403a0321a9fcb907a18cadf8283be3ecf89a7a51e8679978e26cfb050918ced" } }, { @@ -213,8 +234,8 @@ "fileName": "chunk.md", "text": "Chunks a list into smaller lists of a specified size.\n\nUse `list()` and `range()` to create a list of the desired `size`.\nUse `map()` on the list and fill it with splices of the given list.\nFinally, return use created list.\n\n", "codeBlocks": { - "code": "```py\nfrom math import ceil\n\ndef chunk(lst, size):\n return list(\n map(lambda x: lst[x * size:x * size + size],\n list(range(0, ceil(len(lst) / size)))))\n```", - "example": "```py\nchunk([1,2,3,4,5],2) # [[1,2],[3,4],5]\n```" + "code": "from math import ceil\r\n\r\ndef chunk(lst, size):\r\n return list(\r\n map(lambda x: lst[x * size:x * size + size],\r\n list(range(0, ceil(len(lst) / size)))))", + "example": "chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]" }, "tags": [ "list", @@ -222,7 +243,7 @@ ] }, "meta": { - "hash": "f8c9cdb2261bfe2932bc7d3d11853a3d42d468a88ad515e9f15d9abffe9b30a6" + "hash": "3a27343fc9afa6666e7ab4b015ebe883c083fbd37841058a37bb0e143d11b1ff" } }, { @@ -233,8 +254,8 @@ "fileName": "clamp_number.md", "text": "Clamps `num` within the inclusive range specified by the boundary values `a` and `b`.\n\nIf `num` falls within the range, return `num`. \nOtherwise, return the nearest number in the range.\n\n", "codeBlocks": { - "code": "```py\ndef clamp_number(num,a,b):\n return max(min(num, max(a,b)),min(a,b))\n```", - "example": "```py\nclamp_number(2, 3, 5) # 3\nclamp_number(1, -1, -5) # -1\n```" + "code": "def clamp_number(num,a,b):\r\n return max(min(num, max(a,b)),min(a,b))", + "example": "clamp_number(2, 3, 5) # 3\r\nclamp_number(1, -1, -5) # -1" }, "tags": [ "math", @@ -242,7 +263,7 @@ ] }, "meta": { - "hash": "7e3dc4519a629ec87f72e5b495227733735c9c2262e5f6a89264191967cfad31" + "hash": "20549e07442c96474df3db5824f7ac9eb8f1763234ef4abb0b500845cb71383d" } }, { @@ -253,8 +274,8 @@ "fileName": "compact.md", "text": "Removes falsey values from a list.\n\nUse `filter()` to filter out falsey values (`False`, `None`, `0`, and `\"\"`).\n\n", "codeBlocks": { - "code": "```py\ndef compact(lst):\n return list(filter(bool, lst))\n```", - "example": "```py\ncompact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]\n```" + "code": "def compact(lst):\r\n return list(filter(bool, lst))", + "example": "compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]" }, "tags": [ "list", @@ -262,7 +283,7 @@ ] }, "meta": { - "hash": "6b98c58b6aecf1b58ed36f55a407bf2f2d68938723d37472fe43d9e652d93fe6" + "hash": "2cef075bcbeb781fdfd9a2284f04281922c50c79273e90b1d6fa858b06c8fb60" } }, { @@ -273,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": "```py\ndef 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\n```", - "example": "```py\nfrom 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}\n```" + "code": "def count_by(arr, fn=lambda x: x):\r\n key = {}\r\n for el in map(fn, arr):\r\n key[el] = 0 if el not in key else key[el]\r\n key[el] += 1\r\n return key", + "example": "from math import floor\r\ncount_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2}\r\ncount_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}" }, "tags": [ "list", @@ -282,7 +303,7 @@ ] }, "meta": { - "hash": "e78cb9229a2bc4c882fa7d901dbdee6ca9c33bde348650210afb210d3b98e1f6" + "hash": "b5b6656857a0c11b88b579c1f91451b78f7f2bd5c9eb209df7f15d4885d6bbc0" } }, { @@ -293,8 +314,8 @@ "fileName": "count_occurences.md", "text": "Counts the occurrences of a value in a list.\n\nIncrement a counter for every item in the list that has the given value and is of the same type.\n\n", "codeBlocks": { - "code": "```py\ndef count_occurrences(lst, val):\n return len([x for x in lst if x == val and type(x) == type(val)])\n```", - "example": "```py\ncount_occurrences([1, 1, 2, 1, 2, 3], 1) # 3\n```" + "code": "def count_occurrences(lst, val):\r\n return len([x for x in lst if x == val and type(x) == type(val)])", + "example": "count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3" }, "tags": [ "list", @@ -302,7 +323,7 @@ ] }, "meta": { - "hash": "eee88217431699369070beb38ca0265d963c78321bfd98de9e3a38533c6ee90e" + "hash": "9bd71bbccb9b18b35856360e4ccc7ae477ec7cd7e8363fc851ec3eac49813334" } }, { @@ -313,8 +334,8 @@ "fileName": "decapitalize.md", "text": "Decapitalizes the first letter of a string.\n\nDecapitalize the first letter of the string and then add it with rest of the string. \nOmit the `upper_rest` parameter to keep the rest of the string intact, or set it to `True` to convert to uppercase.\n\n", "codeBlocks": { - "code": "```py\ndef decapitalize(string, upper_rest=False):\n return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:])\n```", - "example": "```py\ndecapitalize('FooBar') # 'fooBar'\ndecapitalize('FooBar', True) # 'fOOBAR'\n```" + "code": "def decapitalize(string, upper_rest=False):\r\n return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:])", + "example": "decapitalize('FooBar') # 'fooBar'\r\ndecapitalize('FooBar', True) # 'fOOBAR'" }, "tags": [ "string", @@ -322,7 +343,7 @@ ] }, "meta": { - "hash": "aea9a271d47a5606912e2482b8cc6bfb7b7382c4d0c86545b194cc0ad5f342b5" + "hash": "05d4132e66af5a615c220781d247b7050445e001e76455343760a85e62edd106" } }, { @@ -333,8 +354,8 @@ "fileName": "deep_flatten.md", "text": "Deep flattens a list.\n\nUse recursion. \nDefine a function, `spread`, that uses either `list.extend()` or `list.append()` on each element in a list to flatten it.\nUse `list.extend()` with an empty list and the `spread` function to flatten a list.\nRecursively flatten each element that is a list.\n\n", "codeBlocks": { - "code": "```py\ndef spread(arg):\n ret = []\n for i in arg:\n if isinstance(i, list):\n ret.extend(i)\n else:\n ret.append(i)\n return ret\n\ndef deep_flatten(lst):\n result = []\n result.extend(\n spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))\n return result\n```", - "example": "```py\ndeep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]\n```" + "code": "def spread(arg):\r\n ret = []\r\n for i in arg:\r\n if isinstance(i, list):\r\n ret.extend(i)\r\n else:\r\n ret.append(i)\r\n return ret\r\n\r\ndef deep_flatten(lst):\r\n result = []\r\n result.extend(\r\n spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))\r\n return result", + "example": "deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]" }, "tags": [ "list", @@ -343,7 +364,7 @@ ] }, "meta": { - "hash": "a100d5704afe48a7dce26871bc50c993670d7388d8e4958c02c07efe07884167" + "hash": "3f0192a6f20dd5c0d4635f3e6f736fefbfe433f2cf4f36b3da12f8467f7704c0" } }, { @@ -354,8 +375,8 @@ "fileName": "difference.md", "text": "Returns the difference between two iterables.\n\nCreate a `set` from `b`, then use list comprehension on `a` to only keep values not contained in the previously created set, `_b`.\n\n", "codeBlocks": { - "code": "```py\ndef difference(a, b):\n _b = set(b)\n return [item for item in a if item not in _b]\n```", - "example": "```py\ndifference([1, 2, 3], [1, 2, 4]) # [3]\n```" + "code": "def difference(a, b):\r\n _b = set(b)\r\n return [item for item in a if item not in _b]", + "example": "difference([1, 2, 3], [1, 2, 4]) # [3]" }, "tags": [ "list", @@ -363,7 +384,7 @@ ] }, "meta": { - "hash": "479fcdba73e7429ccb82649a354159ad9008c63f852c960d6733c10ecafceae3" + "hash": "4b7b9afe16e5ffb48c84f267364ff5041598e59dce5fb7f3f165ad9835fb5175" } }, { @@ -374,8 +395,8 @@ "fileName": "difference_by.md", "text": "Returns the difference between two lists, after applying the provided function to each list element of both.\n\nCreate a `set` by applying `fn` to each element in `b`, then use list comprehension in combination with `fn` on `a` to only keep values not contained in the previously created set, `_b`.\n\n", "codeBlocks": { - "code": "```py\ndef difference_by(a, b, fn):\n _b = set(map(fn, b))\n return [item for item in a if fn(item) not in _b]\n```", - "example": "```py\nfrom math import floor\ndifference_by([2.1, 1.2], [2.3, 3.4],floor) # [1.2]\ndifference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x: 2 } ]\n```" + "code": "def difference_by(a, b, fn):\r\n _b = set(map(fn, b))\r\n return [item for item in a if fn(item) not in _b]", + "example": "from math import floor\r\ndifference_by([2.1, 1.2], [2.3, 3.4],floor) # [1.2]\r\ndifference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x: 2 } ]" }, "tags": [ "list", @@ -384,7 +405,7 @@ ] }, "meta": { - "hash": "a240b4f3152f847cb5f3877ab2fa1f4b75259c5ce11e28a773ad34bdba14f0a8" + "hash": "67fcb3f28272e5c105374ce43a42382eba8ddb1c327989c102f82cc441ea0fe1" } }, { @@ -395,8 +416,8 @@ "fileName": "digitize.md", "text": "Converts a number to an array of digits.\n\nUse `map()` combined with `int` on the string representation of `n` and return a list from the result.\n\n", "codeBlocks": { - "code": "```py\ndef digitize(n):\n return list(map(int, str(n)))\n```", - "example": "```py\ndigitize(123) # [1, 2, 3]\n```" + "code": "def digitize(n):\r\n return list(map(int, str(n)))", + "example": "digitize(123) # [1, 2, 3]" }, "tags": [ "math", @@ -405,7 +426,7 @@ ] }, "meta": { - "hash": "576de2896d2565d44da5d42b4b57b37cf1b2e5fc69103ab48a9ed532040ffaf2" + "hash": "be2a8632262614ee14126e69efedbfb6517fdcc5c3453f859d8cd161d44e8b6e" } }, { @@ -416,8 +437,8 @@ "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", "codeBlocks": { - "code": "```py\ndef every(lst, fn=lambda x: not not x):\n for el in lst:\n if not fn(el):\n return False\n return True\n```", - "example": "```py\nevery([4, 2, 3], lambda x: x > 1) # True\nevery([1, 2, 3]) # True\n```" + "code": "def every(lst, fn=lambda x: not not x):\r\n for el in lst:\r\n if not fn(el):\r\n return False\r\n return True", + "example": "every([4, 2, 3], lambda x: x > 1) # True\r\nevery([1, 2, 3]) # True" }, "tags": [ "list", @@ -426,7 +447,7 @@ ] }, "meta": { - "hash": "8356afa6609f21bef9f48c7fb4b82553cae27d67c4bb4e31bfda98ee814cf269" + "hash": "1a444727bf46de6441f3ebc87508c8cd2b1280dc1d0659d00e46a012e703d9d6" } }, { @@ -437,8 +458,8 @@ "fileName": "every_nth.md", "text": "Returns every nth element in a list.\n\nUse `[1::nth]` to create a new list that contains every nth element of the given list.\n\n", "codeBlocks": { - "code": "```py\ndef every_nth(lst, nth):\n return lst[1::nth]\n```", - "example": "```py\nevery_nth([1, 2, 3, 4, 5, 6], 2) # [ 2, 4, 6 ]\n```" + "code": "def every_nth(lst, nth):\r\n return lst[1::nth]", + "example": "every_nth([1, 2, 3, 4, 5, 6], 2) # [ 2, 4, 6 ]" }, "tags": [ "list", @@ -446,7 +467,7 @@ ] }, "meta": { - "hash": "d5c51547b66fcd9d42def2a6d92ba98429a12c6bec56a0dca8e4c0289525d5de" + "hash": "b0e4ab30cc730950401ab4291ce404b37132bc32bdeebbac1de871c581a55919" } }, { @@ -457,8 +478,8 @@ "fileName": "factorial.md", "text": "Calculates the factorial of a number.\n\nUse recursion. \nIf `num` is less than or equal to `1`, return `1`. \nOtherwise, return the product of `num` and the factorial of `num - 1`. \nThrows an exception if `num` is a negative or a floating point number.\n\n", "codeBlocks": { - "code": "```py\ndef factorial(num):\n if not ((num >= 0) & (num % 1 == 0)):\n raise Exception(\n f\"Number( {num} ) can't be floating point or negative \")\n return 1 if num == 0 else num * factorial(num - 1)\n```", - "example": "```py\nfactorial(6) # 720\n```" + "code": "def factorial(num):\r\n if not ((num >= 0) & (num % 1 == 0)):\r\n raise Exception(\r\n f\"Number( {num} ) can't be floating point or negative \")\r\n return 1 if num == 0 else num * factorial(num - 1)", + "example": "factorial(6) # 720" }, "tags": [ "math", @@ -467,7 +488,7 @@ ] }, "meta": { - "hash": "3241103df06912913bbcfb51e03c1d355a7329e367aa9d4f6a770683bc196df8" + "hash": "e22b08efd49d4bba2cef3f7d2315e55a36223d7c861c19eaa23ca1b2ba72dc5b" } }, { @@ -478,8 +499,8 @@ "fileName": "fibonacci.md", "text": "Generates an array, containing the Fibonacci sequence, up until the nth term.\n\nStarting with `0` and `1`, use `list.apoend() to add the sum of the last two numbers of the list to the end of the list, until the length of the list reaches `n`. \nIf `n` is less or equal to `0`, return a list containing `0`.\n\n", "codeBlocks": { - "code": "```py\r\ndef fibonacci(n):\r\n if n <= 0:\r\n return [0]\r\n\r\n sequence = [0, 1]\r\n while len(sequence) <= n:\r\n next_value = sequence[len(sequence) - 1] + sequence[len(sequence) - 2]\r\n sequence.append(next_value)\r\n\r\n return sequence\r\n```", - "example": "```py\r\nfibonacci(7) # [0, 1, 1, 2, 3, 5, 8, 13]\r\n```" + "code": "def fibonacci(n):\r\n if n <= 0:\r\n return [0]\r\n\r\n sequence = [0, 1]\r\n while len(sequence) <= n:\r\n next_value = sequence[len(sequence) - 1] + sequence[len(sequence) - 2]\r\n sequence.append(next_value)\r\n\r\n return sequence", + "example": "fibonacci(7) # [0, 1, 1, 2, 3, 5, 8, 13]" }, "tags": [ "math", @@ -499,8 +520,8 @@ "fileName": "filter_non_unique.md", "text": "Filters out the non-unique values in a list.\n\nUse list comprehension and `list.count()` to create a list containing only the unique values.\n\n", "codeBlocks": { - "code": "```py\ndef filter_non_unique(lst):\n return [item for item in lst if lst.count(item) == 1]\n```", - "example": "```py\nfilter_non_unique([1, 2, 2, 3, 4, 4, 5]) # [1, 3, 5]\n```" + "code": "def filter_non_unique(lst):\r\n return [item for item in lst if lst.count(item) == 1]", + "example": "filter_non_unique([1, 2, 2, 3, 4, 4, 5]) # [1, 3, 5]" }, "tags": [ "list", @@ -508,7 +529,7 @@ ] }, "meta": { - "hash": "05679ae115d276830ec769a04b0800d92eabc6a09678fc1a9cf6013c813cc650" + "hash": "1bbae73c12dc8ee2bf80e93b2328d8f5138e603fa0ea381999fc388a25cdb107" } }, { @@ -519,8 +540,8 @@ "fileName": "gcd.md", "text": "Calculates the greatest common divisor of a list of numbers.\n\nUse `reduce()` and `math.gcd` over the given list.\n\n", "codeBlocks": { - "code": "```py\nfrom functools import reduce\nimport math\n\ndef gcd(numbers):\n return reduce(math.gcd, numbers)\n```", - "example": "```py\ngcd([8,36,28]) # 4\n```" + "code": "from functools import reduce\r\nimport math\r\n\r\ndef gcd(numbers):\r\n return reduce(math.gcd, numbers)", + "example": "gcd([8,36,28]) # 4" }, "tags": [ "math", @@ -528,7 +549,7 @@ ] }, "meta": { - "hash": "dbb01e7253dbb6f3e2f53271fc14fb7b3ee9816fe56266f78a54b4ca21c94cd7" + "hash": "ab1de2550801b4c4c2ab773912ea60553a9ef46d5928c879964432825aa6e7e3" } }, { @@ -539,8 +560,8 @@ "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", "codeBlocks": { - "code": "```py\ndef group_by(lst, fn):\n groups = {}\n for key in list(map(fn,lst)):\n groups[key] = [item for item in lst if fn(item) == key]\n return groups\n```", - "example": "```py\nimport math\ngroup_by([6.1, 4.2, 6.3], math.floor); # {4: [4.2], 6: [6.1, 6.3]}\ngroup_by(['one', 'two', 'three'], 'length'); # {3: ['one', 'two'], 5: ['three']}\n```" + "code": "def group_by(lst, fn):\r\n groups = {}\r\n for key in list(map(fn,lst)):\r\n groups[key] = [item for item in lst if fn(item) == key]\r\n return groups", + "example": "import math\r\ngroup_by([6.1, 4.2, 6.3], math.floor); # {4: [4.2], 6: [6.1, 6.3]}\r\ngroup_by(['one', 'two', 'three'], 'length'); # {3: ['one', 'two'], 5: ['three']}" }, "tags": [ "list", @@ -549,7 +570,7 @@ ] }, "meta": { - "hash": "e103229c295cbfcbf26b5529b068e382bb0a1a04e080311e46302721841bbfe0" + "hash": "592f01a019379e6e1eeca55406a923930ab77133a5653febb585847ed387df58" } }, { @@ -560,8 +581,8 @@ "fileName": "has_duplicates.md", "text": "Returns `True` if there are duplicate values in a flast list, `False` otherwise.\n\nUse `set()` on the given list to remove duplicates, compare its length with the length of the list.\n\n", "codeBlocks": { - "code": "```py\ndef has_duplicates(lst):\n return len(lst) != len(set(lst))\n```", - "example": "```py\nx = [1,2,3,4,5,5]\ny = [1,2,3,4,5]\nhas_duplicates(x) # True\nhas_duplicates(y) # False\n```" + "code": "def has_duplicates(lst):\r\n return len(lst) != len(set(lst))", + "example": "x = [1,2,3,4,5,5]\r\ny = [1,2,3,4,5]\r\nhas_duplicates(x) # True\r\nhas_duplicates(y) # False" }, "tags": [ "list", @@ -569,7 +590,7 @@ ] }, "meta": { - "hash": "762663e25e978ee96720d6fd977e4ca42b328150fa654d0e600c636284552712" + "hash": "8aa2f1de87b94cb1eb6cdc0c048d051aae9d5561b8e6ca92b66c4543c8e24cd5" } }, { @@ -580,8 +601,8 @@ "fileName": "head.md", "text": "Returns the head of a list.\n\nuse `lst[0]` to return the first element of the passed list.\n\n", "codeBlocks": { - "code": "```py\ndef head(lst):\n return lst[0]\n```", - "example": "```py\nhead([1, 2, 3]); # 1\n```" + "code": "def head(lst):\r\n return lst[0]", + "example": "head([1, 2, 3]); # 1" }, "tags": [ "list", @@ -589,7 +610,7 @@ ] }, "meta": { - "hash": "6c86b52c2c7a56a4527114736664a3720b2026d2419b87daf36de6877ad0e4b0" + "hash": "079183a15856f39a61ea1c375a272565e3f70826f2579eaa66683f06aca7205b" } }, { @@ -600,8 +621,8 @@ "fileName": "in_range.md", "text": "Checks if the given number falls within the given range.\n\nUse arithmetic comparison to check if the given number is in the specified range.\nIf the second parameter, `end`, is not specified, the range is considered to be from `0` to `start`.\n\n", "codeBlocks": { - "code": "```py\ndef in_range(n, start, end = 0):\n if (start > end):\n end, start = start, end\n return start <= n <= end\n```", - "example": "```py\nin_range(3, 2, 5); # True\nin_range(3, 4); # True\nin_range(2, 3, 5); # False\nin_range(3, 2); # False\n```" + "code": "def in_range(n, start, end = 0):\r\n if (start > end):\r\n end, start = start, end\r\n return start <= n <= end", + "example": "in_range(3, 2, 5); # True\r\nin_range(3, 4); # True\r\nin_range(2, 3, 5); # False\r\nin_range(3, 2); # False" }, "tags": [ "math", @@ -609,7 +630,7 @@ ] }, "meta": { - "hash": "f65d098189837551aef365c68bd9389a0c1e84e89233fa5a886e889ce6981f9f" + "hash": "d3d40fb3ff5df9a9bb70b825b91952ca16ed4033fae94a1ea0eac7f64566a989" } }, { @@ -620,8 +641,8 @@ "fileName": "initial.md", "text": "Returns all the elements of a list except the last one.\n\nUse `lst[0:-1]` to return all but the last element of the list.\n\n", "codeBlocks": { - "code": "```py\ndef initial(lst):\n return lst[0:-1]\n```", - "example": "```py\ninitial([1, 2, 3]); # [1,2]\n```" + "code": "def initial(lst):\r\n return lst[0:-1]", + "example": "initial([1, 2, 3]); # [1,2]" }, "tags": [ "list", @@ -629,7 +650,7 @@ ] }, "meta": { - "hash": "cf0229dd484711e5847d85952841a13800bb5e0767c1de40e219f63cfed3e0f1" + "hash": "792086091d9aa9180f447ee3fb0d606f2dbc38c2b917d1c7a610d32071d40cc2" } }, { @@ -640,8 +661,8 @@ "fileName": "initialiaze_2d_list.md", "text": "Initializes a 2D list of given width and height and value.\n\nUse list comprehension and `range()` to generate `h` rows where each is a list with length `h`, initialized with `val`.\nIf `val` is not provided, default to `None`.\n\nExplain briefly how the snippet works.\n\n", "codeBlocks": { - "code": "```py\ndef initialize_2d_list(w,h, val = None):\n return [[val for x in range(w)] for y in range(h)]\n```", - "example": "```py\ninitialize_2d_list(2, 2, 0) # [[0,0], [0,0]]\n```" + "code": "def initialize_2d_list(w,h, val = None):\r\n return [[val for x in range(w)] for y in range(h)]", + "example": "initialize_2d_list(2, 2, 0) # [[0,0], [0,0]]" }, "tags": [ "list", @@ -649,7 +670,7 @@ ] }, "meta": { - "hash": "a848fc33363ab533f745e081d885b4c981716ae063867ea9a2b471fd8fc92ac7" + "hash": "e141f00ba64f6308c0563ff6132191a798a14fe7783be884ab46bf698892c8cc" } }, { @@ -660,8 +681,8 @@ "fileName": "initialize_list_with_range.md", "text": "Initializes a list containing the numbers in the specified range where `start` and `end` are inclusive with their common difference `step`.\n\nUse list comprehension and `range()` to generate a list of the appropriate length, filled with the desired values in the given range.\nOmit `start` to use the default value of `0`.\nOmit `step` to use the default value of `1`.\n\n", "codeBlocks": { - "code": "```py\ndef initialize_list_with_range(end, start = 0, step = 1):\n return [x for x in range(start, end + 1, step)]\n```", - "example": "```py\ninitialize_list_with_range(5) # [0, 1, 2, 3, 4, 5]\ninitialize_list_with_range(7,3) # [3, 4, 5, 6, 7]\ninitialize_list_with_range(9,0,2) # [0, 2, 4, 6, 8]\n```" + "code": "def initialize_list_with_range(end, start = 0, step = 1):\r\n return [x for x in range(start, end + 1, step)]", + "example": "initialize_list_with_range(5) # [0, 1, 2, 3, 4, 5]\r\ninitialize_list_with_range(7,3) # [3, 4, 5, 6, 7]\r\ninitialize_list_with_range(9,0,2) # [0, 2, 4, 6, 8]" }, "tags": [ "list", @@ -669,7 +690,7 @@ ] }, "meta": { - "hash": "b68fef168bb92503e54278a95753fd48b3cb9f5151e17d36ce69c7eae298683c" + "hash": "729784a5d236cb13fde71cab421a1487481d34f26e8acc3c923695ff972a965a" } }, { @@ -680,8 +701,8 @@ "fileName": "initialize_list_with_values.md", "text": "Initializes and fills a list with the specified value.\n\nUse list comprehension and `range()` to generate a list of length equal to `n`, filled with the desired values.\nOmit `val` to use the default value of `0`.\n\n", "codeBlocks": { - "code": "```py\ndef initialize_list_with_values(n, val = 0):\n return [val for x in range(n)]\n```", - "example": "```py\ninitialize_list_with_values(5, 2) # [2, 2, 2, 2, 2]\n```" + "code": "def initialize_list_with_values(n, val = 0):\r\n return [val for x in range(n)]", + "example": "initialize_list_with_values(5, 2) # [2, 2, 2, 2, 2]" }, "tags": [ "list", @@ -689,7 +710,7 @@ ] }, "meta": { - "hash": "87604b968fccbc3d60399619b388f5939d646059db467fc2c86f99138f68b952" + "hash": "a86132d77e383748415b694deff0f452f1ee22763a47ee8fa95a938faceec926" } }, { @@ -700,8 +721,8 @@ "fileName": "intersection.md", "text": "Returns a list of elements that exist in both lists.\n\nCreate a `set` from `b`, then use list comprehension on `a` to only keep values contained in both lists.\n\n", "codeBlocks": { - "code": "```py\ndef intersection(a, b):\n _b = set(b)\n return [item for item in a if item in _b]\n```", - "example": "```py\nintersection([1, 2, 3], [4, 3, 2]) # [2, 3]\n```" + "code": "def intersection(a, b):\r\n _b = set(b)\r\n return [item for item in a if item in _b]", + "example": "intersection([1, 2, 3], [4, 3, 2]) # [2, 3]" }, "tags": [ "list", @@ -709,7 +730,7 @@ ] }, "meta": { - "hash": "a332cce7b7d7e303469e8c39c4926bf92ecb12847c3932985cc633e3a85a23bb" + "hash": "50509c3103ce76585b558ecd98a8833568795ecb260e5b9bf955245078a49af8" } }, { @@ -720,8 +741,8 @@ "fileName": "intersection_by.md", "text": "Returns a list of elements that exist in both lists, after applying the provided function to each list element of both.\n\nCreate a `set` by applying `fn` to each element in `b`, then use list comprehension in combination with `fn` on `a` to only keep values contained in both lists.\n\n", "codeBlocks": { - "code": "```py\ndef intersection_by(a, b, fn):\n _b = set(map(fn, b))\n return [item for item in a if fn(item) in _b]\n```", - "example": "```py\nfrom math import floor\nintersection_by([2.1, 1.2], [2.3, 3.4],floor) # [2.1]\n```" + "code": "def intersection_by(a, b, fn):\r\n _b = set(map(fn, b))\r\n return [item for item in a if fn(item) in _b]", + "example": "from math import floor\r\nintersection_by([2.1, 1.2], [2.3, 3.4],floor) # [2.1]" }, "tags": [ "list", @@ -730,7 +751,7 @@ ] }, "meta": { - "hash": "eb4bff1a92a7848c8184531fee590e8f060a6d4f6ffe214aec4ccda7a5e18031" + "hash": "cde08163638452f63975589a3341c69e4621b364db22f6ab3d205bd895ad8b9a" } }, { @@ -741,8 +762,8 @@ "fileName": "is_anagram.md", "text": "Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).\n\nUse `str.replace()` to remove spaces from both strings.\nCompare the lengths of the two strings, return `False` if they are not equal.\nUse `sorted()` on both strings and compare the results.\n\n", "codeBlocks": { - "code": "```py\ndef is_anagram(str1, str2):\n _str1, _str2 = str1.replace(\" \", \"\"), str2.replace(\" \", \"\")\n\n if len(_str1) != len(_str2):\n return False\n else:\n return sorted(_str1.lower()) == sorted(_str2.lower())\n```", - "example": "```py\nis_anagram(\"anagram\", \"Nag a ram\") # True\n```" + "code": "def is_anagram(str1, str2):\r\n _str1, _str2 = str1.replace(\" \", \"\"), str2.replace(\" \", \"\")\r\n\r\n if len(_str1) != len(_str2):\r\n return False\r\n else:\r\n return sorted(_str1.lower()) == sorted(_str2.lower())", + "example": "is_anagram(\"anagram\", \"Nag a ram\") # True" }, "tags": [ "string", @@ -750,7 +771,7 @@ ] }, "meta": { - "hash": "3080e22832c2a393f1546f4cff18b15b701fbe27dc2368fd26700a05f2f109a2" + "hash": "47c878d10f937760d9096ccedb10357df3ba1e87333fdc8256c9a6938994eef8" } }, { @@ -761,8 +782,8 @@ "fileName": "is_divisible.md", "text": "Checks if the first numeric argument is divisible by the second one.\n\nUse the modulo operator (`%`) to check if the remainder is equal to `0`.\n\n", "codeBlocks": { - "code": "```py\ndef is_divisible(dividend, divisor):\n return dividend % divisor == 0\n```", - "example": "```py\nis_divisible(6, 3) # True\n```" + "code": "def is_divisible(dividend, divisor):\r\n return dividend % divisor == 0", + "example": "is_divisible(6, 3) # True" }, "tags": [ "math", @@ -770,7 +791,7 @@ ] }, "meta": { - "hash": "e6272becc36195e4b76f62baa61d43d9de5472201d1fb589e18bd5f47d6d57b0" + "hash": "0502ded5a0a750f7d4f03940d046a9f41ad0189a79ad7f42d9fe8ce9333b81e8" } }, { @@ -781,8 +802,8 @@ "fileName": "is_even.md", "text": "Returns `True` if the given number is even, `False` otherwise.\n\nChecks whether a number is odd or even using the modulo (`%`) operator. \nReturns `True` if the number is even, `False` if the number is odd.\n\n", "codeBlocks": { - "code": "```py\ndef is_even(num):\n return num % 2 == 0\n```", - "example": "```py\nis_even(3) # False\n```" + "code": "def is_even(num):\r\n return num % 2 == 0", + "example": "is_even(3) # False" }, "tags": [ "math", @@ -790,7 +811,7 @@ ] }, "meta": { - "hash": "354d3fcfa67b5df106c0ff528246bfacd2d7cec4942a7c3a904d87e76d7dae11" + "hash": "0579f16d399d4ed57a52e6575633fe924d1b269268c2b1afe830f34ff0cf9a09" } }, { @@ -801,8 +822,8 @@ "fileName": "is_lower_case.md", "text": "Checks if a string is lower case.\n\nConvert the given string to lower case, using `str.lower()` and compare it to the original.\n\n", "codeBlocks": { - "code": "```py\ndef is_lower_case(string):\n return string == string.lower()\n```", - "example": "```py\nis_lower_case('abc') # True\nis_lower_case('a3@$') # True\nis_lower_case('Ab4') # False\n```" + "code": "def is_lower_case(string):\r\n return string == string.lower()", + "example": "is_lower_case('abc') # True\r\nis_lower_case('a3@$') # True\r\nis_lower_case('Ab4') # False" }, "tags": [ "string", @@ -811,7 +832,7 @@ ] }, "meta": { - "hash": "ed6b313aaf87ba7ea9515679eea19f395f7bbd90b9f8b85ef54ca527115fa690" + "hash": "d84277f13e3250c2e9d8a6afd693d52a7383f29ea1fdf71bd5dd1f3d8d3a5baf" } }, { @@ -822,8 +843,8 @@ "fileName": "is_odd.md", "text": "Returns `True` if the given number is odd, `False` otherwise.\n\nChecks whether a number is even or odd using the modulo (`%`) operator. \nReturns `True` if the number is odd, `False` if the number is even.\n\n", "codeBlocks": { - "code": "```py\ndef is_odd(num):\n return num % 2 == `0`\n```", - "example": "```py\nis_odd(3) # True\n```" + "code": "def is_odd(num):\r\n return num % 2 == `0`", + "example": "is_odd(3) # True" }, "tags": [ "math", @@ -831,7 +852,7 @@ ] }, "meta": { - "hash": "2670b3500855c72812593fd30692701798be1bb3a7a68c2bb65a273fdee5c95a" + "hash": "b112c57fecf98765d44027b570c3f9b9fb304d450584bdee477b558861380ab9" } }, { @@ -842,8 +863,8 @@ "fileName": "is_upper_case.md", "text": "Checks if a string is upper case.\n\nConvert the given string to upper case, using `str.upper()` and compare it to the original.\n\n", "codeBlocks": { - "code": "```py\ndef is_upper_case(string):\n return string == string.upper()\n```", - "example": "```py\nis_upper_case('ABC') # True\nis_upper_case('a3@$') # False\nis_upper_case('aB4') # False\n```" + "code": "def is_upper_case(string):\r\n return string == string.upper()", + "example": "is_upper_case('ABC') # True\r\nis_upper_case('a3@$') # False\r\nis_upper_case('aB4') # False" }, "tags": [ "string", @@ -852,7 +873,28 @@ ] }, "meta": { - "hash": "106225722752b199f974c06aec91721344499e46dbf643e9f2ce990ee8c2d3c1" + "hash": "fd01ad582630e68200df9317aa68877d4942412fab8ce123533460f4937203f2" + } + }, + { + "id": "kebab", + "title": "kebab", + "type": "snippet", + "attributes": { + "fileName": "kebab.md", + "text": "Converts a string to kebab case.\n\nBreak the string into words and combine them adding `-` as a separator, using a regexp.\n\n", + "codeBlocks": { + "code": "import re\r\n\r\ndef kebab(str):\r\n return re.sub(r\"(\\s|_|-)+\",\"-\",\r\n re.sub(r\"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+\",\r\n lambda mo: mo.group(0).lower(),str)\r\n )", + "example": "kebab('camelCase'); # 'camel-case'\r\nkebab('some text'); # 'some-text'\r\nkebab('some-mixed_string With spaces_underscores-and-hyphens'); # 'some-mixed-string-with-spaces-underscores-and-hyphens'\r\nkebab('AllThe-small Things'); # \"all-the-small-things\"" + }, + "tags": [ + "string", + "regexp", + "intermediate" + ] + }, + "meta": { + "hash": "35f4a0ae7433cef616f137b696b909d7f5046685e64af70d59934f4d34e6c5d7" } }, { @@ -863,8 +905,8 @@ "fileName": "keys_only.md", "text": "Returns a flat list of all the keys in a flat dictionary.\n\nUse `dict.keys()` to return the keys in the given dictionary.\nReturn a `list()` of the previous result.\n\n", "codeBlocks": { - "code": "```py\ndef keys_only(flat_dict):\n return list(flat_dict.keys())\n```", - "example": "```py\nages = {\n \"Peter\": 10,\n \"Isabel\": 11,\n \"Anna\": 9,\n}\nkeys_only(ages) # ['Peter', 'Isabel', 'Anna']\n```" + "code": "def keys_only(flat_dict):\r\n return list(flat_dict.keys())", + "example": "ages = {\r\n \"Peter\": 10,\r\n \"Isabel\": 11,\r\n \"Anna\": 9,\r\n}\r\nkeys_only(ages) # ['Peter', 'Isabel', 'Anna']" }, "tags": [ "object", @@ -873,7 +915,7 @@ ] }, "meta": { - "hash": "4e4a5b4892fcccb2982e7ae352f9d61fa0234c209431c9461ea47d503feaaf49" + "hash": "f8cf88c53dd53b702bc504a2d36f9c9bddbe3bfdc0c2946909b9fd6de2370122" } }, { @@ -884,8 +926,8 @@ "fileName": "last.md", "text": "Returns the last element in a list.\n\nuse `lst[-1]` to return the last element of the passed list.\n\n", "codeBlocks": { - "code": "```py\ndef last(lst):\n return lst[-1]\n```", - "example": "```py\nlast([1, 2, 3]) # 3\n```" + "code": "def last(lst):\r\n return lst[-1]", + "example": "last([1, 2, 3]) # 3" }, "tags": [ "list", @@ -893,7 +935,7 @@ ] }, "meta": { - "hash": "7fc339fbdb18e993fc93950b307fe2ec792c3f010f5dee0971d02288af16d67b" + "hash": "7cefc731b24f3979e93354686c1ca91adebd7cfadc8cd903cbb784730fcce0d3" } }, { @@ -904,8 +946,8 @@ "fileName": "lcm.md", "text": "Returns the least common multiple of two or more numbers.\n\nDefine a function, `spread`, that uses either `list.extend()` or `list.append()` on each element in a list to flatten it.\nUse `math.gcd()` and `lcm(x,y) = x * y / gcd(x,y)` to determine the least common multiple.\n\n", "codeBlocks": { - "code": "```py\nfrom functools import reduce\nimport math\n\ndef spread(arg):\n ret = []\n for i in arg:\n if isinstance(i, list):\n ret.extend(i)\n else:\n ret.append(i)\n return ret\n\ndef lcm(*args):\n numbers = []\n numbers.extend(spread(list(args)))\n\n def _lcm(x, y):\n return int(x * y / math.gcd(x, y))\n\n return reduce((lambda x, y: _lcm(x, y)), numbers)\n```", - "example": "```py\nlcm(12, 7) # 84\nlcm([1, 3, 4], 5) # 60\n```" + "code": "from functools import reduce\r\nimport math\r\n\r\ndef spread(arg):\r\n ret = []\r\n for i in arg:\r\n if isinstance(i, list):\r\n ret.extend(i)\r\n else:\r\n ret.append(i)\r\n return ret\r\n\r\ndef lcm(*args):\r\n numbers = []\r\n numbers.extend(spread(list(args)))\r\n\r\n def _lcm(x, y):\r\n return int(x * y / math.gcd(x, y))\r\n\r\n return reduce((lambda x, y: _lcm(x, y)), numbers)", + "example": "lcm(12, 7) # 84\r\nlcm([1, 3, 4], 5) # 60" }, "tags": [ "math", @@ -915,7 +957,7 @@ ] }, "meta": { - "hash": "a0cf166fd37f16c6bcaef46345ff11af0dadb488160168a0915e665550f7a669" + "hash": "0365d1bab90dcf808af1c31671e3bb712b4fa0a975e95fb97450a1db6eb8e7f2" } }, { @@ -926,8 +968,8 @@ "fileName": "longest_item.md", "text": "Takes any number of iterable objects or objects with a length property and returns the longest one. \nIf multiple objects have the same length, the first one will be returned.\n\nUse `max()` with `len` as the `key` to return the item with the greatest length.\n\n", "codeBlocks": { - "code": "```py\ndef longest_item(*args):\n return max(args, key = len)\n```", - "example": "```py\nlongest_item('this', 'is', 'a', 'testcase') # 'testcase'\nlongest_item([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]) # [1, 2, 3, 4, 5]\nlongest_item([1, 2, 3], 'foobar') # 'foobar'\n```" + "code": "def longest_item(*args):\r\n return max(args, key = len)", + "example": "longest_item('this', 'is', 'a', 'testcase') # 'testcase'\r\nlongest_item([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]) # [1, 2, 3, 4, 5]\r\nlongest_item([1, 2, 3], 'foobar') # 'foobar'" }, "tags": [ "list", @@ -937,7 +979,7 @@ ] }, "meta": { - "hash": "dd1a2d1300f23bb8f8618f9ab20dad11062f4476be80081e3cefc535717da818" + "hash": "fea8c001eb0d4a9c2bcd37b3e5fe5b636fec2fe0e937f1195bf1fec357d42e53" } }, { @@ -948,8 +990,8 @@ "fileName": "map_values.md", "text": "Creates an object with the same keys as the provided object and values generated by running the provided function for each value.\n\nUse `dict.keys()` to iterate over the object's keys, assigning the values produced by `fn` to each key of a new object.\n\n", "codeBlocks": { - "code": "```py\ndef map_values(obj, fn):\n ret = {}\n for key in obj.keys():\n ret[key] = fn(obj[key])\n return ret\n```", - "example": "```py\nusers = {\n 'fred': { 'user': 'fred', 'age': 40 },\n 'pebbles': { 'user': 'pebbles', 'age': 1 }\n}\n\nmap_values(users, lambda u : u['age']) # {'fred': 40, 'pebbles': 1}\n```" + "code": "def map_values(obj, fn):\r\n ret = {}\r\n for key in obj.keys():\r\n ret[key] = fn(obj[key])\r\n return ret", + "example": "users = {\r\n 'fred': { 'user': 'fred', 'age': 40 },\r\n 'pebbles': { 'user': 'pebbles', 'age': 1 }\r\n}\r\n\r\nmap_values(users, lambda u : u['age']) # {'fred': 40, 'pebbles': 1}" }, "tags": [ "object", @@ -958,7 +1000,7 @@ ] }, "meta": { - "hash": "91f93e124e2f1fb9d25b62db478f91503330947a7c212dc97215dec8c4b225fe" + "hash": "8478ad6fd73b9c7c15d7243b0be989ada0159c694471a520076f953ebf986258" } }, { @@ -967,10 +1009,10 @@ "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, convert to a `list` and use `max()` to return the maximum value.\n\n", "codeBlocks": { - "code": "```py\ndef max_by(lst, fn):\n return max(list(map(fn,lst)))\n```", - "example": "```py\nmax_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 8\n```" + "code": "def max_by(lst, fn):\r\n return max(list(map(fn,lst)))", + "example": "max_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 8" }, "tags": [ "math", @@ -980,7 +1022,7 @@ ] }, "meta": { - "hash": "b6758923ca701c9248e39ad2fd866c00af60dcdb670eb9845729b50bc2c59131" + "hash": "3538fe0c61f5ba76adc478a7a03603e227ae49209ff345f1a65e8163844e7024" } }, { @@ -991,8 +1033,8 @@ "fileName": "max_n.md", "text": "Returns the `n` maximum elements from the provided list. \nIf `n` is greater than or equal to the provided list's length, then return the original list (sorted in descending order).\n\nUse `sorted() to sort the list, `[:n]` to get the specified number of elements.\nOmit the second argument, `n`, to get a one-element list.\n\n", "codeBlocks": { - "code": "```py\ndef max_n(lst, n=1):\n return sorted(lst, reverse=True)[:n]\n```", - "example": "```py\nmax_n([1, 2, 3]) # [3]\nmax_n([1, 2, 3], 2) # [3,2]\n```" + "code": "def max_n(lst, n=1):\r\n return sorted(lst, reverse=True)[:n]", + "example": "max_n([1, 2, 3]) # [3]\r\nmax_n([1, 2, 3], 2) # [3,2]" }, "tags": [ "list", @@ -1001,7 +1043,7 @@ ] }, "meta": { - "hash": "572897e2101454c75554f182d31b02182f296be88e10d8b41c15c7ac1e74ce25" + "hash": "779eb91b49e8f15d476fa067c4892bac0c63c962afa82ab42e7ef416c4a3d5a2" } }, { @@ -1010,10 +1052,10 @@ "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, convert to a `list` and use `min()` to return the minimum value.\n\n", "codeBlocks": { - "code": "```py\ndef min_by(lst, fn):\n return min(list(map(fn,lst)))\n```", - "example": "```py\nmin_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 2\n```" + "code": "def min_by(lst, fn):\r\n return min(list(map(fn,lst)))", + "example": "min_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 2" }, "tags": [ "math", @@ -1023,7 +1065,7 @@ ] }, "meta": { - "hash": "9b63955e5c0c0723f55213a88f052e97a297f042b162dc4b6507a87317615c9c" + "hash": "72812142d33c4328a3c69764b1ddde2ba4a64e0a6c10107b38835e1ef9d0655a" } }, { @@ -1034,8 +1076,8 @@ "fileName": "min_n.md", "text": "Returns the `n` minimum elements from the provided list. \nIf `n` is greater than or equal to the provided list's length, then return the original list (sorted in ascending order).\n\nUse `sorted() to sort the list, `[:n]` to get the specified number of elements.\nOmit the second argument, `n`, to get a one-element list.\n\n", "codeBlocks": { - "code": "```py\ndef min_n(lst, n=1):\n return sorted(lst, reverse=False)[:n]\n```", - "example": "```py\nmin_n([1, 2, 3]) # [1]\nmin_n([1, 2, 3], 2) # [1,2]\n```" + "code": "def min_n(lst, n=1):\r\n return sorted(lst, reverse=False)[:n]", + "example": "min_n([1, 2, 3]) # [1]\r\nmin_n([1, 2, 3], 2) # [1,2]" }, "tags": [ "list", @@ -1044,7 +1086,7 @@ ] }, "meta": { - "hash": "c920b58f01c63346e65eab83a60c4635d4c104e41767b9d41ad94029e662b902" + "hash": "653535ca2eb51b2f792c529c4e8e96f99dcaceaf63b72c486c9b072fb5ea1a41" } }, { @@ -1055,8 +1097,8 @@ "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", "codeBlocks": { - "code": "```py\ndef none(lst, fn=lambda x: not not x):\n for el in lst:\n if fn(el):\n return False\n return True\n```", - "example": "```py\nnone([0, 1, 2, 0], lambda x: x >= 2 ) # False\nnone([0, 0, 0]) # True\n```" + "code": "def none(lst, fn=lambda x: not not x):\r\n for el in lst:\r\n if fn(el):\r\n return False\r\n return True", + "example": "none([0, 1, 2, 0], lambda x: x >= 2 ) # False\r\nnone([0, 0, 0]) # True" }, "tags": [ "list", @@ -1065,7 +1107,7 @@ ] }, "meta": { - "hash": "2f345a768997fb0523dfbb775281ca0338a849032b5d8671d313c08f46f9b26e" + "hash": "50d683ab59844b8de41fee0fb2ed3267e68c5c3020265b51b2f49f75bc3bd85e" } }, { @@ -1076,8 +1118,8 @@ "fileName": "offset.md", "text": "Moves the specified amount of elements to the end of the list.\n\nUse `lst[offset:]` and `lst[:offset]` to get the two slices of the list and combine them before returning.\n\nExplain briefly how the snippet works.\n\n", "codeBlocks": { - "code": "```py\ndef offset(lst, offset):\n return lst[offset:] + lst[:offset]\n```", - "example": "```py\noffset([1, 2, 3, 4, 5], 2) # [3, 4, 5, 1, 2]\noffset([1, 2, 3, 4, 5], -2) # [4, 5, 1, 2, 3]\n```" + "code": "def offset(lst, offset):\r\n return lst[offset:] + lst[:offset]", + "example": "offset([1, 2, 3, 4, 5], 2) # [3, 4, 5, 1, 2]\r\noffset([1, 2, 3, 4, 5], -2) # [4, 5, 1, 2, 3]" }, "tags": [ "list", @@ -1085,7 +1127,7 @@ ] }, "meta": { - "hash": "86c03a0d8ea80a203414f79670fec491b232acd5448108c1858058df68833f42" + "hash": "e86853e988f5c40cd8ef95ef80da49fe9e40626ea754db4a83f5b68d9713afe9" } }, { @@ -1096,8 +1138,8 @@ "fileName": "palindrome.md", "text": "Returns `True` if the given string is a palindrome, `False` otherwise.\n\nUse `str.lower()` and `re.sub()` to convert to lowercase and remove non-alphanumeric characters from the given string. \nThen, compare the new string with its reverse.\n\n", "codeBlocks": { - "code": "```py\nfrom re import sub\n\ndef palindrome(string):\n s = sub('[\\W_]', '', string.lower())\n return s == s[::-1]\n```", - "example": "```py\npalindrome('taco cat') # True\n```" + "code": "from re import sub\r\n\r\ndef palindrome(string):\r\n s = sub('[\\W_]', '', string.lower())\r\n return s == s[::-1]", + "example": "palindrome('taco cat') # True" }, "tags": [ "string", @@ -1105,7 +1147,7 @@ ] }, "meta": { - "hash": "e707d6b2f27bcc3dda322b114199b2b22ea916871b1c657c43648ecb5b21240b" + "hash": "2da6734174e371587baeebba432bc0f441f024211e900c4852fca7eeb08f89df" } }, { @@ -1116,8 +1158,8 @@ "fileName": "rads_to_degrees.md", "text": "Converts an angle from radians to degrees.\n\nUse `math.pi` and the radian to degree formula to convert the angle from radians to degrees.\n\n", "codeBlocks": { - "code": "```py\nimport math\n\ndef rads_to_degrees(rad):\n return (rad * 180.0) / math.pi\n```", - "example": "```py\nimport math\nrads_to_degrees(math.pi / 2) # 90.0\n```" + "code": "import math\r\n\r\ndef rads_to_degrees(rad):\r\n return (rad * 180.0) / math.pi", + "example": "import math\r\nrads_to_degrees(math.pi / 2) # 90.0" }, "tags": [ "math", @@ -1125,7 +1167,7 @@ ] }, "meta": { - "hash": "c3008a44f2eb31adaca3e4f91c29a56a955e3105f9c187aa055fc55c35874b41" + "hash": "b5eac6740b6b78892d5978c26e44dc2747898c179f6d1e2c6f181f716ed4ee97" } }, { @@ -1136,8 +1178,8 @@ "fileName": "sample.md", "text": "Returns a random element from an array.\n\nUse `randint()` to generate a random number that corresponds to an index in the list, return the element at that index.\n\n", "codeBlocks": { - "code": "```py\nfrom random import randint\n\ndef sample(lst):\n return lst[randint(0, len(lst) - 1)]\n```", - "example": "```py\nsample([3, 7, 9, 11]) # 9\n```" + "code": "from random import randint\r\n\r\ndef sample(lst):\r\n return lst[randint(0, len(lst) - 1)]", + "example": "sample([3, 7, 9, 11]) # 9" }, "tags": [ "list", @@ -1146,7 +1188,7 @@ ] }, "meta": { - "hash": "68c3a2f20c4969324199dc6005be573d95ec4d08747586bf26d0bf06b571c94c" + "hash": "d8cf3ead7fde16fd0886baa5469ef55844848d25629c4fff011d1e27496b75ad" } }, { @@ -1157,8 +1199,8 @@ "fileName": "shuffle.md", "text": "Randomizes the order of the values of an list, returning a new list.\n\nUses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) to reorder the elements of the list.\n\n", "codeBlocks": { - "code": "```py\nfrom copy import deepcopy\nfrom random import randint\n\ndef shuffle(lst):\n temp_lst = deepcopy(lst)\n m = len(temp_lst)\n while (m):\n m -= 1\n i = randint(0, m)\n temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]\n return temp_lst\n```", - "example": "```py\nfoo = [1,2,3]\nshuffle(foo) # [2,3,1] , foo = [1,2,3]\n```" + "code": "from copy import deepcopy\r\nfrom random import randint\r\n\r\ndef shuffle(lst):\r\n temp_lst = deepcopy(lst)\r\n m = len(temp_lst)\r\n while (m):\r\n m -= 1\r\n i = randint(0, m)\r\n temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]\r\n return temp_lst", + "example": "foo = [1,2,3]\r\nshuffle(foo) # [2,3,1] , foo = [1,2,3]" }, "tags": [ "list", @@ -1167,7 +1209,7 @@ ] }, "meta": { - "hash": "8c6e1dafadd78f04b11d412b7b8db01e1275339adf906e2637129d761117e480" + "hash": "e50c5b8daa558ff5ea7f2516a3e382f4e91bcf8ab5999041376020080dd916c0" } }, { @@ -1178,8 +1220,8 @@ "fileName": "similarity.md", "text": "Returns a list of elements that exist in both lists.\n\nUse list comprehension on `a` to only keep values contained in both lists.\n\n", "codeBlocks": { - "code": "```py\ndef similarity(a, b):\n return [item for item in a if item in b]\n```", - "example": "```py\nsimilarity([1, 2, 3], [1, 2, 4]) # [1, 2]\n```" + "code": "def similarity(a, b):\r\n return [item for item in a if item in b]", + "example": "similarity([1, 2, 3], [1, 2, 4]) # [1, 2]" }, "tags": [ "list", @@ -1187,7 +1229,28 @@ ] }, "meta": { - "hash": "51e5a4fc0b3376f24804b51f71c3daed109b1e60535aa39bca44c1918fedaacf" + "hash": "54faf1a5edfa2e6c1508635fa2492ad22978ac988b250acedd2cdb64670fc019" + } + }, + { + "id": "snake", + "title": "snake", + "type": "snippet", + "attributes": { + "fileName": "snake.md", + "text": "Converts a string to snake case.\n\nBreak the string into words and combine them adding `_-_` as a separator, using a regexp.\n\n", + "codeBlocks": { + "code": "import re\r\n\r\ndef snake(str):\r\n return re.sub(r\"(\\s|_|-)+\",\"-\",\r\n re.sub(r\"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+\",\r\n lambda mo: mo.group(0).lower(),str)\r\n )", + "example": "snake('camelCase'); # 'camel_case'\r\nsnake('some text'); # 'some_text'\r\nsnake('some-mixed_string With spaces_underscores-and-hyphens'); # 'some_mixed_string_with_spaces_underscores_and_hyphens'\r\nsnake('AllThe-small Things'); # \"all_the_smal_things\"" + }, + "tags": [ + "string", + "regexp", + "intermediate" + ] + }, + "meta": { + "hash": "c2a90128c4edf395cf659da0b2ad9ddccbf13eb19c900d4a8734bc98613096d8" } }, { @@ -1198,8 +1261,8 @@ "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", "codeBlocks": { - "code": "```py\ndef some(lst, fn=lambda x: not not x):\n for el in lst:\n if fn(el):\n return True\n return False\n```", - "example": "```py\nsome([0, 1, 2, 0], lambda x: x >= 2 ) # True\nsome([0, 0, 1, 0]) # True\n```" + "code": "def some(lst, fn=lambda x: not not x):\r\n for el in lst:\r\n if fn(el):\r\n return True\r\n return False", + "example": "some([0, 1, 2, 0], lambda x: x >= 2 ) # True\r\nsome([0, 0, 1, 0]) # True" }, "tags": [ "list", @@ -1208,7 +1271,7 @@ ] }, "meta": { - "hash": "5052bf5e5a38a8a928ea6b3e42cecda2027d2123dd5e47764b354eafd3310dee" + "hash": "aada6496416fff036ff40ae4a8555d4bc27d56a0ce869d0d4366c410beada2bc" } }, { @@ -1219,8 +1282,8 @@ "fileName": "split_lines.md", "text": "Splits a multiline string into a list of lines.\n\nUse `str.split()` and `'\\n'` to match line breaks and create a list.\n\n", "codeBlocks": { - "code": "```py\ndef split_lines(str):\n str.split('\\n')\n```", - "example": "```py\nsplit_lines('This\\nis a\\nmultiline\\nstring.\\n') # 'This\\nis a\\nmultiline\\nstring.\\n'\n```" + "code": "def split_lines(str):\r\n str.split('\\n')", + "example": "split_lines('This\\nis a\\nmultiline\\nstring.\\n') # 'This\\nis a\\nmultiline\\nstring.\\n'" }, "tags": [ "string", @@ -1228,7 +1291,7 @@ ] }, "meta": { - "hash": "9c8d9abb223e26561b4278e5b29c373a9196297143a504d353c7fbcd8c5102b2" + "hash": "aa12646b6b9629fe38eb7b04bdc5a9fbff64b012d5b65fcac1e8d0a42a14fd05" } }, { @@ -1239,8 +1302,8 @@ "fileName": "spread.md", "text": "Flattens a list, by spreading its elements into a new list.\n\nLoop over elements, use `list.extend()` if the element is a list, `list.append()` otherwise.\n\n", "codeBlocks": { - "code": "```py\ndef spread(arg):\n ret = []\n for i in arg:\n if isinstance(i, list):\n ret.extend(i)\n else:\n ret.append(i)\n return ret\n```", - "example": "```py\nspread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]\n```" + "code": "def spread(arg):\r\n ret = []\r\n for i in arg:\r\n if isinstance(i, list):\r\n ret.extend(i)\r\n else:\r\n ret.append(i)\r\n return ret", + "example": "spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]" }, "tags": [ "list", @@ -1249,7 +1312,70 @@ ] }, "meta": { - "hash": "db09a105c21df08d2d580e95dd9d3f895a38e2e9b29818f78a671203ab94df38" + "hash": "5e9cf0033df73310e23800b6d41677a8607342717d23ef661f17f559cae7a8d8" + } + }, + { + "id": "sum_by", + "title": "sum_by", + "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", + "codeBlocks": { + "code": "def sum_by(lst, fn):\r\n return sum(list(map(fn,lst)))", + "example": "sum_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda v : v['n']) # 20" + }, + "tags": [ + "math", + "list", + "function", + "beginner" + ] + }, + "meta": { + "hash": "d97b4f2c69fafe781e6a0a72f2e41a2041ae960b2cb078b40a89ebba7464adab" + } + }, + { + "id": "symmetric_difference", + "title": "symmetric_difference", + "type": "snippet", + "attributes": { + "fileName": "symmetric_difference.md", + "text": "Returns the symmetric difference between two iterables, without filtering out duplicate values.\n\nCreate a `set` from each list, then use list comprehension on each one to only keep values not contained in the previously created set of the other.\n\n", + "codeBlocks": { + "code": "def symmetric_difference(a, b):\r\n _a, _b = set(a), set(b)\r\n return [item for item in a if item not in _b] + [item for item in b if item not in _a]", + "example": "symmetric_difference([1, 2, 3], [1, 2, 4]) # [3, 4]" + }, + "tags": [ + "list", + "beginner" + ] + }, + "meta": { + "hash": "b1b608eac070e1c51ded6494ba0a5291e3f2afaced5ea026201dac0a1ebdbe73" + } + }, + { + "id": "symmetric_difference_by", + "title": "symmetric_difference_by", + "type": "snippet", + "attributes": { + "fileName": "symmetric_difference_by.md", + "text": "Returns the symmetric difference between two lists, after applying the provided function to each list element of both.\n\nCreate a `set` by applying `fn` to each element in every list, then use list comprehension in combination with `fn` on each one to only keep values not contained in the previously created set of the other.\n\n", + "codeBlocks": { + "code": "def symmetric_difference_by(a, b, fn):\r\n _a, _b = set(map(fn, a)), set(map(fn, b))\r\n return [item for item in a if fn(item) not in _b] + [item for item in b if fn(item) not in _a]", + "example": "from math import floor\r\nsymmetric_difference_by([2.1, 1.2], [2.3, 3.4],floor) # [1.2, 3.4]" + }, + "tags": [ + "list", + "function", + "intermediate" + ] + }, + "meta": { + "hash": "d6b4f42c3d08fee38e8674c6059e01deffeb74d943e9d89e0836a39fe19309cc" } }, { @@ -1260,8 +1386,8 @@ "fileName": "tail.md", "text": "Returns all elements in a list except for the first one.\n\nReturn `lst[1:]` if the list's length is more than `1`, otherwise, return the whole list.\n\n", "codeBlocks": { - "code": "```py\ndef tail(lst):\n return lst[1:] if len(lst) > 1 else lst\n```", - "example": "```py\ntail([1, 2, 3]); # [2,3]\ntail([1]); # [1]\n```" + "code": "def tail(lst):\r\n return lst[1:] if len(lst) > 1 else lst", + "example": "tail([1, 2, 3]); # [2,3]\r\ntail([1]); # [1]" }, "tags": [ "list", @@ -1269,7 +1395,48 @@ ] }, "meta": { - "hash": "f06acf219bcdfbacb9e86b01f91c7d68f6f8fa8a5a043c37cf33de4969c62eae" + "hash": "819f20907ca5f906b185988f32e06c1c39e9a39786dac76a3421c55c7f5327b1" + } + }, + { + "id": "union", + "title": "union", + "type": "snippet", + "attributes": { + "fileName": "union.md", + "text": "Returns every element that exists in any of the two lists once.\n\nCreate a `set` with all values of `a` and `b` and convert to a `list`.\n\n", + "codeBlocks": { + "code": "def union(a,b):\r\n return list(set(a + b))", + "example": "union([1, 2, 3], [4, 3, 2]) # [1,2,3,4]" + }, + "tags": [ + "list", + "beginner" + ] + }, + "meta": { + "hash": "cf3bd4a569cce91769a1e4781a4317d93a023ec04658389532b00572fc3d2b7a" + } + }, + { + "id": "union_by", + "title": "union_by", + "type": "snippet", + "attributes": { + "fileName": "union_by.md", + "text": "Returns every element that exists in any of the two lists once, after applying the provided function to each element of both.\n\nCreate a `set` by applying `fn` to each element in `a`, then use list comprehension in combination with `fn` on `b` to only keep values not contained in the previously created set, `_a`.\nFinally, create a `set` from the previous result and `a` and transform it into a `list`\n\n", + "codeBlocks": { + "code": "def union_by(a,b,fn):\r\n _a = set(map(fn, a))\r\n return list(set(a + [item for item in b if fn(item) not in _a]))", + "example": "from math import floor\r\nunion_by([2.1], [1.2, 2.3], floor) # [2.1, 1.2]" + }, + "tags": [ + "list", + "function", + "intermediate" + ] + }, + "meta": { + "hash": "0da21f57e2c6eb4efedcbcf555b00843c03df3f5c505de421a4b4d226a1f081e" } }, { @@ -1280,8 +1447,8 @@ "fileName": "unique_elements.md", "text": "Returns the unique elements in a given list.\n\nCreate a `set` from the list to discard duplicated values, then return a `list` from it.\n\n", "codeBlocks": { - "code": "```py\ndef unique_elements(li):\n return list(set(li))\n```", - "example": "```py\nunique_elements([1, 2, 2, 3, 4, 3]) # [1, 2, 3, 4]\n```" + "code": "def unique_elements(li):\r\n return list(set(li))", + "example": "unique_elements([1, 2, 2, 3, 4, 3]) # [1, 2, 3, 4]" }, "tags": [ "list", @@ -1289,7 +1456,7 @@ ] }, "meta": { - "hash": "1f28461c8bbf4ce0195c756b2f013e519a43fbdef8b6a3640eafab2a6cb20bc9" + "hash": "3a1e3b5eb2b223a3b7b8cdcf96b961724d72ddd7d9e276f9fcd730572bd1bba2" } }, { @@ -1300,8 +1467,8 @@ "fileName": "values_only.md", "text": "Returns a flat list of all the values in a flat dictionary.\n\nUse `dict.values()` to return the values in the given dictionary.\nReturn a `list()` of the previous result.\n\n", "codeBlocks": { - "code": "```py\ndef values_only(dict):\n return list(flat_dict.values())\n```", - "example": "```py\nages = {\n \"Peter\": 10,\n \"Isabel\": 11,\n \"Anna\": 9,\n}\nvalues_only(ages) # [10, 11, 9]\n```" + "code": "def values_only(dict):\r\n return list(flat_dict.values())", + "example": "ages = {\r\n \"Peter\": 10,\r\n \"Isabel\": 11,\r\n \"Anna\": 9,\r\n}\r\nvalues_only(ages) # [10, 11, 9]" }, "tags": [ "object", @@ -1310,7 +1477,7 @@ ] }, "meta": { - "hash": "15dec93f30e55d25c07f9d5456d5e96f9b6a5a794d9a0874a39be0359a42c10f" + "hash": "be7662f58cb7e4a29f719939f58214192107a86d764cd2ef9fbf583ee6dc5f59" } }, { @@ -1321,8 +1488,8 @@ "fileName": "zip.md", "text": "Creates a list of elements, grouped based on the position in the original lists.\n\n\nUse `max` combined with `list comprehension` to get the length of the longest list in the arguments. \nLoop for `max_length` times grouping elements. \nIf lengths of `lists` vary, use `fill_value` (defaults to `None`). \n\n", "codeBlocks": { - "code": "```py\ndef zip(*args, fillvalue=None):\n max_length = max([len(lst) for lst in args])\n result = []\n for i in range(max_length):\n result.append([\n args[k][i] if i < len(args[k]) else fillvalue for k in range(len(args))\n ])\n return result\n```", - "example": "```py\nzip(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]]\nzip(['a'], [1, 2], [True, False]) # [['a', 1, True], [None, 2, False]]\nzip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]]\n```" + "code": "def zip(*args, fillvalue=None):\r\n max_length = max([len(lst) for lst in args])\r\n result = []\r\n for i in range(max_length):\r\n result.append([\r\n args[k][i] if i < len(args[k]) else fillvalue for k in range(len(args))\r\n ])\r\n return result", + "example": "zip(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]]\r\nzip(['a'], [1, 2], [True, False]) # [['a', 1, True], [None, 2, False]]\r\nzip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]]" }, "tags": [ "list", @@ -1331,7 +1498,7 @@ ] }, "meta": { - "hash": "8c0dea46f260c7edfa363a0a295683d858c2011c9220b2d4fc53025a684388f0" + "hash": "44281b36c4ae79373b6266790741f21225e47c989e3bbfe232b939f5b69270c6" } } ], diff --git a/src/static-parts/README-end.md b/src/static-parts/README-end.md index 9f9c73317..0c8f3d52c 100644 --- a/src/static-parts/README-end.md +++ b/src/static-parts/README-end.md @@ -5,5 +5,4 @@ ## Credits -*Icons made by [Smashicons](https://www.flaticon.com/authors/smashicons) from [www.flaticon.com](https://www.flaticon.com/) is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/).* *This README is built using [markdown-builder](https://github.com/30-seconds/markdown-builder).*