def __sort_command__(*args): list_, start, end = __find_sort_args__(args[1:]) if not list_: print('Не передан сортируемый массив') else: quicksort.sort(list_, None, start, end) print(list_)
def test_range_sorting_with_pivot_item(self): expected = [4, 5, 6, 7, 8, 9, 0, 0, 0, 0] input_data = [6, 5, 7, 4, 8, 9, 0, 0, 0, 0] quicksort.sort(input_data, 6, 0, 5) self.assertEqual(expected, input_data)
def test_sorting(self): expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] input_data = [6, 5, 7, 4, 8, 9, 0, 2, 3, 1] quicksort.sort(input_data) self.assertEqual(expected, input_data)
def test_range_sorting(self): expected = [4, 5, 6, 7, 8, 9, 0, 0, 0, 0] input_data = [6, 5, 7, 4, 8, 9, 0, 0, 0, 0] quicksort.sort(input_data, start=0, end=5) self.assertEqual(expected, input_data)
def test_sorting_with_pivot_item(self): expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] input_data = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] quicksort.sort(input_data, 3) self.assertEqual(expected, input_data)
def sort_by_latitude(): global_list = read_parse_in_file() sort(global_list, compare_ints_lat) out_file = open("cities_latitude.txt", "w") # opens a file for writing for city in global_list: out_file.write(str(city) + "\n") # write the info for each city and then make a new line for the next one
def test_opposite(self): A = [2, 1] qs.sort(A) self.assertEqual(A, [1, 2]) A = [5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5] qs.sort(A) self.assertEqual(A, [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])
def testRandomLargeArray(): """Test quick sort random""" arr = range(1, 100) import random random.shuffle(arr) print arr sort(arr) print arr expectedArr = range(1, 100) assert isEqual(arr, expectedArr)
def median(data): index = len(data) // 2 is_odd = len(data) % 2 > 0 data = quicksort.sort(copy.deepcopy(data)) return data[index] if is_odd else (data[index] + data[index - 1]) / 2
def sort(connection, A): #getting the array and sorting it A = A.split(" ", 1) B = pickle.loads(A[1]) #obtanining the object from serialized input B.x = [int(i) for i in B.x] B.x = quicksort.sort(B.x, len(B.x)) #persorming quicksort x = pickle.dumps(B) #serializing the result connection.send(x) return
def sort(method, isSorted, i): # Tuple that is returned from the getDB() method # ITEM_DATABASE is a dictionary where the keys are integers and the values are an array # upc14_collection is an array of integers ITEM_DATABASE, upc14_collection = getDB(isSorted) while i < len(upc14_collection): if i == 0: break else: del upc14_collection[i] # Tracks the amount of time it takes for the algorithm to run start_time = time.clock() # Checks to see which method the user wants to use if method == 'QUICKSORT': quicksort.sort(upc14_collection) elif method == 'RADIXSORT': radixsort.sort(upc14_collection) elif method == 'HEAPSORT': heapsort.sort(upc14_collection) # Ends the time when the algorithm is finished end_time = time.clock() # Prints the algorithm used and how long it took to compute clock = end_time - start_time print('{} DONE: {} seconds'.format(method, clock)) #Opens the output sorted file and writes to the file to enter the newly sorted data with open('Sorted Database.csv', 'w') as file: #For each upc code, a new line in the file will be written for i in range(0, len(upc14_collection)): #Written in the format: UPC14 CODE, BRAND, PRODUCT NAME file.write('{}, {}, {} \n'.format( ITEM_DATABASE.get(upc14_collection[i])[0], ITEM_DATABASE.get(upc14_collection[i])[1], ITEM_DATABASE.get(upc14_collection[i])[2])) file.close() # OS opens the sorted text file os.system('open Sorted\ Database.csv')
def test_param_sort(array): assert sort(array) == sorted(array) # Fixture Scope # - function(default) - run for every test # - class - run for every class # - module - run for every module # - session - run for session only 1 time
def test_sort_len_small(self): A = [] qs.sort(A) self.assertEqual(A, []) A = [1] qs.sort(A) self.assertEqual(A, [1]) A = [2, 1] qs.sort(A) self.assertEqual(A, [1, 2]) A = [2, 1] qs.sort(A, 0, 0) self.assertEqual(A, [2, 1]) A = [2, 1] qs.sort(A, 0, 1) self.assertEqual(A, [2, 1])
def visualize_cities(): global_list = read_parse_in_file() # sorted by population size sort(global_list, compare_ints_pop) out_file = open("cities_population.txt", "w") # opens a file for writing for city in global_list: out_file.write(str(city) + "\n") # write the info for each city and then make a new line for the next one while not window_closed(): clear img = load_image("world.png") # load the blank world map draw_image(img, 0, 0) # the reference to an object for the image returned by load_image for city in range(0, 50): # uses the most populated 50 cities draw_rectangle((global_list[city].longitude * 2) + 360, (global_list[city].latitude * 2) + 180, 10, 10) # conversion factor request_redraw() sleep(0.5) # slow sleep to show cities individually popping up
def test_sort_no_change(self): A = [1, 2] qs.sort(A) self.assertEqual(A, [1, 2]) A = [1, 2, 3] qs.sort(A) self.assertEqual(A, [1, 2, 3]) A = [1, 2, 3, 4, 5] qs.sort(A) self.assertEqual(A, [1, 2, 3, 4, 5]) A = [1, 2, 3, 4, 5, 6, 7, 8] qs.sort(A) self.assertEqual(A, [1, 2, 3, 4, 5, 6, 7, 8])
def test_sort_partial(self): A = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] qs.sort(A, 1, 3) self.assertEqual(A, [10, 7, 8, 9, 6, 5, 4, 3, 2, 1]) A = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] qs.sort(A, 8, 2) self.assertEqual(A, [10, 9, 8, 7, 6, 5, 4, 3, 1, 2]) A = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] qs.sort(A, 5, 5) self.assertEqual(A, [10, 9, 8, 7, 6, 1, 2, 3, 4, 5])
def test_z_breaking_1(self): A = [1, 3, 2] qs.sort(A) self.assertEqual(A, [1, 2, 3]) A = [0, 1, 3, 2, 4] qs.sort(A, 1, 3) self.assertEqual(A, list(range(5))) A = [0, 4, 1, 3, 2] qs.sort(A) self.assertEqual(A, list(range(5)))
def test_sort_random(self): for _ in range(100): A = list(range(5)) shuffle(A) qs.sort(A) self.assertEqual(A, list(range(5))) A = list(range(10)) shuffle(A) qs.sort(A) self.assertEqual(A, list(range(10))) A = list(range(20)) shuffle(A) qs.sort(A) self.assertEqual(A, list(range(20))) A = list(range(1000)) shuffle(A) qs.sort(A) self.assertEqual(A, list(range(1000)))
def main(): city_list = [] #Initialize an empty list to append to in_file = open("world_cities.txt", "r") #Open the original file for line in in_file: #For each line: t = line.strip() #Strip the line of whitespace before and after it variable_list = t.split( ",") #Split the variables that are separated by commas #Create the object in the city class city = City(variable_list[0], variable_list[1], variable_list[2], int(variable_list[3]), float(variable_list[4]), float(variable_list[5])) city_list.append(city) #Append the reference into the empty list in_file.close() #Close the file out_file_name = open("cities_alpha.txt", "w") #Open output file for name sort(city_list, compare_name) for cities in city_list: out_file_name.write(str(cities) + "\n") #Write the string function for name out_file_name.close() #Close the file out_file_population = open("cities_population.txt", "w") #Open output file for population sort(city_list, compare_population) for cities in city_list: out_file_population.write( str(cities) + "\n") #Write the string function for population out_file_population.close() #Close the file out_file_latitude = open("cities_latitude.txt", "w") #Open output file for latitiude sort(city_list, compare_latitude) for cities in city_list: out_file_latitude.write(str(cities) + "\n") #Write the string function for latitude out_file_latitude.close() #Close the file
def test_long_strings_different_cases_1(self): self.assertEqual(sort(["Aaa", "aba", "Bab", "bba"]), ["Aaa", "Bab", "aba", "bba"])
def test_long_strings(self): self.assertEqual(sort(["aaa", "aba", "aab", "bba"]), ["aaa", "aab", "aba", "bba"])
def test_simple_strings_special_character_and_numbers_and_cases(self): self.assertEqual(sort(["a", "5", "!", "B"]), ["!", "5", "B", "a"])
def test(numbers): print numbers print sort(numbers) print ""
def test_quicksort(self): retlist = quicksort.sort(self.unsorted) self.assertEqual(retlist, self.expected)
def test_empty(self): self.assertEqual(sort([]), [])
def test_long_strings_special_character_different_cases(self): self.assertEqual(sort(["A!", "a!", "@a!"]), ["@a!", "A!", "a!"])
def test_long_strings_different_cases_2(self): self.assertEqual(sort(["bbF", "Aaa", "ABA", "Cba", "Bab", "bba"]), ["ABA", "Aaa", "Bab", "Cba", "bbF", "bba"])
if __name__ == '__main__': # First, lets handle the arguments" parser = argparse.ArgumentParser(description='Sort a huge file.') parser.add_argument('--input', help='File to sort') parser.add_argument('--output', help='Output file') parser.add_argument('--tempfile', help='Temporarily output pattern prefix (default: output)', default='output') parser.add_argument('--splitsize', help='Number of bytes in each split (default: 10000)', type=int, default=10000) args = parser.parse_args() # Let's split up the files in manageable smaller files splitted_files = split_file(args.input, '%s_{0:04d}.txt' % args.tempfile, args.splitsize) # Sort each individual file for split_file in splitted_files: sort(split_file, "%s_sorted" % split_file) splitted_files_sorted = ["%s_sorted" % filename for filename in splitted_files] # Merge all the files together again merge_files(args.output, splitted_files_sorted) # Let's clean up the mess we have temporarily created for filename in splitted_files + splitted_files_sorted: os.remove(filename) # Tada print "success"
def do_quicksort(strings): return quicksort.sort(strings)
def test_long_strings_special_character(self): self.assertEqual(sort(["!", "a!", "@a!"]), ["!", "@a!", "a!"])
def test_random_data(self): data = [random.randint(0,100) for _ in range(10)] sorted_data = copy.copy(data) sorted_data.sort() quicksort.sort(data) self.assertEqual(data, sorted_data)
def test_equal_elements(self): self.assertEqual(sort(["a", "a", "a"]), ["a", "a", "a"])
fields = line.strip().split(',') # separate its contents cities.append(City(fields[0], fields[1], fields[2], int(fields[3]), float(fields[4]), float(fields[5]))) f.close() # done with the file return cities # Write information about City objects to a file. def write_cities(cities, filename): outfile = open(filename, "w") # get ready to write i = 0 while i < len(cities): outfile.write(str(cities[i]) + "\n") # write the next City object i += 1 outfile.close() # done writing cities = load_cities("world_cities.txt") # Sort by name. sort(cities, compare_name) write_cities(cities, "cities_alpha.txt") # Sort by population. sort(cities, compare_population) write_cities(cities, "cities_population.txt") # Sort by latitude sort(cities, compare_latitude) write_cities(cities, "cities_latitude.txt")
def test_one_element_array(self): self.assertEqual(sort(["a"]), ["a"])
def test_simple_strings_different_cases(self): self.assertEqual(sort(["a", "A", "B", "b"]), ["A", "B", "a", "b"])
import random import quicksort li = [] for x in range(1000000): random.randint(0, x) li.append(x) quicksort.sort(li)
def test_simple_strings_special_character(self): self.assertEqual(sort(["a", "A", "!", "B"]), ["!", "A", "B", "a"])
import random import quicksort li = [] for x in range(1000000): random.randint(0,x) li.append(x) quicksort.sort(li)
def test_quicksort(self): quicksort.sort(self.test) self.assertItemsEqual(self.test, self.expected)