From 3b9790bd73f80afc4af2de1c4fc8f4b5bb5fda45 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sat, 14 Apr 2018 10:54:46 +0000 Subject: [PATCH] Travis build: 943 --- README.md | 102 ++++++++++------------ test/bubble_sort/bubble_sort.py | 7 ++ test/bubble_sort/bubble_sort.test.py | 6 ++ test/chunk/chunk.py | 6 +- test/compact/compact.py | 4 +- test/count_occurences/count_occurences.py | 9 +- test/deep_flatten/deep_flatten.py | 4 +- test/difference/difference.py | 1 - test/insertion_sort/insertion_sort.py | 12 +-- test/is_lower_case/is_lower_case.py | 4 +- test/is_upper_case/is_upper_case.py | 4 +- test/max_n/max_n.py | 10 +-- test/min_n/min_n.py | 4 +- test/shuffle/shuffle.py | 10 +-- test/zip/zip.py | 2 +- website/app/snippets | 1 + website/app/templates/index.html | 87 +++++++++--------- 17 files changed, 132 insertions(+), 141 deletions(-) create mode 100644 test/bubble_sort/bubble_sort.py create mode 100644 test/bubble_sort/bubble_sort.test.py create mode 100644 website/app/snippets diff --git a/README.md b/README.md index 178009540..44d45d672 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ ![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") -[![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) >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 ```
View Examples ```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] ```
@@ -82,17 +83,17 @@ print("sorted %s" %arr) # [17,20,26,31,44,54,55,77,91] Contributors:-[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))))) ```
View Examples @@ -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)) ```
View Examples @@ -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 ```
View Examples @@ -215,12 +216,11 @@ deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5] Contributors:-[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] ```
View Examples @@ -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 ```
View Examples ```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] ```
@@ -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 ```
View Examples @@ -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] ```
View Examples @@ -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() ```
View Examples @@ -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() ```
View Examples diff --git a/test/bubble_sort/bubble_sort.py b/test/bubble_sort/bubble_sort.py new file mode 100644 index 000000000..c8000f193 --- /dev/null +++ b/test/bubble_sort/bubble_sort.py @@ -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 \ No newline at end of file diff --git a/test/bubble_sort/bubble_sort.test.py b/test/bubble_sort/bubble_sort.test.py new file mode 100644 index 000000000..08627cfef --- /dev/null +++ b/test/bubble_sort/bubble_sort.test.py @@ -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)),'.snippet object at 0x7fc8ea4c6978> is a function') +test('Testing bubble_sort',bubble_sort_test) \ No newline at end of file diff --git a/test/chunk/chunk.py b/test/chunk/chunk.py index 6f247c2c9..af2e29307 100644 --- a/test/chunk/chunk.py +++ b/test/chunk/chunk.py @@ -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))))) \ No newline at end of file + map(lambda x: lst[x * size:x * size + size], + list(range(0, ceil(len(lst) / size))))) \ No newline at end of file diff --git a/test/compact/compact.py b/test/compact/compact.py index f90cc92d7..ca84484f8 100644 --- a/test/compact/compact.py +++ b/test/compact/compact.py @@ -1,2 +1,2 @@ -def compact(arr): - return list(filter(lambda x: bool(x), arr)) \ No newline at end of file +def compact(lst): + return list(filter(bool, lst)) \ No newline at end of file diff --git a/test/count_occurences/count_occurences.py b/test/count_occurences/count_occurences.py index 661332a85..64c2cacf5 100644 --- a/test/count_occurences/count_occurences.py +++ b/test/count_occurences/count_occurences.py @@ -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) \ No newline at end of file +def count_occurrences(lst, val): + return len([x for x in lst if x == val and type(x) == type(val)]) \ No newline at end of file diff --git a/test/deep_flatten/deep_flatten.py b/test/deep_flatten/deep_flatten.py index 190bfd53a..18d8a978d 100644 --- a/test/deep_flatten/deep_flatten.py +++ b/test/deep_flatten/deep_flatten.py @@ -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 \ No newline at end of file diff --git a/test/difference/difference.py b/test/difference/difference.py index b375d7748..add57939f 100644 --- a/test/difference/difference.py +++ b/test/difference/difference.py @@ -1,3 +1,2 @@ def difference(a, b): - b = set(b) return [item for item in a if item not in b] \ No newline at end of file diff --git a/test/insertion_sort/insertion_sort.py b/test/insertion_sort/insertion_sort.py index 1d2771675..5f1e6258e 100644 --- a/test/insertion_sort/insertion_sort.py +++ b/test/insertion_sort/insertion_sort.py @@ -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 \ No newline at end of file + lst[j + 1] = key \ No newline at end of file diff --git a/test/is_lower_case/is_lower_case.py b/test/is_lower_case/is_lower_case.py index 3b914e8b9..82c2aca6e 100644 --- a/test/is_lower_case/is_lower_case.py +++ b/test/is_lower_case/is_lower_case.py @@ -1,2 +1,2 @@ -def is_lower_case(str): - return str == str.lower() \ No newline at end of file +def is_lower_case(string): + return string == string.lower() \ No newline at end of file diff --git a/test/is_upper_case/is_upper_case.py b/test/is_upper_case/is_upper_case.py index 062b4f8d2..73256c995 100644 --- a/test/is_upper_case/is_upper_case.py +++ b/test/is_upper_case/is_upper_case.py @@ -1,2 +1,2 @@ -def is_upper_case(str): - return str == str.upper() \ No newline at end of file +def is_upper_case(string): + return string == string.upper() \ No newline at end of file diff --git a/test/max_n/max_n.py b/test/max_n/max_n.py index 70006802c..84a4e9427 100644 --- a/test/max_n/max_n.py +++ b/test/max_n/max_n.py @@ -1,8 +1,2 @@ -from copy import deepcopy - - -def max_n(arr, n=1): - numbers = deepcopy(arr) - numbers.sort() - numbers.reverse() - return numbers[:n] \ No newline at end of file +def max_n(lst, n=1, reverse=True): + return sorted(lst, reverse=reverse)[:n] \ No newline at end of file diff --git a/test/min_n/min_n.py b/test/min_n/min_n.py index c973b3a61..2db277f1e 100644 --- a/test/min_n/min_n.py +++ b/test/min_n/min_n.py @@ -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] \ No newline at end of file diff --git a/test/shuffle/shuffle.py b/test/shuffle/shuffle.py index 2d16df93f..2e2e74ea4 100644 --- a/test/shuffle/shuffle.py +++ b/test/shuffle/shuffle.py @@ -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 \ No newline at end of file + temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m] + return temp_lst \ No newline at end of file diff --git a/test/zip/zip.py b/test/zip/zip.py index ef9aa5593..358682a99 100644 --- a/test/zip/zip.py +++ b/test/zip/zip.py @@ -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([ diff --git a/website/app/snippets b/website/app/snippets new file mode 100644 index 000000000..e33435ef8 --- /dev/null +++ b/website/app/snippets @@ -0,0 +1 @@ +bubble_sort \ No newline at end of file diff --git a/website/app/templates/index.html b/website/app/templates/index.html index 724038a4f..1ec679e81 100644 --- a/website/app/templates/index.html +++ b/website/app/templates/index.html @@ -92,15 +92,10 @@ lcm([1, 3, 4], 5) # 60

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

-

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

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

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

-

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

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]
@@ -123,16 +118,16 @@ min_n([1, 2, 3], 2) # [1,2]

List

chunk

-

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.

+

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 lst.

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)))))
chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]
@@ -141,8 +136,8 @@ def chunk(arr, size):

Removes falsey values from a list.

Use filter() to filter out falsey values (False, None, 0, and "").

-
def compact(arr):
-    return list(filter(bool, arr))
+
def compact(lst):
+    return list(filter(bool, lst))
compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]
@@ -160,7 +155,7 @@ def chunk(arr, size):

deep_flatten

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.

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
deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]
@@ -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 + temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m] + return temp_lst
foo = [1,2,3]
 shuffle(foo) # [2,3,1] , foo = [1,2,3]
@@ -234,7 +229,7 @@ shuffle(foo) # [2,3,1] , foo = [1,2,3]

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.

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
 

insertion_sort

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!

-
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
-
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]

String

count_vowels

@@ -316,7 +311,7 @@ count_vowels('gym') # 0
def byte_size(string):
     return(len(string.encode('utf-8')))
-
byte_size('Γ°ΕΈΛœβ‚¬') # 4
+
byte_size('πŸ˜€') # 4
 byte_size('Hello World') # 11
@@ -391,17 +386,17 @@ is_lower_case('Ab4') # False

list

bubble_sort

Bubble_sort uses the technique of comparing and swapping

-
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
-
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]