hidden examples
This commit is contained in:
159
README.md
159
README.md
@ -44,7 +44,7 @@ Chunks an array into smaller lists of a specified size.
|
|||||||
|
|
||||||
Uses `range` to create a list of desired size. Then use `map` on this list and fill it with splices of `arr`.
|
Uses `range` to create a list of desired size. Then use `map` on this list and fill it with splices of `arr`.
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
from math import ceil
|
from math import ceil
|
||||||
@ -55,29 +55,40 @@ def chunk(arr, size):
|
|||||||
map(lambda x: arr[x * size:x * size + size],
|
map(lambda x: arr[x * size:x * size + size],
|
||||||
list(range(0, ceil(len(arr) / size)))))
|
list(range(0, ceil(len(arr) / size)))))
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
``` python
|
|
||||||
chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]
|
chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]
|
||||||
```
|
|
||||||
|
|
||||||
|
```
|
||||||
|
<details>
|
||||||
### compact
|
### compact
|
||||||
|
|
||||||
Removes falsey values from a list.
|
Removes falsey values from a list.
|
||||||
|
|
||||||
Use `filter()` to filter out falsey values (False, None, 0, and "").
|
Use `filter()` to filter out falsey values (False, None, 0, and "").
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
def compact(arr):
|
def compact(arr):
|
||||||
return list(filter(lambda x: bool(x), arr))
|
return list(filter(lambda x: bool(x), arr))
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
``` python
|
|
||||||
compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]
|
compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
### count_occurences
|
### count_occurences
|
||||||
|
|
||||||
:information_source: Already implemented via `list.count()`.
|
:information_source: Already implemented via `list.count()`.
|
||||||
@ -86,7 +97,7 @@ Counts the occurrences of a value in an list.
|
|||||||
|
|
||||||
Uses the `reduce` functin from built-in module `functools` to increment a counter each time you encounter the specific value inside the list.
|
Uses the `reduce` functin from built-in module `functools` to increment a counter each time you encounter the specific value inside the list.
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
def count_occurences(arr, val):
|
def count_occurences(arr, val):
|
||||||
@ -94,18 +105,24 @@ def count_occurences(arr, val):
|
|||||||
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
|
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
|
||||||
arr)
|
arr)
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
```python
|
|
||||||
count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3
|
count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
### deep_flatten
|
### deep_flatten
|
||||||
|
|
||||||
Deep flattens a list.
|
Deep flattens a list.
|
||||||
|
|
||||||
Use recursion. Use `list.extend()` with an empty array (`result`) and the spread function to flatten a list. Recursively flatten each element that is a list.
|
Use recursion. Use `list.extend()` with an empty array (`result`) and the spread function to flatten a list. Recursively flatten each element that is a list.
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
def spread(arg):
|
def spread(arg):
|
||||||
@ -124,29 +141,40 @@ def deep_flatten(arr):
|
|||||||
spread(list(map(lambda x: deep(x) if type(x) == list else x, arr))))
|
spread(list(map(lambda x: deep(x) if type(x) == list else x, arr))))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
```python
|
|
||||||
deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]
|
deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]
|
||||||
```
|
|
||||||
|
|
||||||
|
```
|
||||||
|
<details>
|
||||||
### difference
|
### difference
|
||||||
|
|
||||||
Returns the difference between two arrays.
|
Returns the difference between two arrays.
|
||||||
|
|
||||||
Create a `set` from `b`, then use list comprehension to only keep values not contained in `b`
|
Create a `set` from `b`, then use list comprehension to only keep values not contained in `b`
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
def difference(a, b):
|
def difference(a, b):
|
||||||
b = set(b)
|
b = set(b)
|
||||||
return [item for item in a if item not in b]
|
return [item for item in a if item not in b]
|
||||||
|
|
||||||
```
|
|
||||||
``` python
|
```
|
||||||
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
difference([1, 2, 3], [1, 2, 4]) # [3]
|
difference([1, 2, 3], [1, 2, 4]) # [3]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
### shuffle
|
### shuffle
|
||||||
|
|
||||||
:information_source: The same algorithm is already implemented via `random.shuffle`.
|
:information_source: The same algorithm is already implemented via `random.shuffle`.
|
||||||
@ -155,7 +183,7 @@ 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.
|
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 copy import deepcopy
|
||||||
@ -171,17 +199,23 @@ def shuffle(arr):
|
|||||||
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
|
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
|
||||||
return temp_arr
|
return temp_arr
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
``` python
|
|
||||||
foo = [1,2,3]
|
foo = [1,2,3]
|
||||||
shuffle(foo) # [2,3,1] , foo = [1,2,3]
|
shuffle(foo) # [2,3,1] , foo = [1,2,3]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
### spread
|
### spread
|
||||||
|
|
||||||
Implements javascript's spread syntax as a function. Flattens the list(non-deep) and returns an list.
|
Implements javascript's spread syntax as a function. Flattens the list(non-deep) and returns an list.
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
def spread(arg):
|
def spread(arg):
|
||||||
@ -193,12 +227,18 @@ def spread(arg):
|
|||||||
ret.append(i)
|
ret.append(i)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
```python
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]
|
spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
### zip
|
### zip
|
||||||
|
|
||||||
:information_source: Already implemented via `itertools.zip_longest()`
|
:information_source: Already implemented via `itertools.zip_longest()`
|
||||||
@ -207,7 +247,7 @@ Creates a list of elements, grouped based on the position in the original lists.
|
|||||||
|
|
||||||
Use `max` combined with `list comprehension` to get the length of the longest list in the arguments. Loops for `max_length` times grouping elements. If lengths of `lists` vary `fill_value` is used. By default `fill_value` is `None`.
|
Use `max` combined with `list comprehension` to get the length of the longest list in the arguments. Loops for `max_length` times grouping elements. If lengths of `lists` vary `fill_value` is used. By default `fill_value` is `None`.
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
def zip(*args, fillvalue=None):
|
def zip(*args, fillvalue=None):
|
||||||
@ -219,13 +259,19 @@ def zip(*args, fillvalue=None):
|
|||||||
])
|
])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
``` python
|
|
||||||
zip(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]]
|
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]) # [['a', 1, True], [None, 2, False]]
|
||||||
zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]]
|
zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
## :scroll: String
|
## :scroll: String
|
||||||
|
|
||||||
### count_vowels
|
### count_vowels
|
||||||
@ -234,7 +280,7 @@ Retuns `number` of vowels in provided `string`.
|
|||||||
|
|
||||||
Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a string.
|
Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a string.
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
import re
|
import re
|
||||||
@ -243,13 +289,18 @@ import re
|
|||||||
def count_vowels(str):
|
def count_vowels(str):
|
||||||
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))
|
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
``` python
|
|
||||||
count_vowels('foobar') # 3
|
count_vowels('foobar') # 3
|
||||||
count_vowels('gym') # 0
|
count_vowels('gym') # 0
|
||||||
```
|
|
||||||
|
|
||||||
|
```
|
||||||
|
<details>
|
||||||
## :heavy_division_sign: Math
|
## :heavy_division_sign: Math
|
||||||
|
|
||||||
### gcd
|
### gcd
|
||||||
@ -262,7 +313,7 @@ The `helperGcdfunction` uses recursion. Base case is when `y` equals `0`. In thi
|
|||||||
|
|
||||||
Uses the reduce function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists.
|
Uses the reduce function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists.
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
@ -287,12 +338,18 @@ def gcd(*args):
|
|||||||
|
|
||||||
return reduce((lambda x, y: _gcd(x, y)), numbers)
|
return reduce((lambda x, y: _gcd(x, y)), numbers)
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
``` python
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
gcd(8,36) # 4
|
gcd(8,36) # 4
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
### lcm
|
### lcm
|
||||||
|
|
||||||
Returns the least common multiple of two or more numbers.
|
Returns the least common multiple of two or more numbers.
|
||||||
@ -301,7 +358,7 @@ Use the `greatest common divisor (GCD)` formula and the fact that `lcm(x,y) = x
|
|||||||
|
|
||||||
Uses `reduce` function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists.
|
Uses `reduce` function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists.
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
@ -329,20 +386,26 @@ def lcm(*args):
|
|||||||
|
|
||||||
return reduce((lambda x, y: _lcm(x, y)), numbers)
|
return reduce((lambda x, y: _lcm(x, y)), numbers)
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
``` python
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
lcm(12, 7) # 84
|
lcm(12, 7) # 84
|
||||||
lcm([1, 3, 4], 5) # 60
|
lcm([1, 3, 4], 5) # 60
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
### max_n
|
### max_n
|
||||||
|
|
||||||
Returns the `n` maximum elements from the provided list. If `n` is greater than or equal to the provided list's length, then return the original list(sorted in descending order).
|
Returns the `n` maximum elements from the provided list. If `n` is greater than or equal to the provided list's length, then return the original list(sorted in descending order).
|
||||||
|
|
||||||
Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order and then use `list.reverse()` reverse it to make it descending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element array
|
Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order and then use `list.reverse()` reverse it to make it descending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element array
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
@ -354,19 +417,25 @@ def max_n(arr, n=1):
|
|||||||
numbers.reverse()
|
numbers.reverse()
|
||||||
return numbers[:n]
|
return numbers[:n]
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
```python
|
|
||||||
max_n([1, 2, 3]) # [3]
|
max_n([1, 2, 3]) # [3]
|
||||||
max_n([1, 2, 3], 2) # [3,2]
|
max_n([1, 2, 3], 2) # [3,2]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
### min_n
|
### min_n
|
||||||
|
|
||||||
Returns the `n` minimum elements from the provided list. If `n` is greater than or equal to the provided list's length, then return the original list(sorted in ascending order).
|
Returns the `n` minimum elements from the provided list. If `n` is greater than or equal to the provided list's length, then return the original list(sorted in ascending order).
|
||||||
|
|
||||||
Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element array
|
Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element array
|
||||||
|
|
||||||
```python
|
```py
|
||||||
|
|
||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
@ -377,12 +446,18 @@ def min_n(arr, n=1):
|
|||||||
numbers.sort()
|
numbers.sort()
|
||||||
return numbers[:n]
|
return numbers[:n]
|
||||||
|
|
||||||
```
|
|
||||||
|
```
|
||||||
|
|
||||||
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
```python
|
|
||||||
min_n([1, 2, 3]) # [1]
|
min_n([1, 2, 3]) # [1]
|
||||||
min_n([1, 2, 3], 2) # [1,2]
|
min_n([1, 2, 3], 2) # [1,2]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ for file in files:
|
|||||||
fileData = someFile.read()
|
fileData = someFile.read()
|
||||||
someFile.close()
|
someFile.close()
|
||||||
originalCode = re.search(codeRe,fileData).group(0)
|
originalCode = re.search(codeRe,fileData).group(0)
|
||||||
|
print(re.split(codeRe,fileData)[0])
|
||||||
formatedCode = autopep8.fix_code(re.split(codeRe,fileData)[1])
|
formatedCode = autopep8.fix_code(re.split(codeRe,fileData)[1])
|
||||||
fileToSave = fileData.replace(originalCode,('```python \n'+formatedCode+'\n```'))
|
fileToSave = fileData.replace(originalCode,('```python \n'+formatedCode+'\n```'))
|
||||||
someFile = open("snippets/"+file,'w')
|
someFile = open("snippets/"+file,'w')
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
codeRe = "```\s*python([\s\S]*?)```"
|
||||||
def title_case(str):
|
def title_case(str):
|
||||||
return str[:1].upper() + str[1:].lower()
|
return str[:1].upper() + str[1:].lower()
|
||||||
EMOJIS = {
|
EMOJIS = {
|
||||||
@ -46,5 +48,6 @@ for category in tag_dict:
|
|||||||
for snippet in tag_dict[category]:
|
for snippet in tag_dict[category]:
|
||||||
someFile = open("snippets/" + snippet + '.md')
|
someFile = open("snippets/" + snippet + '.md')
|
||||||
fileData = someFile.read()
|
fileData = someFile.read()
|
||||||
toAppend += fileData + '\n'
|
codeParts = re.split(codeRe,fileData)
|
||||||
|
toAppend += codeParts[0] + f'```py{codeParts[1]} \n ```' +codeParts[2] + f'<details><summary>View Examples</summary>\n\n```py\n{codeParts[3]}\n```\n<details>' + '\n'
|
||||||
open("README.md",'w').write(start+toAppend+'\n'+end)
|
open("README.md",'w').write(start+toAppend+'\n'+end)
|
||||||
|
|||||||
Reference in New Issue
Block a user