Travis build: 943

This commit is contained in:
Rohit Tanwar
2018-04-14 10:54:46 +00:00
parent c265a93ede
commit 3b9790bd73
17 changed files with 132 additions and 141 deletions

102
README.md
View File

@ -1,7 +1,8 @@
![Logo](/icon.png) ![Logo](/icon.png)
# 30-seconds-of-python-code [![Tweet](http://jpillora.com/github-twitter-button/img/tweet.png)](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") # 30-seconds-of-python-code [![Tweet](http://jpillora.com/github-twitter-button/img/tweet.png)](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")
[![License](https://img.shields.io/aur/license/yaourt.svg)](https://github.com/kriadmin/30-seconds-of-python-code/blob/master/LICENSE) [![Gitter chat](https://img.shields.io/badge/chat-on%20gitter-4FB999.svg)](https://gitter.im/30-seconds-of-python-code/Lobby) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Travis Build](https://travis-ci.org/kriadmin/30-seconds-of-python-code.svg?branch=master)](https://travis-ci.org/kriadmin/30-seconds-of-python-code) [![Insight.io](https://img.shields.io/badge/insight.io-Ready-brightgreen.svg)](https://insight.io/github.com/kriadmin/30-seconds-of-python-code/tree/master/?source=0) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard) [![License](https://img.shields.io/aur/license/yaourt.svg)](https://github.com/kriadmin/30-seconds-of-python-code/blob/master/LICENSE)
[![first-timers-only](http://img.shields.io/badge/first--timers--only-friendly-blue.svg?style=flat-square)](http://www.firsttimersonly.com/) [![Gitter chat](https://img.shields.io/badge/chat-on%20gitter-4FB999.svg)](https://gitter.im/30-seconds-of-python-code/Lobby) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![Travis Build](https://travis-ci.org/kriadmin/30-seconds-of-python-code.svg?branch=master)](https://travis-ci.org/kriadmin/30-seconds-of-python-code) [![Insight.io](https://img.shields.io/badge/insight.io-Ready-brightgreen.svg)](https://insight.io/github.com/kriadmin/30-seconds-of-python-code/tree/master/?source=0) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fkriadmin%2F30-seconds-of-python-code.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fkriadmin%2F30-seconds-of-python-code?ref=badge_shield) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fkriadmin%2F30-seconds-of-python-code.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fkriadmin%2F30-seconds-of-python-code?ref=badge_shield)
>Python implementation of 30-seconds-of-code. >Python implementation of 30-seconds-of-code.
@ -58,20 +59,20 @@
Bubble_sort uses the technique of comparing and swapping Bubble_sort uses the technique of comparing and swapping
```py ```py
def bubble_sort(arr): def bubble_sort(lst):
for passnum in range(len(arr) - 1, 0, -1): for passnum in range(len(lst) - 1, 0, -1):
for i in range(passnum): for i in range(passnum):
if arr[i] > arr[i + 1]: if lst[i] > lst[i + 1]:
temp = arr[i] temp = lst[i]
arr[i] = arr[i + 1] lst[i] = lst[i + 1]
arr[i + 1] = temp lst[i + 1] = temp
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
```py ```py
arr = [54,26,93,17,77,31,44,55,20] lst = [54,26,93,17,77,31,44,55,20]
bubble_sort(arr) bubble_sort(lst)
print("sorted %s" %arr) # [17,20,26,31,44,54,55,77,91] print("sorted %s" %lst) # [17,20,26,31,44,54,55,77,91]
``` ```
</details> </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) <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 ```py
from math import ceil from math import ceil
def chunk(arr, size): def chunk(lst, size):
return list( return list(
map(lambda x: arr[x * size:x * size + size], map(lambda x: lst[x * size:x * size + size],
list(range(0, ceil(len(arr) / size))))) list(range(0, ceil(len(lst) / size)))))
``` ```
<details><summary>View Examples</summary> <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 ""). Use `filter()` to filter out falsey values (False, None, 0, and "").
```py ```py
def compact(arr): def compact(lst):
return list(filter(lambda x: bool(x), arr)) return list(filter(bool, lst))
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -183,7 +184,7 @@ count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3
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 list (`result`) and the spread function to flatten a list. Recursively flatten each element that is a list.
```py ```py
def spread(arg): def spread(arg):
ret = [] ret = []
@ -195,10 +196,10 @@ def spread(arg):
return ret return ret
def deep_flatten(arr): def deep_flatten(lst):
result = [] result = []
result.extend( 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 return result
``` ```
<details><summary>View Examples</summary> <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) <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 ```py
def difference(a, b): def difference(a, 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]
``` ```
<details><summary>View Examples</summary> <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! 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 ```py
def insertion_sort(arr): def insertion_sort(lst):
for i in range(1, len(arr)): for i in range(1, len(lst)):
key = arr[i] key = lst[i]
j = i - 1 j = i - 1
while j >= 0 and key < arr[j]: while j >= 0 and key < lst[j]:
arr[j + 1] = arr[j] lst[j + 1] = lst[j]
j -= 1 j -= 1
arr[j + 1] = key lst[j + 1] = key
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
```py ```py
arr = [7,4,9,2,6,3] lst = [7,4,9,2,6,3]
insertionsort(arr) insertionsort(lst)
print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9] print('Sorted %s' %lst) # sorted [2, 3, 4, 6, 7, 9]
``` ```
</details> </details>
@ -299,14 +299,14 @@ from copy import deepcopy
from random import randint from random import randint
def shuffle(arr): def shuffle(lst):
temp_arr = deepcopy(arr) temp_lst = deepcopy(lst)
m = len(temp_arr) m = len(temp_lst)
while (m): while (m):
m -= 1 m -= 1
i = randint(0, m) i = randint(0, m)
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
return temp_arr return temp_lst
``` ```
<details><summary>View Examples</summary> <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`. 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 ```py
def zip(*args, fillvalue=None): def zip(*args, fillvalue=None):
max_length = max([len(arr) for arr in args]) max_length = max([len(lst) for lst in args])
result = [] result = []
for i in range(max_length): for i in range(max_length):
result.append([ 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). 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 ```py
from copy import deepcopy def max_n(lst, n=1, reverse=True):
return sorted(lst, reverse=reverse)[:n]
def max_n(arr, n=1):
numbers = deepcopy(arr)
numbers.sort()
numbers.reverse()
return numbers[:n]
``` ```
<details><summary>View Examples</summary> <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). 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 ```py
from copy import deepcopy from copy import deepcopy
def min_n(arr, n=1): def min_n(lst, n=1):
numbers = deepcopy(arr) numbers = deepcopy(lst)
numbers.sort() numbers.sort()
return numbers[:n] 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. Convert the given string to lower case, using `str.lower()` method and compare it to the original.
```py ```py
def is_lower_case(str): def is_lower_case(string):
return str == str.lower() return string == string.lower()
``` ```
<details><summary>View Examples</summary> <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. Convert the given string to upper case, using `str.upper()` method and compare it to the original.
```py ```py
def is_upper_case(str): def is_upper_case(string):
return str == str.upper() return string == string.upper()
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>

View 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

View 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)

View File

@ -1,7 +1,7 @@
from math import ceil from math import ceil
def chunk(arr, size): def chunk(lst, size):
return list( return list(
map(lambda x: arr[x * size:x * size + size], map(lambda x: lst[x * size:x * size + size],
list(range(0, ceil(len(arr) / size))))) list(range(0, ceil(len(lst) / size)))))

View File

@ -1,2 +1,2 @@
def compact(arr): def compact(lst):
return list(filter(lambda x: bool(x), arr)) return list(filter(bool, lst))

View File

@ -1,7 +1,2 @@
from functools import reduce def count_occurrences(lst, val):
return len([x for x in lst if x == val and type(x) == type(val)])
def count_occurences(arr, val):
return reduce(
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
arr)

View File

@ -8,8 +8,8 @@ def spread(arg):
return ret return ret
def deep_flatten(arr): def deep_flatten(lst):
result = [] result = []
result.extend( 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 return result

View File

@ -1,3 +1,2 @@
def difference(a, b): def difference(a, 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]

View File

@ -1,9 +1,9 @@
def insertion_sort(arr): def insertion_sort(lst):
for i in range(1, len(arr)): for i in range(1, len(lst)):
key = arr[i] key = lst[i]
j = i - 1 j = i - 1
while j >= 0 and key < arr[j]: while j >= 0 and key < lst[j]:
arr[j + 1] = arr[j] lst[j + 1] = lst[j]
j -= 1 j -= 1
arr[j + 1] = key lst[j + 1] = key

View File

@ -1,2 +1,2 @@
def is_lower_case(str): def is_lower_case(string):
return str == str.lower() return string == string.lower()

View File

@ -1,2 +1,2 @@
def is_upper_case(str): def is_upper_case(string):
return str == str.upper() return string == string.upper()

View File

@ -1,8 +1,2 @@
from copy import deepcopy def max_n(lst, n=1, reverse=True):
return sorted(lst, reverse=reverse)[:n]
def max_n(arr, n=1):
numbers = deepcopy(arr)
numbers.sort()
numbers.reverse()
return numbers[:n]

View File

@ -1,7 +1,7 @@
from copy import deepcopy from copy import deepcopy
def min_n(arr, n=1): def min_n(lst, n=1):
numbers = deepcopy(arr) numbers = deepcopy(lst)
numbers.sort() numbers.sort()
return numbers[:n] return numbers[:n]

View File

@ -2,11 +2,11 @@ from copy import deepcopy
from random import randint from random import randint
def shuffle(arr): def shuffle(lst):
temp_arr = deepcopy(arr) temp_lst = deepcopy(lst)
m = len(temp_arr) m = len(temp_lst)
while (m): while (m):
m -= 1 m -= 1
i = randint(0, m) i = randint(0, m)
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
return temp_arr return temp_lst

View File

@ -1,5 +1,5 @@
def zip(*args, fillvalue=None): def zip(*args, fillvalue=None):
max_length = max([len(arr) for arr in args]) max_length = max([len(lst) for lst in args])
result = [] result = []
for i in range(max_length): for i in range(max_length):
result.append([ result.append([

1
website/app/snippets Normal file
View File

@ -0,0 +1 @@
bubble_sort

View File

@ -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"> </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>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 <pre class="language-python">def max_n(lst, n=1, reverse=True):
return sorted(lst, reverse=reverse)[:n]</pre>
def max_n(arr, n=1):
numbers = deepcopy(arr)
numbers.sort(reverse=True)
return numbers[:n]</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">max_n([1, 2, 3]) # [3] <pre class="language-python">max_n([1, 2, 3]) # [3]
max_n([1, 2, 3], 2) # [3,2]</pre> 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"> </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>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 <pre class="language-python">from copy import deepcopy
def min_n(arr, n=1): def min_n(lst, n=1):
numbers = deepcopy(arr) numbers = deepcopy(lst)
numbers.sort() numbers.sort()
return numbers[:n]</pre> return numbers[:n]</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
@ -123,16 +118,16 @@ min_n([1, 2, 3], 2) # [1,2]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;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"> </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>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>arr</code>.</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 <pre class="language-python">from math import ceil
def chunk(arr, size): def chunk(lst, size):
return list( return list(
map(lambda x: arr[x * size:x * size + size], map(lambda x: lst[x * size:x * size + size],
list(range(0, ceil(len(arr) / size)))))</pre> list(range(0, ceil(len(lst) / size)))))</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]</pre> <pre class="language-python">chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
@ -141,8 +136,8 @@ def chunk(arr, size):
<p>Removes falsey values from a list.</p> <p>Removes falsey values from a list.</p>
<p>Use <code>filter()</code> to filter out falsey values (False, None, 0, and "").</p> <p>Use <code>filter()</code> to filter out falsey values (False, None, 0, and "").</p>
<pre class="language-python">def compact(arr): <pre class="language-python">def compact(lst):
return list(filter(bool, arr))</pre> return list(filter(bool, lst))</pre>
<label class="collapse">Show examples</label> <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> <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">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;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"> </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>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): <pre class="language-python">def spread(arg):
ret = [] ret = []
@ -172,10 +167,10 @@ def chunk(arr, size):
return ret return ret
def deep_flatten(arr): def deep_flatten(lst):
result = [] result = []
result.extend( 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> 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>
@ -200,14 +195,14 @@ def deep_flatten(arr):
from random import randint from random import randint
def shuffle(arr): def shuffle(lst):
temp_arr = deepcopy(arr) temp_lst = deepcopy(lst)
m = len(temp_arr) m = len(temp_lst)
while (m): while (m):
m -= 1 m -= 1
i = randint(0, m) i = randint(0, m)
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
return temp_arr</pre> return temp_lst</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">foo = [1,2,3] <pre class="language-python">foo = [1,2,3]
shuffle(foo) # [2,3,1] , foo = [1,2,3]</pre> 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> <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): <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 = [] result = []
for i in range(max_length): for i in range(max_length):
result.append([ 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"> </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> <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)): for i in range(1, len(lst)):
key = arr[i] key = lst[i]
j = i - 1 j = i - 1
while j &gt;= 0 and key &lt; arr[j]: while j &gt;= 0 and key &lt; lst[j]:
arr[j + 1] = arr[j] lst[j + 1] = lst[j]
j -= 1 j -= 1
arr[j + 1] = key</pre> lst[j + 1] = key</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">arr = [7,4,9,2,6,3] <pre class="language-python">lst = [7,4,9,2,6,3]
insertionsort(arr) insertionsort(lst)
print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9]</pre> print('Sorted %s' %lst) # sorted [2, 3, 4, 6, 7, 9]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;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">
@ -316,7 +311,7 @@ count_vowels('gym') # 0</pre>
<pre class="language-python">def byte_size(string): <pre class="language-python">def byte_size(string):
return(len(string.encode('utf-8')))</pre> return(len(string.encode('utf-8')))</pre>
<label class="collapse">Show examples</label> <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> byte_size('Hello World') # 11</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;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"> </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> <p>Bubble_sort uses the technique of comparing and swapping</p>
<pre class="language-python">def bubble_sort(arr): <pre class="language-python">def bubble_sort(lst):
for passnum in range(len(arr) - 1, 0, -1): for passnum in range(len(lst) - 1, 0, -1):
for i in range(passnum): for i in range(passnum):
if arr[i] &gt; arr[i + 1]: if lst[i] &gt; lst[i + 1]:
temp = arr[i] temp = lst[i]
arr[i] = arr[i + 1] lst[i] = lst[i + 1]
arr[i + 1] = temp</pre> lst[i + 1] = temp</pre>
<label class="collapse">Show examples</label> <label class="collapse">Show examples</label>
<pre class="language-python">arr = [54,26,93,17,77,31,44,55,20] <pre class="language-python">lst = [54,26,93,17,77,31,44,55,20]
bubble_sort(arr) bubble_sort(lst)
print("sorted %s" %arr) # [17,20,26,31,44,54,55,77,91]</pre> print("sorted %s" %lst) # [17,20,26,31,44,54,55,77,91]</pre>
<button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div> <button class="primary clipboard-copy">📋&nbsp;Copy to clipboard</button></div>
</div><button class="scroll-to-top"></button> </div><button class="scroll-to-top"></button>