Best Python code snippet using localstack_python
sort.py
Source:sort.py
1class InsertionSort(object):2 def sort(self, list_in):3 if list_in is None:4 return TypeError('input can not be None')5 if len(list_in)<2:6 return list_in7 for inseration_index in range(1, len(list_in)):8 for next_index in range(inseration_index):9 if list_in[inseration_index] < list_in[next_index]:10 tmp = list_in[inseration_index]11 list_in[next_index+1:inseration_index+1] = list_in[next_index:inseration_index]12 list_in[next_index] = tmp13 return list_in14 15class SelectionSort(object):16 def sort(self, list_in):17 if list_in is None:18 raise TypeError('input can not be None')19 if len(list_in) < 2:20 return list_in21 for selected_index in range(len(list_in)-1):22 temp = selected_index23 for next_index in range(selected_index+1, len(list_in)):24 if list_in[next_index] < list_in[temp]:25 temp = next_index26 list_in[temp], list_in[selected_index] = list_in[selected_index], list_in[temp]27 return list_in28 29class MergeSort(object):30 """ Sort a list based on the merge sort algorithm """31 32 def sort(self, list_in):33 if list_in is None:34 raise TypeError('input list can not be none')35 return self._sort(list_in)36 37 def _sort(self, list_in):38 if len(list_in)<2:39 return list_in40 mid = len(list_in)//241 right = list_in[:mid]42 left = list_in[mid:]43 right_ = self._sort(right)44 left_ = self._sort(left)45 return self._merge(left, right)46 47 def _merge(self, left, right):48 index_l = 049 index_r = 050 result = []51 while index_l < len(left) and index_r < len(right):52 if left[index_l] < right[index_r]:53 result.append(left[index_l])54 index_l += 155 else:56 result.append(right[index_r])57 index_r += 158 while index_l < len(left):59 result.append(left[index_l])60 index_l += 161 while index_r < len(right):62 result.append(right[index_r])63 index_r += 164 return result65class QuickSort(object):66 def quick_sort(self, list_in):67 if list_in is None:68 raise TypeError('input can not be None')69 return self._quick_sort(list_in)70 def _quick_sort(self, list_in):71 if len(list_in) < 2:72 return list_in73 left = []74 right = []75 equal = []76 pivot = list_in[len(list_in)//2]77 for element in list_in:78 if element == pivot:79 equal.append(element)80 elif element > pivot:81 right.append(element)82 else:83 left.append(element)84 if len(left)>len(right):85 _left = self._quick_sort(left)86 _right = self._quick_sort(right)87 else:88 _right = self._quick_sort(right)89 _left = self._quick_sort(left)...
ex119_range_in_list.py
Source:ex119_range_in_list.py
1'''2range_in_list([1,2,3,4],0,2) # 63range_in_list([1,2,3,4],0,3) # 104range_in_list([1,2,3,4],1) # 95range_in_list([1,2,3,4]) # 106range_in_list([1,2,3,4],0,100) # 107range_in_list([],0,1) # 08UDEMY9def range_in_list(lst, start=0, end=None):10 end = end or lst[-1]11 return sum(lst[start:end+1])12'''13def range_in_list(list_in, ind_st=0, ind_end=''):14 sum_bet_ind = 015 # print(len(list_in))16 if ind_end == '' or ind_end > len(list_in)-1:17 ind_end = len(list_in)-118 print("{} for {} and {}".format(list_in,ind_st,ind_end) )19 20 for i in range(ind_st, ind_end+1):21 sum_bet_ind += list_in[i]22 23 return sum_bet_ind24 #return [ sum(list_in[list_item]) for list_item in range(list_in[ind_st],list_in[ind_end]) ]25print(range_in_list([1,2,3,4],0,2)) # 626print(range_in_list([1,2,3,4],0,3)) # 1027print(range_in_list([1,2,3,4],1)) # 928print(range_in_list([1,2,3,4])) # 1029print(range_in_list([1,2,3,4],0,100)) # 10...
0_4.py
Source:0_4.py
1"""2Python program to remove an empty element from a list.3"""4list_in = ['Hello', 34, 45, '', 40]5# result = ['Hello', 34, 45, 40]6for item in list_in:7 if not item:8 list_in.remove(item)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!