Travis build: 943
This commit is contained in:
102
README.md
102
README.md
@ -1,7 +1,8 @@
|
||||

|
||||
|
||||
# 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)
|
||||
[](http://www.firsttimersonly.com/) [](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)
|
||||
|
||||
>Python implementation of 30-seconds-of-code.
|
||||
@ -58,20 +59,20 @@
|
||||
|
||||
Bubble_sort uses the technique of comparing and swapping
|
||||
```py
|
||||
def bubble_sort(arr):
|
||||
for passnum in range(len(arr) - 1, 0, -1):
|
||||
def bubble_sort(lst):
|
||||
for passnum in range(len(lst) - 1, 0, -1):
|
||||
for i in range(passnum):
|
||||
if arr[i] > arr[i + 1]:
|
||||
temp = arr[i]
|
||||
arr[i] = arr[i + 1]
|
||||
arr[i + 1] = temp
|
||||
if lst[i] > lst[i + 1]:
|
||||
temp = lst[i]
|
||||
lst[i] = lst[i + 1]
|
||||
lst[i + 1] = temp
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
arr = [54,26,93,17,77,31,44,55,20]
|
||||
bubble_sort(arr)
|
||||
print("sorted %s" %arr) # [17,20,26,31,44,54,55,77,91]
|
||||
lst = [54,26,93,17,77,31,44,55,20]
|
||||
bubble_sort(lst)
|
||||
print("sorted %s" %lst) # [17,20,26,31,44,54,55,77,91]
|
||||
```
|
||||
</details>
|
||||
|
||||
@ -82,17 +83,17 @@ print("sorted %s" %arr) # [17,20,26,31,44,54,55,77,91]
|
||||
|
||||
<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 list 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 `lst`.
|
||||
```py
|
||||
from math import ceil
|
||||
|
||||
|
||||
def chunk(arr, size):
|
||||
def chunk(lst, size):
|
||||
return list(
|
||||
map(lambda x: arr[x * size:x * size + size],
|
||||
list(range(0, ceil(len(arr) / size)))))
|
||||
map(lambda x: lst[x * size:x * size + size],
|
||||
list(range(0, ceil(len(lst) / size)))))
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
@ -112,8 +113,8 @@ Removes falsey values from a list.
|
||||
|
||||
Use `filter()` to filter out falsey values (False, None, 0, and "").
|
||||
```py
|
||||
def compact(arr):
|
||||
return list(filter(lambda x: bool(x), arr))
|
||||
def compact(lst):
|
||||
return list(filter(bool, lst))
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
@ -183,7 +184,7 @@ count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3
|
||||
|
||||
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 list (`result`) and the spread function to flatten a list. Recursively flatten each element that is a list.
|
||||
```py
|
||||
def spread(arg):
|
||||
ret = []
|
||||
@ -195,10 +196,10 @@ def spread(arg):
|
||||
return ret
|
||||
|
||||
|
||||
def deep_flatten(arr):
|
||||
def deep_flatten(lst):
|
||||
result = []
|
||||
result.extend(
|
||||
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, arr))))
|
||||
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))
|
||||
return result
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
@ -215,12 +216,11 @@ deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]
|
||||
|
||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
||||
|
||||
Returns the difference between two arrays.
|
||||
Returns the difference between two iterables.
|
||||
|
||||
Create a `set` from `b`, then use list comprehension to only keep values not contained in `b`
|
||||
Use list comprehension to only keep values not contained in `b`
|
||||
```py
|
||||
def difference(a, b):
|
||||
b = set(b)
|
||||
return [item for item in a if item not in b]
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
@ -263,22 +263,22 @@ difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x
|
||||
|
||||
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!
|
||||
```py
|
||||
def insertion_sort(arr):
|
||||
def insertion_sort(lst):
|
||||
|
||||
for i in range(1, len(arr)):
|
||||
key = arr[i]
|
||||
for i in range(1, len(lst)):
|
||||
key = lst[i]
|
||||
j = i - 1
|
||||
while j >= 0 and key < arr[j]:
|
||||
arr[j + 1] = arr[j]
|
||||
while j >= 0 and key < lst[j]:
|
||||
lst[j + 1] = lst[j]
|
||||
j -= 1
|
||||
arr[j + 1] = key
|
||||
lst[j + 1] = key
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
```py
|
||||
arr = [7,4,9,2,6,3]
|
||||
insertionsort(arr)
|
||||
print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9]
|
||||
lst = [7,4,9,2,6,3]
|
||||
insertionsort(lst)
|
||||
print('Sorted %s' %lst) # sorted [2, 3, 4, 6, 7, 9]
|
||||
```
|
||||
</details>
|
||||
|
||||
@ -299,14 +299,14 @@ from copy import deepcopy
|
||||
from random import randint
|
||||
|
||||
|
||||
def shuffle(arr):
|
||||
temp_arr = deepcopy(arr)
|
||||
m = len(temp_arr)
|
||||
def shuffle(lst):
|
||||
temp_lst = deepcopy(lst)
|
||||
m = len(temp_lst)
|
||||
while (m):
|
||||
m -= 1
|
||||
i = randint(0, m)
|
||||
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
|
||||
return temp_arr
|
||||
temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
|
||||
return temp_lst
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
@ -355,7 +355,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`.
|
||||
```py
|
||||
def zip(*args, fillvalue=None):
|
||||
max_length = max([len(arr) for arr in args])
|
||||
max_length = max([len(lst) for lst in args])
|
||||
result = []
|
||||
for i in range(max_length):
|
||||
result.append([
|
||||
@ -521,16 +521,10 @@ lcm([1, 3, 4], 5) # 60
|
||||
|
||||
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 list
|
||||
```py
|
||||
from copy import deepcopy
|
||||
|
||||
|
||||
def max_n(arr, n=1):
|
||||
numbers = deepcopy(arr)
|
||||
numbers.sort()
|
||||
numbers.reverse()
|
||||
return numbers[:n]
|
||||
def max_n(lst, n=1, reverse=True):
|
||||
return sorted(lst, reverse=reverse)[:n]
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
@ -549,13 +543,13 @@ max_n([1, 2, 3], 2) # [3,2]
|
||||
|
||||
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 list
|
||||
```py
|
||||
from copy import deepcopy
|
||||
|
||||
|
||||
def min_n(arr, n=1):
|
||||
numbers = deepcopy(arr)
|
||||
def min_n(lst, n=1):
|
||||
numbers = deepcopy(lst)
|
||||
numbers.sort()
|
||||
return numbers[:n]
|
||||
```
|
||||
@ -692,8 +686,8 @@ Checks if a string is lower case.
|
||||
|
||||
Convert the given string to lower case, using `str.lower()` method and compare it to the original.
|
||||
```py
|
||||
def is_lower_case(str):
|
||||
return str == str.lower()
|
||||
def is_lower_case(string):
|
||||
return string == string.lower()
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
@ -715,8 +709,8 @@ Checks if a string is upper case.
|
||||
|
||||
Convert the given string to upper case, using `str.upper()` method and compare it to the original.
|
||||
```py
|
||||
def is_upper_case(str):
|
||||
return str == str.upper()
|
||||
def is_upper_case(string):
|
||||
return string == string.upper()
|
||||
```
|
||||
<details><summary>View Examples</summary>
|
||||
|
||||
|
||||
7
test/bubble_sort/bubble_sort.py
Normal file
7
test/bubble_sort/bubble_sort.py
Normal file
@ -0,0 +1,7 @@
|
||||
def bubble_sort(lst):
|
||||
for passnum in range(len(lst) - 1, 0, -1):
|
||||
for i in range(passnum):
|
||||
if lst[i] > lst[i + 1]:
|
||||
temp = lst[i]
|
||||
lst[i] = lst[i + 1]
|
||||
lst[i + 1] = temp
|
||||
6
test/bubble_sort/bubble_sort.test.py
Normal file
6
test/bubble_sort/bubble_sort.test.py
Normal file
@ -0,0 +1,6 @@
|
||||
import types,functools
|
||||
from pytape import test
|
||||
from bubble_sort import bubble_sort
|
||||
def bubble_sort_test(t):
|
||||
t.true(isinstance(bubble_sort, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'<util.read_snippets.<locals>.snippet object at 0x7fc8ea4c6978> is a function')
|
||||
test('Testing bubble_sort',bubble_sort_test)
|
||||
@ -1,7 +1,7 @@
|
||||
from math import ceil
|
||||
|
||||
|
||||
def chunk(arr, size):
|
||||
def chunk(lst, size):
|
||||
return list(
|
||||
map(lambda x: arr[x * size:x * size + size],
|
||||
list(range(0, ceil(len(arr) / size)))))
|
||||
map(lambda x: lst[x * size:x * size + size],
|
||||
list(range(0, ceil(len(lst) / size)))))
|
||||
@ -1,2 +1,2 @@
|
||||
def compact(arr):
|
||||
return list(filter(lambda x: bool(x), arr))
|
||||
def compact(lst):
|
||||
return list(filter(bool, lst))
|
||||
@ -1,7 +1,2 @@
|
||||
from functools import reduce
|
||||
|
||||
|
||||
def count_occurences(arr, val):
|
||||
return reduce(
|
||||
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
|
||||
arr)
|
||||
def count_occurrences(lst, val):
|
||||
return len([x for x in lst if x == val and type(x) == type(val)])
|
||||
@ -8,8 +8,8 @@ def spread(arg):
|
||||
return ret
|
||||
|
||||
|
||||
def deep_flatten(arr):
|
||||
def deep_flatten(lst):
|
||||
result = []
|
||||
result.extend(
|
||||
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, arr))))
|
||||
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))
|
||||
return result
|
||||
@ -1,3 +1,2 @@
|
||||
def difference(a, b):
|
||||
b = set(b)
|
||||
return [item for item in a if item not in b]
|
||||
@ -1,9 +1,9 @@
|
||||
def insertion_sort(arr):
|
||||
def insertion_sort(lst):
|
||||
|
||||
for i in range(1, len(arr)):
|
||||
key = arr[i]
|
||||
for i in range(1, len(lst)):
|
||||
key = lst[i]
|
||||
j = i - 1
|
||||
while j >= 0 and key < arr[j]:
|
||||
arr[j + 1] = arr[j]
|
||||
while j >= 0 and key < lst[j]:
|
||||
lst[j + 1] = lst[j]
|
||||
j -= 1
|
||||
arr[j + 1] = key
|
||||
lst[j + 1] = key
|
||||
@ -1,2 +1,2 @@
|
||||
def is_lower_case(str):
|
||||
return str == str.lower()
|
||||
def is_lower_case(string):
|
||||
return string == string.lower()
|
||||
@ -1,2 +1,2 @@
|
||||
def is_upper_case(str):
|
||||
return str == str.upper()
|
||||
def is_upper_case(string):
|
||||
return string == string.upper()
|
||||
@ -1,8 +1,2 @@
|
||||
from copy import deepcopy
|
||||
|
||||
|
||||
def max_n(arr, n=1):
|
||||
numbers = deepcopy(arr)
|
||||
numbers.sort()
|
||||
numbers.reverse()
|
||||
return numbers[:n]
|
||||
def max_n(lst, n=1, reverse=True):
|
||||
return sorted(lst, reverse=reverse)[:n]
|
||||
@ -1,7 +1,7 @@
|
||||
from copy import deepcopy
|
||||
|
||||
|
||||
def min_n(arr, n=1):
|
||||
numbers = deepcopy(arr)
|
||||
def min_n(lst, n=1):
|
||||
numbers = deepcopy(lst)
|
||||
numbers.sort()
|
||||
return numbers[:n]
|
||||
@ -2,11 +2,11 @@ from copy import deepcopy
|
||||
from random import randint
|
||||
|
||||
|
||||
def shuffle(arr):
|
||||
temp_arr = deepcopy(arr)
|
||||
m = len(temp_arr)
|
||||
def shuffle(lst):
|
||||
temp_lst = deepcopy(lst)
|
||||
m = len(temp_lst)
|
||||
while (m):
|
||||
m -= 1
|
||||
i = randint(0, m)
|
||||
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
|
||||
return temp_arr
|
||||
temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
|
||||
return temp_lst
|
||||
@ -1,5 +1,5 @@
|
||||
def zip(*args, fillvalue=None):
|
||||
max_length = max([len(arr) for arr in args])
|
||||
max_length = max([len(lst) for lst in args])
|
||||
result = []
|
||||
for i in range(max_length):
|
||||
result.append([
|
||||
|
||||
1
website/app/snippets
Normal file
1
website/app/snippets
Normal file
@ -0,0 +1 @@
|
||||
bubble_sort
|
||||
@ -92,15 +92,10 @@ lcm([1, 3, 4], 5) # 60</pre>
|
||||
|
||||
</div><div class="card fluid"><h3 id="max_n" class="section double-padded">max_n</h3><!--<form action="" method="post"><button type="submit" value="max_n" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
|
||||
<p>Returns the <code>n</code> maximum elements from the provided list. If <code>n</code> is greater than or equal to the provided list's length, then return the original list(sorted in descending order).</p>
|
||||
<p>Use <code>list.sort()</code> combined with the <code>deepcopy</code> function from the inbuilt <code>copy</code> module to create a shallow clone of the list and sort it in ascending order and then use <code>list.reverse()</code> reverse it to make it descending order. Use <code>[:n]</code> to get the specified number of elements. Omit the second argument, <code>n</code>, to get a one-element array</p>
|
||||
<p>Use <code>list.sort()</code> combined with the <code>deepcopy</code> function from the inbuilt <code>copy</code> module to create a shallow clone of the list and sort it in ascending order and then use <code>list.reverse()</code> reverse it to make it descending order. Use <code>[:n]</code> to get the specified number of elements. Omit the second argument, <code>n</code>, to get a one-element list</p>
|
||||
|
||||
<pre class="language-python">from copy import deepcopy
|
||||
|
||||
|
||||
def max_n(arr, n=1):
|
||||
numbers = deepcopy(arr)
|
||||
numbers.sort(reverse=True)
|
||||
return numbers[:n]</pre>
|
||||
<pre class="language-python">def max_n(lst, n=1, reverse=True):
|
||||
return sorted(lst, reverse=reverse)[:n]</pre>
|
||||
<label class="collapse">Show examples</label>
|
||||
<pre class="language-python">max_n([1, 2, 3]) # [3]
|
||||
max_n([1, 2, 3], 2) # [3,2]</pre>
|
||||
@ -108,13 +103,13 @@ max_n([1, 2, 3], 2) # [3,2]</pre>
|
||||
|
||||
</div><div class="card fluid"><h3 id="min_n" class="section double-padded">min_n</h3><!--<form action="" method="post"><button type="submit" value="min_n" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
|
||||
<p>Returns the <code>n</code> minimum elements from the provided list. If <code>n</code> is greater than or equal to the provided list's length, then return the original list(sorted in ascending order).</p>
|
||||
<p>Use <code>list.sort()</code> combined with the <code>deepcopy</code> function from the inbuilt <code>copy</code> module to create a shallow clone of the list and sort it in ascending order. Use <code>[:n]</code> to get the specified number of elements. Omit the second argument, <code>n</code>, to get a one-element array</p>
|
||||
<p>Use <code>list.sort()</code> combined with the <code>deepcopy</code> function from the inbuilt <code>copy</code> module to create a shallow clone of the list and sort it in ascending order. Use <code>[:n]</code> to get the specified number of elements. Omit the second argument, <code>n</code>, to get a one-element list</p>
|
||||
|
||||
<pre class="language-python">from copy import deepcopy
|
||||
|
||||
|
||||
def min_n(arr, n=1):
|
||||
numbers = deepcopy(arr)
|
||||
def min_n(lst, n=1):
|
||||
numbers = deepcopy(lst)
|
||||
numbers.sort()
|
||||
return numbers[:n]</pre>
|
||||
<label class="collapse">Show examples</label>
|
||||
@ -123,16 +118,16 @@ min_n([1, 2, 3], 2) # [1,2]</pre>
|
||||
<button class="primary clipboard-copy">📋 Copy to clipboard</button></div>
|
||||
|
||||
</div><h2 style="text-align:center">List</h2><div class="card fluid"><h3 id="chunk" class="section double-padded">chunk</h3><!--<form action="" method="post"><button type="submit" value="chunk" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
|
||||
<p>Chunks an array into smaller lists of a specified size.</p>
|
||||
<p>Uses <code>range</code> to create a list of desired size. Then use <code>map</code> on this list and fill it with splices of <code>arr</code>.</p>
|
||||
<p>Chunks an list into smaller lists of a specified size.</p>
|
||||
<p>Uses <code>range</code> to create a list of desired size. Then use <code>map</code> on this list and fill it with splices of <code>lst</code>.</p>
|
||||
|
||||
<pre class="language-python">from math import ceil
|
||||
|
||||
|
||||
def chunk(arr, size):
|
||||
def chunk(lst, size):
|
||||
return list(
|
||||
map(lambda x: arr[x * size:x * size + size],
|
||||
list(range(0, ceil(len(arr) / size)))))</pre>
|
||||
map(lambda x: lst[x * size:x * size + size],
|
||||
list(range(0, ceil(len(lst) / size)))))</pre>
|
||||
<label class="collapse">Show examples</label>
|
||||
<pre class="language-python">chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]</pre>
|
||||
<button class="primary clipboard-copy">📋 Copy to clipboard</button></div>
|
||||
@ -141,8 +136,8 @@ def chunk(arr, size):
|
||||
<p>Removes falsey values from a list.</p>
|
||||
<p>Use <code>filter()</code> to filter out falsey values (False, None, 0, and "").</p>
|
||||
|
||||
<pre class="language-python">def compact(arr):
|
||||
return list(filter(bool, arr))</pre>
|
||||
<pre class="language-python">def compact(lst):
|
||||
return list(filter(bool, lst))</pre>
|
||||
<label class="collapse">Show examples</label>
|
||||
<pre class="language-python">compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]</pre>
|
||||
<button class="primary clipboard-copy">📋 Copy to clipboard</button></div>
|
||||
@ -160,7 +155,7 @@ def chunk(arr, size):
|
||||
|
||||
</div><div class="card fluid"><h3 id="deep_flatten" class="section double-padded">deep_flatten</h3><!--<form action="" method="post"><button type="submit" value="deep_flatten" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
|
||||
<p>Deep flattens a list.</p>
|
||||
<p>Use recursion. Use <code>list.extend()</code> with an empty array (<code>result</code>) and the spread function to flatten a list. Recursively flatten each element that is a list.</p>
|
||||
<p>Use recursion. Use <code>list.extend()</code> with an empty list (<code>result</code>) and the spread function to flatten a list. Recursively flatten each element that is a list.</p>
|
||||
|
||||
<pre class="language-python">def spread(arg):
|
||||
ret = []
|
||||
@ -172,10 +167,10 @@ def chunk(arr, size):
|
||||
return ret
|
||||
|
||||
|
||||
def deep_flatten(arr):
|
||||
def deep_flatten(lst):
|
||||
result = []
|
||||
result.extend(
|
||||
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, arr))))
|
||||
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))
|
||||
return result</pre>
|
||||
<label class="collapse">Show examples</label>
|
||||
<pre class="language-python">deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]</pre>
|
||||
@ -200,14 +195,14 @@ def deep_flatten(arr):
|
||||
from random import randint
|
||||
|
||||
|
||||
def shuffle(arr):
|
||||
temp_arr = deepcopy(arr)
|
||||
m = len(temp_arr)
|
||||
def shuffle(lst):
|
||||
temp_lst = deepcopy(lst)
|
||||
m = len(temp_lst)
|
||||
while (m):
|
||||
m -= 1
|
||||
i = randint(0, m)
|
||||
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
|
||||
return temp_arr</pre>
|
||||
temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
|
||||
return temp_lst</pre>
|
||||
<label class="collapse">Show examples</label>
|
||||
<pre class="language-python">foo = [1,2,3]
|
||||
shuffle(foo) # [2,3,1] , foo = [1,2,3]</pre>
|
||||
@ -234,7 +229,7 @@ shuffle(foo) # [2,3,1] , foo = [1,2,3]</pre>
|
||||
<p>Use <code>max</code> combined with <code>list comprehension</code> to get the length of the longest list in the arguments. Loops for <code>max_length</code> times grouping elements. If lengths of <code>lists</code> vary <code>fill_value</code> is used. By default <code>fill_value</code> is <code>None</code>.</p>
|
||||
|
||||
<pre class="language-python">def zip(*args, fillvalue=None):
|
||||
max_length = max([len(arr) for arr in args])
|
||||
max_length = max([len(lst) for lst in args])
|
||||
result = []
|
||||
for i in range(max_length):
|
||||
result.append([
|
||||
@ -280,19 +275,19 @@ difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x
|
||||
</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):
|
||||
<pre class="language-python">def insertion_sort(lst):
|
||||
|
||||
for i in range(1, len(arr)):
|
||||
key = arr[i]
|
||||
for i in range(1, len(lst)):
|
||||
key = lst[i]
|
||||
j = i - 1
|
||||
while j >= 0 and key < arr[j]:
|
||||
arr[j + 1] = arr[j]
|
||||
while j >= 0 and key < lst[j]:
|
||||
lst[j + 1] = lst[j]
|
||||
j -= 1
|
||||
arr[j + 1] = key</pre>
|
||||
lst[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>
|
||||
<pre class="language-python">lst = [7,4,9,2,6,3]
|
||||
insertionsort(lst)
|
||||
print('Sorted %s' %lst) # 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">
|
||||
@ -316,7 +311,7 @@ count_vowels('gym') # 0</pre>
|
||||
<pre class="language-python">def byte_size(string):
|
||||
return(len(string.encode('utf-8')))</pre>
|
||||
<label class="collapse">Show examples</label>
|
||||
<pre class="language-python">byte_size('😀') # 4
|
||||
<pre class="language-python">byte_size('😀') # 4
|
||||
byte_size('Hello World') # 11</pre>
|
||||
<button class="primary clipboard-copy">📋 Copy to clipboard</button></div>
|
||||
|
||||
@ -391,17 +386,17 @@ is_lower_case('Ab4') # False</pre>
|
||||
</div><h2 style="text-align:center"> list</h2><div class="card fluid"><h3 id="bubble_sort" class="section double-padded">bubble_sort</h3><!--<form action="" method="post"><button type="submit" value="bubble_sort" name="submit">Vote</button></form><p></p>--><div class="section double-padded">
|
||||
<p>Bubble_sort uses the technique of comparing and swapping</p>
|
||||
|
||||
<pre class="language-python">def bubble_sort(arr):
|
||||
for passnum in range(len(arr) - 1, 0, -1):
|
||||
<pre class="language-python">def bubble_sort(lst):
|
||||
for passnum in range(len(lst) - 1, 0, -1):
|
||||
for i in range(passnum):
|
||||
if arr[i] > arr[i + 1]:
|
||||
temp = arr[i]
|
||||
arr[i] = arr[i + 1]
|
||||
arr[i + 1] = temp</pre>
|
||||
if lst[i] > lst[i + 1]:
|
||||
temp = lst[i]
|
||||
lst[i] = lst[i + 1]
|
||||
lst[i + 1] = temp</pre>
|
||||
<label class="collapse">Show examples</label>
|
||||
<pre class="language-python">arr = [54,26,93,17,77,31,44,55,20]
|
||||
bubble_sort(arr)
|
||||
print("sorted %s" %arr) # [17,20,26,31,44,54,55,77,91]</pre>
|
||||
<pre class="language-python">lst = [54,26,93,17,77,31,44,55,20]
|
||||
bubble_sort(lst)
|
||||
print("sorted %s" %lst) # [17,20,26,31,44,54,55,77,91]</pre>
|
||||
<button class="primary clipboard-copy">📋 Copy to clipboard</button></div>
|
||||
|
||||
</div><button class="scroll-to-top">↑</button>
|
||||
|
||||
Reference in New Issue
Block a user