Travis build: 910
This commit is contained in:
33
README.md
33
README.md
@ -1,6 +1,6 @@
|
|||||||

|

|
||||||
|
|
||||||
# 30-seconds-of-python-code
|
# 30-seconds-of-python-code [](http://www.twitter.com/share?text=%2330secondsofcode+30-seconds-of-python-code+-+Python+Implementation+of+30+seconds+of+code%0Ahttps://github.com/kriadmin/30-seconds-of-python-code&url=a")
|
||||||
[](https://github.com/kriadmin/30-seconds-of-python-code/blob/master/LICENSE) [](https://gitter.im/30-seconds-of-python-code/Lobby) [](http://makeapullrequest.com) [](https://travis-ci.org/kriadmin/30-seconds-of-python-code) [](https://insight.io/github.com/kriadmin/30-seconds-of-python-code/tree/master/?source=0) [](https://github.com/Flet/semistandard)
|
[](https://github.com/kriadmin/30-seconds-of-python-code/blob/master/LICENSE) [](https://gitter.im/30-seconds-of-python-code/Lobby) [](http://makeapullrequest.com) [](https://travis-ci.org/kriadmin/30-seconds-of-python-code) [](https://insight.io/github.com/kriadmin/30-seconds-of-python-code/tree/master/?source=0) [](https://github.com/Flet/semistandard)
|
||||||
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fkriadmin%2F30-seconds-of-python-code?ref=badge_shield)
|
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fkriadmin%2F30-seconds-of-python-code?ref=badge_shield)
|
||||||
|
|
||||||
@ -52,6 +52,7 @@
|
|||||||
|
|
||||||
### chunk
|
### chunk
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Chunks an array into smaller lists of a specified size.
|
Chunks an array into smaller lists of a specified size.
|
||||||
@ -77,6 +78,7 @@ chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]
|
|||||||
|
|
||||||
### compact
|
### compact
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Removes falsey values from a list.
|
Removes falsey values from a list.
|
||||||
@ -97,6 +99,7 @@ compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]
|
|||||||
|
|
||||||
### count_by
|
### count_by
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
:information_source: Already implemented via `collections.Counter`
|
:information_source: Already implemented via `collections.Counter`
|
||||||
@ -108,7 +111,7 @@ Use `map()` to map the values of the list using the given function. Iterate over
|
|||||||
def count_by(arr, fn=lambda x: x):
|
def count_by(arr, fn=lambda x: x):
|
||||||
key = {}
|
key = {}
|
||||||
for el in map(fn, arr):
|
for el in map(fn, arr):
|
||||||
key[el] = 0 if not el in key else key[el]
|
key[el] = 0 if el not in key else key[el]
|
||||||
key[el] += 1
|
key[el] += 1
|
||||||
return key
|
return key
|
||||||
```
|
```
|
||||||
@ -125,6 +128,7 @@ count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}
|
|||||||
|
|
||||||
### count_occurences
|
### count_occurences
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
:information_source: Already implemented via `list.count()`.
|
:information_source: Already implemented via `list.count()`.
|
||||||
@ -152,6 +156,7 @@ count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3
|
|||||||
|
|
||||||
### deep_flatten
|
### deep_flatten
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [Meet Zaveri](https://www.github.com/meetzaveri)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [Meet Zaveri](https://www.github.com/meetzaveri)
|
||||||
|
|
||||||
Deep flattens a list.
|
Deep flattens a list.
|
||||||
@ -185,6 +190,7 @@ deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]
|
|||||||
|
|
||||||
### difference
|
### difference
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Returns the difference between two arrays.
|
Returns the difference between two arrays.
|
||||||
@ -206,6 +212,7 @@ difference([1, 2, 3], [1, 2, 4]) # [3]
|
|||||||
|
|
||||||
### difference_by
|
### difference_by
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Returns the difference between two list, after applying the provided function to each list element of both.
|
Returns the difference between two list, after applying the provided function to each list element of both.
|
||||||
@ -229,6 +236,7 @@ difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x
|
|||||||
|
|
||||||
### insertion_sort
|
### insertion_sort
|
||||||
<span style="color:grey">Author:-</span> [Meet Zaveri](https://www.github.com/meetzaveri)
|
<span style="color:grey">Author:-</span> [Meet Zaveri](https://www.github.com/meetzaveri)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Meet Zaveri](https://www.github.com/meetzaveri), [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Meet Zaveri](https://www.github.com/meetzaveri), [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
On a very basic level, an insertion sort algorithm contains the logic of shifting around and inserting elements in order to sort an unordered list of any size. The way that it goes about inserting elements, however, is what makes insertion sort so very interesting!
|
On a very basic level, an insertion sort algorithm contains the logic of shifting around and inserting elements in order to sort an unordered list of any size. The way that it goes about inserting elements, however, is what makes insertion sort so very interesting!
|
||||||
@ -256,6 +264,7 @@ print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9]
|
|||||||
|
|
||||||
### shuffle
|
### shuffle
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
:information_source: The same algorithm is already implemented via `random.shuffle`.
|
:information_source: The same algorithm is already implemented via `random.shuffle`.
|
||||||
@ -289,6 +298,7 @@ shuffle(foo) # [2,3,1] , foo = [1,2,3]
|
|||||||
|
|
||||||
### spread
|
### spread
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Implements javascript's `[].concat(...arr)`. Flattens the list(non-deep) and returns an list.
|
Implements javascript's `[].concat(...arr)`. Flattens the list(non-deep) and returns an list.
|
||||||
@ -313,6 +323,7 @@ spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]
|
|||||||
|
|
||||||
### zip
|
### zip
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
:information_source: Already implemented via `itertools.zip_longest()`
|
:information_source: Already implemented via `itertools.zip_longest()`
|
||||||
@ -345,13 +356,14 @@ zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2,
|
|||||||
|
|
||||||
### average
|
### average
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
|
||||||
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [Hui Binyuan](https://www.github.com/huybery)
|
||||||
|
|
||||||
:information_source: Already implemented via `statistics.mean`. `statistics.mean` takes an array as an argument whereas this function takes variadic arguments.
|
:information_source: Already implemented via `statistics.mean`. `statistics.mean` takes an array as an argument whereas this function takes variadic arguments.
|
||||||
|
|
||||||
Returns the average of two or more numbers.
|
Returns the average of two or more numbers.
|
||||||
|
|
||||||
Takes the sum of all the `args` and divides it by `len(args)`. The secind argument `0.0` in sum is to handle floating point division in `python2`.
|
Takes the sum of all the `args` and divides it by `len(args)`. The second argument `0.0` in sum is to handle floating point division in `python2`.
|
||||||
```py
|
```py
|
||||||
def average(*args):
|
def average(*args):
|
||||||
return sum(args, 0.0) / len(args)
|
return sum(args, 0.0) / len(args)
|
||||||
@ -368,6 +380,7 @@ average(1, 2, 3) # 2.0
|
|||||||
|
|
||||||
### factorial
|
### factorial
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Calculates the factorial of a number.
|
Calculates the factorial of a number.
|
||||||
@ -391,6 +404,7 @@ factorial(6) # 720
|
|||||||
|
|
||||||
### gcd
|
### gcd
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [cclauss][@cclauss]
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [cclauss][@cclauss]
|
||||||
|
|
||||||
:information_source: `math.gcd` works with only two numbers
|
:information_source: `math.gcd` works with only two numbers
|
||||||
@ -434,6 +448,7 @@ gcd(8,36) # 4
|
|||||||
|
|
||||||
### lcm
|
### lcm
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Returns the least common multiple of two or more numbers.
|
Returns the least common multiple of two or more numbers.
|
||||||
@ -479,6 +494,7 @@ lcm([1, 3, 4], 5) # 60
|
|||||||
|
|
||||||
### max_n
|
### max_n
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
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).
|
||||||
@ -506,6 +522,7 @@ max_n([1, 2, 3], 2) # [3,2]
|
|||||||
|
|
||||||
### min_n
|
### min_n
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
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).
|
||||||
@ -534,6 +551,7 @@ min_n([1, 2, 3], 2) # [1,2]
|
|||||||
|
|
||||||
### byte_size
|
### byte_size
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Returns the length of a string in bytes.
|
Returns the length of a string in bytes.
|
||||||
@ -555,6 +573,7 @@ byte_size('Hello World') # 11
|
|||||||
|
|
||||||
### capitalize
|
### capitalize
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Capitalizes the first letter of a string.
|
Capitalizes the first letter of a string.
|
||||||
@ -576,6 +595,7 @@ capitalize('fooBar', True) # 'Foobar'
|
|||||||
|
|
||||||
### capitalize_every_word
|
### capitalize_every_word
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Capitalizes the first letter of every word in a string.
|
Capitalizes the first letter of every word in a string.
|
||||||
@ -596,6 +616,7 @@ capitalize_every_word('hello world!') # 'Hello World!'
|
|||||||
|
|
||||||
### count_vowels
|
### count_vowels
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Retuns `number` of vowels in provided `string`.
|
Retuns `number` of vowels in provided `string`.
|
||||||
@ -620,6 +641,7 @@ count_vowels('gym') # 0
|
|||||||
|
|
||||||
### decapitalize
|
### decapitalize
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Decapitalizes the first letter of a string.
|
Decapitalizes the first letter of a string.
|
||||||
@ -641,6 +663,7 @@ decapitalize('FooBar', True) # 'fOOBAR'
|
|||||||
|
|
||||||
### is_lower_case
|
### is_lower_case
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Checks if a string is lower case.
|
Checks if a string is lower case.
|
||||||
@ -663,6 +686,7 @@ is_lower_case('Ab4') # False
|
|||||||
|
|
||||||
### is_upper_case
|
### is_upper_case
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Checks if a string is upper case.
|
Checks if a string is upper case.
|
||||||
@ -685,6 +709,7 @@ is_upper_case('aB4') # False
|
|||||||
|
|
||||||
### palindrome
|
### palindrome
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
Returns `True` if the given string is a palindrome, `False` otherwise.
|
Returns `True` if the given string is a palindrome, `False` otherwise.
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
def count_by(arr, fn=lambda x: x):
|
def count_by(arr, fn=lambda x: x):
|
||||||
key = {}
|
key = {}
|
||||||
for el in map(fn, arr):
|
for el in map(fn, arr):
|
||||||
key[el] = 0 if not el in key else key[el]
|
key[el] = 0 if el not in key else key[el]
|
||||||
key[el] += 1
|
key[el] += 1
|
||||||
return key
|
return key
|
||||||
@ -1,9 +1,9 @@
|
|||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block content %}<div class="row" style="height:calc(100vh - 5.875rem);overflow:hidden"><input id="doc-drawer-checkbox" class="drawer" value="on" type="checkbox"><nav class="col-md-4 col-lg-3" style="border-top:0"><div class="group"><input class="search" id="searchInput" onkeyup="search(this)" type="text"><label id="search-label">Search for snippet...</label></div><label for="doc-drawer-checkbox" class="button drawer-close"></label><h3 style = "">Math</h3><a class="sublink-1" tags="math" href="#average" style="">average</a><a class="sublink-1" tags="math" href="#factorial" style="">factorial</a><a class="sublink-1" tags="math" href="#gcd" style="">gcd</a><a class="sublink-1" tags="math" href="#lcm" style="">lcm</a><a class="sublink-1" tags="math" href="#max_n" style="">max_n</a><a class="sublink-1" tags="math" href="#min_n" style="">min_n</a><h3 style = "">List</h3><a class="sublink-1" tags="list" href="#chunk" style="">chunk</a><a class="sublink-1" tags="list" href="#compact" style="">compact</a><a class="sublink-1" tags="list" href="#count_occurences" style="">count_occurences</a><a class="sublink-1" tags="list" href="#deep_flatten" style="">deep_flatten</a><a class="sublink-1" tags="list" href="#difference" style="">difference</a><a class="sublink-1" tags="list" href="#shuffle" style="">shuffle</a><a class="sublink-1" tags="list" href="#spread" style="">spread</a><a class="sublink-1" tags="list" href="#zip" style="">zip</a><a class="sublink-1" tags="list" href="#count_by" style="">count_by</a><a class="sublink-1" tags="list" href="#difference_by" style="">difference_by</a><h3 style = "">String</h3><a class="sublink-1" tags="string" href="#count_vowels" style="">count_vowels</a><a class="sublink-1" tags="string" href="#byte_size" style="">byte_size</a><a class="sublink-1" tags="string" href="#capitalize" style="">capitalize</a><a class="sublink-1" tags="string" href="#capitalize_every_word" style="">capitalize_every_word</a><a class="sublink-1" tags="string" href="#decapitalize" style="">decapitalize</a><a class="sublink-1" tags="string" href="#palindrome" style="">palindrome</a><a class="sublink-1" tags="string" href="#is_upper_case" style="">is_upper_case</a><a class="sublink-1" tags="string" href="#is_lower_case" style="">is_lower_case</a></nav><main class="col-sm-12 col-md-8 col-lg-9" style="height:100%;overflow-y:auto;background:#eceef2;padding:0"><a id="top"></a><h2 style="text-align:center">Math</h2><div class="card fluid"><h3 id="average" class="section double-padded">average</h3><!--<form action="" method="post"><button type="submit" value="average" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
|
{% block content %}<div class="row" style="height:calc(100vh - 5.875rem);overflow:hidden"><input id="doc-drawer-checkbox" class="drawer" value="on" type="checkbox"><nav class="col-md-4 col-lg-3" style="border-top:0"><div class="group"><input class="search" id="searchInput" onkeyup="search(this)" type="text"><label id="search-label">Search for snippet...</label></div><label for="doc-drawer-checkbox" class="button drawer-close"></label><h3 style = "">Math</h3><a class="sublink-1" tags="math" href="#average" style="">average</a><a class="sublink-1" tags="math" href="#factorial" style="">factorial</a><a class="sublink-1" tags="math" href="#gcd" style="">gcd</a><a class="sublink-1" tags="math" href="#lcm" style="">lcm</a><a class="sublink-1" tags="math" href="#max_n" style="">max_n</a><a class="sublink-1" tags="math" href="#min_n" style="">min_n</a><h3 style = "">List</h3><a class="sublink-1" tags="list" href="#chunk" style="">chunk</a><a class="sublink-1" tags="list" href="#compact" style="">compact</a><a class="sublink-1" tags="list" href="#count_occurences" style="">count_occurences</a><a class="sublink-1" tags="list" href="#deep_flatten" style="">deep_flatten</a><a class="sublink-1" tags="list" href="#difference" style="">difference</a><a class="sublink-1" tags="list" href="#shuffle" style="">shuffle</a><a class="sublink-1" tags="list" href="#spread" style="">spread</a><a class="sublink-1" tags="list" href="#zip" style="">zip</a><a class="sublink-1" tags="list" href="#count_by" style="">count_by</a><a class="sublink-1" tags="list" href="#difference_by" style="">difference_by</a><a class="sublink-1" tags="list" href="#insertion_sort" style="">insertion_sort</a><h3 style = "">String</h3><a class="sublink-1" tags="string" href="#count_vowels" style="">count_vowels</a><a class="sublink-1" tags="string" href="#byte_size" style="">byte_size</a><a class="sublink-1" tags="string" href="#capitalize" style="">capitalize</a><a class="sublink-1" tags="string" href="#capitalize_every_word" style="">capitalize_every_word</a><a class="sublink-1" tags="string" href="#decapitalize" style="">decapitalize</a><a class="sublink-1" tags="string" href="#palindrome" style="">palindrome</a><a class="sublink-1" tags="string" href="#is_upper_case" style="">is_upper_case</a><a class="sublink-1" tags="string" href="#is_lower_case" style="">is_lower_case</a></nav><main class="col-sm-12 col-md-8 col-lg-9" style="height:100%;overflow-y:auto;background:#eceef2;padding:0"><a id="top"></a><h2 style="text-align:center">Math</h2><div class="card fluid"><h3 id="average" class="section double-padded">average</h3><!--<form action="" method="post"><button type="submit" value="average" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
|
||||||
<p><emoji>ℹ</emoji> Already implemented via <code>statistics.mean</code>. <code>statistics.mean</code> takes an array as an argument whereas this function takes variadic arguments.</p>
|
<p><emoji>ℹ</emoji> Already implemented via <code>statistics.mean</code>. <code>statistics.mean</code> takes an array as an argument whereas this function takes variadic arguments.</p>
|
||||||
<p>Returns the average of two or more numbers.</p>
|
<p>Returns the average of two or more numbers.</p>
|
||||||
<p>Takes the sum of all the <code>args</code> and divides it by <code>len(args)</code>. The secind argument <code>0.0</code> in sum is to handle floating point division in <code>python2</code>.</p>
|
<p>Takes the sum of all the <code>args</code> and divides it by <code>len(args)</code>. The second argument <code>0.0</code> in sum is to handle floating point division in <code>python2</code>.</p>
|
||||||
|
|
||||||
<pre class="language-python">def average(*args):
|
<pre class="language-python">def average(*args):
|
||||||
return sum(args, 0.0) / len(args)</pre>
|
return sum(args, 0.0) / len(args)</pre>
|
||||||
@ -79,7 +79,7 @@ def lcm(*args):
|
|||||||
numbers.extend(spread(list(args)))
|
numbers.extend(spread(list(args)))
|
||||||
|
|
||||||
def _gcd(x, y):
|
def _gcd(x, y):
|
||||||
return x if not y else gcd(y, x % y)
|
return x if not y else _gcd(y, x % y)
|
||||||
|
|
||||||
def _lcm(x, y):
|
def _lcm(x, y):
|
||||||
return x * y / _gcd(x, y)
|
return x * y / _gcd(x, y)
|
||||||
@ -153,7 +153,10 @@ def chunk(arr, size):
|
|||||||
<p>Counts the occurrences of a value in an list.</p>
|
<p>Counts the occurrences of a value in an list.</p>
|
||||||
<p>Uses the <code>reduce</code> functin from built-in module <code>functools</code> to increment a counter each time you encounter the specific value inside the list.</p>
|
<p>Uses the <code>reduce</code> functin from built-in module <code>functools</code> to increment a counter each time you encounter the specific value inside the list.</p>
|
||||||
|
|
||||||
<pre class="language-python">def count_occurences(arr, val):
|
<pre class="language-python">from functools import reduce
|
||||||
|
|
||||||
|
|
||||||
|
def count_occurences(arr, val):
|
||||||
return reduce(
|
return reduce(
|
||||||
(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)</pre>
|
arr)</pre>
|
||||||
@ -178,7 +181,7 @@ def chunk(arr, size):
|
|||||||
def deep_flatten(arr):
|
def deep_flatten(arr):
|
||||||
result = []
|
result = []
|
||||||
result.extend(
|
result.extend(
|
||||||
spread(list(map(lambda x: deep(x) if type(x) == list else x, arr))))
|
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, arr))))
|
||||||
return result</pre>
|
return result</pre>
|
||||||
<label class="collapse">Show examples</label>
|
<label class="collapse">Show examples</label>
|
||||||
<pre class="language-python">deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]</pre>
|
<pre class="language-python">deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]</pre>
|
||||||
@ -259,7 +262,7 @@ zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2,
|
|||||||
<pre class="language-python">def count_by(arr, fn=lambda x: x):
|
<pre class="language-python">def count_by(arr, fn=lambda x: x):
|
||||||
key = {}
|
key = {}
|
||||||
for el in map(fn, arr):
|
for el in map(fn, arr):
|
||||||
key[el] = 0 if not el in key else key[el]
|
key[el] = 0 if el not in key else key[el]
|
||||||
key[el] += 1
|
key[el] += 1
|
||||||
return key</pre>
|
return key</pre>
|
||||||
<label class="collapse">Show examples</label>
|
<label class="collapse">Show examples</label>
|
||||||
@ -281,6 +284,24 @@ 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 } ]</pre>
|
difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x: 2 } ]</pre>
|
||||||
<button class="primary clipboard-copy">📋 Copy to clipboard</button></div>
|
<button class="primary clipboard-copy">📋 Copy to clipboard</button></div>
|
||||||
|
|
||||||
|
</div><div class="card fluid"><h3 id="insertion_sort" class="section double-padded">insertion_sort</h3><!--<form action="" method="post"><button type="submit" value="insertion_sort" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
|
||||||
|
<p>On a very basic level, an insertion sort algorithm contains the logic of shifting around and inserting elements in order to sort an unordered list of any size. The way that it goes about inserting elements, however, is what makes insertion sort so very interesting!</p>
|
||||||
|
|
||||||
|
<pre class="language-python">def insertion_sort(arr):
|
||||||
|
|
||||||
|
for i in range(1, len(arr)):
|
||||||
|
key = arr[i]
|
||||||
|
j = i - 1
|
||||||
|
while j >= 0 and key < arr[j]:
|
||||||
|
arr[j + 1] = arr[j]
|
||||||
|
j -= 1
|
||||||
|
arr[j + 1] = key</pre>
|
||||||
|
<label class="collapse">Show examples</label>
|
||||||
|
<pre class="language-python">arr = [7,4,9,2,6,3]
|
||||||
|
insertionsort(arr)
|
||||||
|
print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9]</pre>
|
||||||
|
<button class="primary clipboard-copy">📋 Copy to clipboard</button></div>
|
||||||
|
|
||||||
</div><h2 style="text-align:center">String</h2><div class="card fluid"><h3 id="count_vowels" class="section double-padded">count_vowels</h3><!--<form action="" method="post"><button type="submit" value="count_vowels" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
|
</div><h2 style="text-align:center">String</h2><div class="card fluid"><h3 id="count_vowels" class="section double-padded">count_vowels</h3><!--<form action="" method="post"><button type="submit" value="count_vowels" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
|
||||||
<p>Retuns <code>number</code> of vowels in provided <code>string</code>.</p>
|
<p>Retuns <code>number</code> of vowels in provided <code>string</code>.</p>
|
||||||
<p>Use a regular expression to count the number of vowels <code>(A, E, I, O, U)</code> in a string.</p>
|
<p>Use a regular expression to count the number of vowels <code>(A, E, I, O, U)</code> in a string.</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user