From 841385b5eda93d883d748a62c499d20eb1ea4623 Mon Sep 17 00:00:00 2001 From: vignesh Date: Sat, 14 Apr 2018 08:35:58 +0530 Subject: [PATCH] changing-array-to-list --- snippets/bubble_sort.md | 18 +++++++++--------- snippets/chunk.md | 12 ++++++------ snippets/compact.md | 4 ++-- snippets/deep_flatten.md | 6 +++--- snippets/insertion_sort.md | 18 +++++++++--------- snippets/max_n.md | 8 ++++---- snippets/min_n.md | 8 ++++---- snippets/shuffle.md | 12 ++++++------ snippets/zip.md | 4 ++-- 9 files changed, 45 insertions(+), 45 deletions(-) diff --git a/snippets/bubble_sort.md b/snippets/bubble_sort.md index 57b509811..1f81b3324 100644 --- a/snippets/bubble_sort.md +++ b/snippets/bubble_sort.md @@ -2,18 +2,18 @@ Bubble_sort uses the technique of comparing and swapping ```python -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 ``` ```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] +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] ``` diff --git a/snippets/chunk.md b/snippets/chunk.md index d6dc58060..825e602a3 100644 --- a/snippets/chunk.md +++ b/snippets/chunk.md @@ -1,17 +1,17 @@ -### chunk +### chunk -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`. ```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))))) + map(lambda x: lst[x * size:x * size + size], + list(range(0, ceil(len(lst) / size))))) ``` ``` python diff --git a/snippets/compact.md b/snippets/compact.md index 86c8ab9ca..1d1767512 100644 --- a/snippets/compact.md +++ b/snippets/compact.md @@ -5,8 +5,8 @@ Removes falsey values from a list. Use `filter()` to filter out falsey values (False, None, 0, and ""). ```python -def compact(arr): - return list(filter(bool, arr)) +def compact(lst): + return list(filter(bool, lst)) ``` ``` python diff --git a/snippets/deep_flatten.md b/snippets/deep_flatten.md index 62d086589..cfbf6591d 100644 --- a/snippets/deep_flatten.md +++ b/snippets/deep_flatten.md @@ -2,7 +2,7 @@ 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. ```python def spread(arg): @@ -15,10 +15,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 ``` diff --git a/snippets/insertion_sort.md b/snippets/insertion_sort.md index 798c3062f..aa94cb006 100644 --- a/snippets/insertion_sort.md +++ b/snippets/insertion_sort.md @@ -3,19 +3,19 @@ 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! ```python -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 ``` ```python -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] ``` diff --git a/snippets/max_n.md b/snippets/max_n.md index 6fe3f5092..b47ddbdcf 100644 --- a/snippets/max_n.md +++ b/snippets/max_n.md @@ -2,14 +2,14 @@ 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 ```python -def max_n(arr, n=1, reverse=True): - return sorted(arr, reverse=reverse)[:n] +def max_n(lst, n=1, reverse=True): + return sorted(lst, reverse=reverse)[:n] ``` ```python max_n([1, 2, 3]) # [3] max_n([1, 2, 3], 2) # [3,2] -``` \ No newline at end of file +``` diff --git a/snippets/min_n.md b/snippets/min_n.md index 36a436d10..d6c942c64 100644 --- a/snippets/min_n.md +++ b/snippets/min_n.md @@ -2,14 +2,14 @@ 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 ```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] ``` @@ -17,4 +17,4 @@ def min_n(arr, n=1): ```python min_n([1, 2, 3]) # [1] min_n([1, 2, 3], 2) # [1,2] -``` \ No newline at end of file +``` diff --git a/snippets/shuffle.md b/snippets/shuffle.md index 536591f1e..3bcdec8b8 100644 --- a/snippets/shuffle.md +++ b/snippets/shuffle.md @@ -11,17 +11,17 @@ 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 ``` ``` python foo = [1,2,3] shuffle(foo) # [2,3,1] , foo = [1,2,3] -``` \ No newline at end of file +``` diff --git a/snippets/zip.md b/snippets/zip.md index c36700aab..2260bc926 100644 --- a/snippets/zip.md +++ b/snippets/zip.md @@ -8,7 +8,7 @@ Use `max` combined with `list comprehension` to get the length of the longest li ```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([ @@ -21,4 +21,4 @@ def zip(*args, fillvalue=None): zip(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]] zip(['a'], [1, 2], [True, False]) # [['a', 1, True], [None, 2, False]] zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]] -``` \ No newline at end of file +```