def rearrange_digits(input_list): """ Rearrange Array Elements so as to form two number such that their sum is maximum. Args: input_list(list): Input List Returns: (int),(int): Two maximum sums """ # Create a max heap of input list O(n) heap = HeapSort(input_list) # Create two output strings, that will represent returned values O(1) first_number = "0" second_number = "0" # Remove max value from the heap, and add it to one of the output lists. O(log n) # Then repeat for the second output list. O(log n) # Time complexity of the loop: O(n log n) should_add_to_first_list = True while heap.size() > 0: max_elem = heap.remove_largest() if should_add_to_first_list: first_number += str(max_elem) else: second_number += str(max_elem) should_add_to_first_list = not should_add_to_first_list # Change output strings to numbers and return return [int(first_number), int(second_number)]
def __setitem__(self, key, val): dict.__setitem__(self,key,val) heap = self.__heap if len(heap) > 2 * len(self): self.__heap = [(v, k) for k, v in self.iteritems()] hs.heapsort(self.__heap, len(self.__heap)) else: newPair = (val, key) insertionPoint = len(heap) heap.append(None) while insertionPoint > 0 and newPair < heap[(insertionPoint - 1) // 2]: heap[insertionPoint] = heap[(insertionPoint-1) // 2] insertionPoint = (insertionPoint-1) // 2 heap[insertionPoint] = newPair
def run(): # orig_obj_values = [ "apple", "orange", "banana", "apple", "coconut", "olive", "egg", "banana", "ham", "milk", "coconut", "cheese" ] value_count = 10000 orig_obj_values = np.empty(shape=(value_count)) min = value_count + 1 max = -1 for i in range(value_count): # in c#, random.next(incl, excl), but in python, randint(incl, incl) v = random.randint(0, value_count - 1) if v < min: min = v if v > max: max = v orig_obj_values[i] = int(v) obj_values = np.empty_like(orig_obj_values) obj_values[:] = orig_obj_values algorithms = np.array([ BubbleSort(), SelectionSort(), InsertionSort(), QuickSort(), MergeSort(), CountingSort(min, max), HeapSort(), RadixSort() ]) print("Python") print("STARTED --------------------------------------------") for algorithm in algorithms: # reset input values obj_values[:] = orig_obj_values algorithm.run(obj_values) print(" ") print("STOPPED --------------------------------------------")
def main(): check = Check() create = CreateRandomList() selectionSort = SelectionSort() bubbleSort = BubbleSort() insertionSort = InsertionSort() mergeSort = MergeSort() quickSort = QuickSort() heapSort = HeapSort() while True: system("cls") print(menu1) choice0 = input("Enter a number = ") if choice0 == "1": system("cls") small = int(input("Enter smallest number = ")) big = int(input("Enter biggest number = ")) length = int(input("Enter length = ")) if check.checkLength(length) == True and check.checkSmallBig(small, big) == True: aList = create.createList(small, big, length) print(createdList.format(aList), end="\n\n") while True: print(menu2) choice1 = input("Enter a number = ") if choice1 == "1": tempList = aList.copy() print(createdList.format(tempList), end="\n\n") selectionSort.sort(tempList) input(string1) elif choice1 == "2": tempList = aList.copy() print(createdList.format(tempList), end="\n\n") bubbleSort.sort(tempList) input("PRESS ENTER or ANY INPUT") elif choice1 == "3": tempList = aList.copy() print(createdList.format(tempList), end="\n\n") insertionSort.sort(tempList) input("PRESS ENTER or ANY INPUT") elif choice1 == "4": tempList = aList.copy() print(createdList.format(tempList), end="\n\n") mergeSort.sort(tempList) input("PRESS ENTER or ANY INPUT") elif choice1 == "5": tempList = aList.copy() print(createdList.format(tempList), end="\n\n") quickSort.sort(tempList, 0, len(tempList)-1) input("PRESS ENTER or ANY INPUT") elif choice1 == "6": tempList = aList.copy() print(createdList.format(tempList), end="\n\n") heapSort.sort(tempList) input("PRESS ENTER or ANY INPUT") else: input("PRESS ENTER or ANY INPUT") elif choice0 == "0": quit()
def test_empty_arr(self): hs = HeapSort() arr = [''] arr2 = arr.copy() arr2.sort() self.assertEqual(hs.sort(arr), arr2)
def test_dif_case_string(self): hs = HeapSort() arr = ['A', 'B', 'a', 'b', 'AA'] arr2 = arr.copy() arr2.sort() self.assertEqual(hs.sort(arr), arr2)
def test_negative_integer(self): hs = HeapSort() arr = [-1, -23, -25, -11, -543, -32, -5234, -111111] arr2 = arr.copy() arr2.sort() self.assertEqual(hs.sort(arr), arr2)
def test_integer(self): hs = HeapSort() arr = [1, 2, 3, 11, 12, 13, 111, 913, 25] arr2 = arr.copy() arr2.sort() self.assertEqual(hs.sort(arr), arr2)
def test_string(self): hs = HeapSort() arr = ['arr', 'data', 'string', 'heap_sort', 'sort'] arr2 = arr.copy() arr2.sort() self.assertEqual(hs.sort(arr), arr2)
tree = ET.parse("data.xml") wb = openpyxl.load_workbook('result.xlsx') sheet = wb['MassTask'] sheet['A1'] = 'Длина массива' sheet['B1'] = 'Кол-во итераций' root = tree.getroot() j = 2 for child in root: start_length = int(child.attrib['startLength']) max_length = int(child.attrib['maxLength']) min_element = int(child.attrib['minElement']) max_element = int(child.attrib['maxElement']) diff = float(child.attrib['diff']) while start_length <= max_length: for repeat in range(int(child.attrib['repeat'])): arr = [] for i in range(start_length): arr.append(random.randint(min_element, max_element)) heap_sort = HeapSort() heap_sort.sort(arr) sheet.cell(row=j, column=1).value = len(arr) sheet.cell(row=j, column=2).value = heap_sort.iteration_counter j += 1 if child.attrib['name'] == 'Arithmetic Progression': start_length = int(start_length + diff) else: start_length = int(start_length * diff) wb.save('result.xlsx') print('Результат массовой задачи сохранён в result.xlsx')
# -*- coding: utf-8 -*- #!/usr/bin/env python __author__ = "Andres Mendez-Vazquez" __copyright__ = "Copyright 2017 Sampler Project" __credits__ = ["Andres Mendez-Vazquez"] __license__ = "GPL" __version__ = "1.0.0" __maintainer__ = "Andres Mendez-Vazquez" __email__ = "*****@*****.**" __status__ = "Development" __name__ = "__main__" import numpy as np from HeapSort import HeapSort if __name__ == "__main__": x = np.arange(10, 0, -1, dtype=np.int32) print(x) HObject = HeapSort() HObject.Sort(x) HObject.print_container()