Merge pull request #31 from vignesh240/changing-array-to-list

changing-array-to-list
This commit is contained in:
Rohit Tanwar
2018-04-14 15:49:04 +05:30
committed by GitHub
9 changed files with 45 additions and 45 deletions

View File

@ -2,18 +2,18 @@
Bubble_sort uses the technique of comparing and swapping Bubble_sort uses the technique of comparing and swapping
```python ```python
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
``` ```
```python ```python
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]
``` ```

View File

@ -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 ```python
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)))))
``` ```
``` python ``` python

View File

@ -5,8 +5,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 "").
```python ```python
def compact(arr): def compact(lst):
return list(filter(bool, arr)) return list(filter(bool, lst))
``` ```
``` python ``` python

View File

@ -2,7 +2,7 @@
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.
```python ```python
def spread(arg): def spread(arg):
@ -15,10 +15,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
``` ```

View File

@ -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! 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 ```python
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
``` ```
```python ```python
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]
``` ```

View File

@ -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). 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 ```python
def max_n(arr, n=1, reverse=True): def max_n(lst, n=1, reverse=True):
return sorted(arr, reverse=reverse)[:n] return sorted(lst, reverse=reverse)[:n]
``` ```
```python ```python
max_n([1, 2, 3]) # [3] max_n([1, 2, 3]) # [3]
max_n([1, 2, 3], 2) # [3,2] max_n([1, 2, 3], 2) # [3,2]
``` ```

View File

@ -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). 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 ```python
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]
``` ```
@ -17,4 +17,4 @@ def min_n(arr, n=1):
```python ```python
min_n([1, 2, 3]) # [1] min_n([1, 2, 3]) # [1]
min_n([1, 2, 3], 2) # [1,2] min_n([1, 2, 3], 2) # [1,2]
``` ```

View File

@ -11,17 +11,17 @@ 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
``` ```
``` python ``` python
foo = [1,2,3] foo = [1,2,3]
shuffle(foo) # [2,3,1] , foo = [1,2,3] shuffle(foo) # [2,3,1] , foo = [1,2,3]
``` ```

View File

@ -8,7 +8,7 @@ Use `max` combined with `list comprehension` to get the length of the longest li
```python ```python
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([
@ -21,4 +21,4 @@ def zip(*args, fillvalue=None):
zip(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]] 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]) # [['a', 1, True], [None, 2, False]]
zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]] zip(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]]
``` ```