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]
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]
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]
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 ]
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
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]
def byte_size(string):
return(len(string.encode('utf-8')))
-byte_size('Γ°ΕΈΛβ¬') # 4
+byte_size('π') # 4
byte_size('Hello World') # 11
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]