changing-array-to-list

This commit is contained in:
vignesh
2018-04-14 08:35:58 +05:30
parent d3c9524379
commit 841385b5ed
9 changed files with 45 additions and 45 deletions

View File

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

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

View File

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

View File

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

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

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

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

View File

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

View File

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