def bubbleSort(arr): isSorted = False while not isSorted: isSorted = True for i in range(len(arr) - 1): if arr[i] > arr[i + 1]: # if the current number is greather then the next, swap tem helper.swap(i, i + 1, arr) isSorted = False return arr
def insertionSort(arr): for i in range(1, len(arr)): j = 1 # as long as we are not at the first number and the current number is smaller # then the before number swap the numbers and decrement [j] while j > 0 and arr[j] < arr[j - 1]: helper.swap(j, j - 1, arr) j -= 1 return arr
def selection_sort(unsorted_list): for position in range(len(unsorted_list)): for i in range(position + 1, len(unsorted_list)): # Find the minimum in this range and swap it with the current position # if the found min is lesse than the current item at position minimum = hp.find_minimum(unsorted_list, i, len(unsorted_list)) if minimum[0] < unsorted_list[position]: hp.swap(unsorted_list, position, minimum[1]) return unsorted_list
def selectionSort(arr): currentIdx = 0 while currentIdx < len(arr) - 1: # run until the before last item smallestInx = currentIdx for i in range(currentIdx + 1, len(arr)): if arr[smallestInx] > arr[i]: smallestInx = i helper.swap(currentIdx, smallestInx, arr) currentIdx += 1 return arr
def selection_sort(list_to_sort): step = 0 for i in range(0, len(list_to_sort)): min = i for j in range(i + 1, len(list_to_sort)): if list_to_sort[j] < list_to_sort[min]: min = j swap(list_to_sort, min, i) print('Step {}: {}'.format(step, list_to_sort)) step += 1
def insertion_sort(list_to_sort): step = 0 for i in range(1, len(list_to_sort)): position = i current_value = list_to_sort[i] while position > 0 and list_to_sort[position - 1] > current_value: swap(list_to_sort, position - 1, position) position = position - 1 print('Step {}: {}'.format(step, list_to_sort)) step += 1
def bubble_sort(ARRAY): sorted = False step = 0 while not sorted: sorted = True for i in range(0, len(ARRAY) - 1): if ARRAY[i] > ARRAY[i + 1]: sorted = False swap(ARRAY, i, i + 1) print('Step {}: {}'.format(step, ARRAY)) step += 1
def insertion_sort(array): n = len(array) sortedCount = 1 while sortedCount <= n: # compare all the number from the left sorted array 0->sortedCount-1 for i in range(0, sortedCount): if array[i] > array[sortedCount]: helper.swap(array, i, sortedCount) pass
def KSA(key): """ Key-scheduling algorithm (KSA) :param key: :type key: string """ keylength = len(key) S = list(range(256)) j = 0 for i in range(256): j = (j + S[i] + key[i % keylength]) % 256 swap(S, i, j) return S
def PRGA(S): """ Pseudo-random generation algorithm (PRGA) :param S: :type S: list """ i = 0 j = 0 while True: i = (i + 1) % 256 j = (j + S[i]) % 256 swap(S, i, j) K = S[(S[i] + S[j]) % 256] yield K
def insertion_sort(arr): n = len(arr) for i in range(1, n): next = arr[i] # sortedStartIndex # compare backward with sorted part j = i - 1 while (j >= 0 and arr[j] > next): helper.swap(arr, j + 1, j) j -= 1 return arr
def bubble_sort(unsorted_list): """ Worst case complexity: O(n^2) Best case complexity: O(n) """ flag = 1 while flag: flag = 0 for i in range(len(unsorted_list) - 1): if unsorted_list[i] > unsorted_list[i + 1]: hp.swap(unsorted_list, i, i + 1) flag = 1 return unsorted_list
def bubble_sort(array): # using two indices # find the max in each iteration and put to the end length = len(array) # stop condition: when the max couter == length -1 # i as the sorted counter for i in range(0, length - 1): # pair compare and move forword # will get the max every time, which will be sorted # only need length-1 number to be sorted for j in range(length - 1 - i): # i number has been sorted if (array[j] > array[j + 1]): helper.swap(array, j, j + 1) return array
def weird_thing(arg: Dict[str, str]) -> Dict: """Get a dict and tranform its keys to upper and value lower. Then swapping key with values them. Arguments: arg: A dictionary to transform. Returns: swapped dict with former keys to upper and values to lower. """ build = {} for key, value in arg.items(): build[key.upper()] = value.lower() return swap(build)
def read_lgn(filepath): edges_in_lgn = [] with open(filepath) as lgn_file: lgn_reader = csv.reader(lgn_file) lgn_reader.next() # skip the header row edges_in_lgn = [] for row in lgn_reader: gene_1, gene_2 = helper.swap(row[0], row[1]) edges_in_lgn.append([gene_1, gene_2]) genes_in_lgn = helper.genes_from_edges(edges_in_lgn) return genes_in_lgn, edges_in_lgn
def read_lgn(filepath): edges_in_lgn = [] with open(filepath) as lgn_file: lgn_reader = csv.reader(lgn_file, delimiter=';') lgn_reader.next() # skip the header row edges_in_lgn = [] for row in lgn_reader: try: gene_1, gene_2 = helper.swap(row[0], row[1]) edges_in_lgn.append([gene_1, gene_2]) except IndexError, e: logging.warning("%s is not in the correct format" % row) pass
def create_list_intra_extra(self): print 'create_list_intra_extra' for block in self.blocks: connected_nodes = self.__find_genes_connected_with_LGN(block) # create list_intra # TODO: remove duplicated edge between LGN # since A, B belongs to LGN # then connected_nodes[A] = {[], [B]} # connected_nodes[B] = {[], [A]} # But they're actually the same connection for key in connected_nodes: for LGN_connected_gene in connected_nodes[key][1]: ordered_key = helper.swap(key, LGN_connected_gene) temp_key = helper.encode_edge_key(ordered_key[0], ordered_key[1]) if temp_key in self.list_intra: self.list_intra[temp_key] += 1 else: self.list_intra[temp_key] = 1 # create list_extra for key in connected_nodes: for extra_connected_gene in connected_nodes[key][0]: # TODO: without doing swap, can the code run faster """ I think by placing the LGN gene first, and the extra_connected_gene later, we can always ensure the unique of connection """ temp_key = helper.encode_edge_key(key, extra_connected_gene) if temp_key in self.list_extra: self.list_extra[temp_key][0] += 1 else: self.list_extra[temp_key] = [1, 0] self.__refine_list_extra()
def gap_sort(unsorted_list, gap): for i in range(len(unsorted_list)): if i + gap < len(unsorted_list): if unsorted_list[i] > unsorted_list[i + int(gap)]: hp.swap(unsorted_list, i, i + int(gap))
# printing possible choices for each in array: if 0 in each: i = array.index(each) j = each.index(0) zero_pos = i, j poss_ij = [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)] for each in poss_ij: if not each[0] in range(size) or not each[1] in range(size): poss_ij.remove(each) #making array of possible choices poss_num = [] for each in poss_ij: poss_num.append(array[each[0]][each[1]]) print "possible moves are on numbers :" for each in poss_num: print each, print "" choice = '$$$$' while not choice in poss_num: choice = raw_input('> ') choice = int(choice) print "making move for choice %r" % choice array = swap(choice, array) # printing the board print "after your move" print_board(array) active = not array == end_game if not active: print "Congrats bro!!"