diff --git a/src/abstract_structures/adt/heap/__init__.py b/src/abstract_structures/heap/__init__.py similarity index 100% rename from src/abstract_structures/adt/heap/__init__.py rename to src/abstract_structures/heap/__init__.py diff --git a/src/abstract_structures/adt/heap/find_N_largest_smallest_items_seq.py b/src/abstract_structures/heap/find_N_largest_smallest_items_seq.py similarity index 100% rename from src/abstract_structures/adt/heap/find_N_largest_smallest_items_seq.py rename to src/abstract_structures/heap/find_N_largest_smallest_items_seq.py diff --git a/src/abstract_structures/adt/heap/heapify.py b/src/abstract_structures/heap/heapify.py similarity index 100% rename from src/abstract_structures/adt/heap/heapify.py rename to src/abstract_structures/heap/heapify.py diff --git a/src/abstract_structures/adt/heap/merge_sorted_seqs.py b/src/abstract_structures/heap/merge_sorted_seqs.py similarity index 100% rename from src/abstract_structures/adt/heap/merge_sorted_seqs.py rename to src/abstract_structures/heap/merge_sorted_seqs.py diff --git a/src/abstract_structures/adt/heap/priority_queue.py b/src/abstract_structures/heap/priority_queue.py similarity index 100% rename from src/abstract_structures/adt/heap/priority_queue.py rename to src/abstract_structures/heap/priority_queue.py diff --git a/src/abstract_structures/adt/linked_lists/__init__.py b/src/abstract_structures/linked_list/__init__.py similarity index 100% rename from src/abstract_structures/adt/linked_lists/__init__.py rename to src/abstract_structures/linked_list/__init__.py diff --git a/src/abstract_structures/adt/linked_lists/linked_list_from_stack.py b/src/abstract_structures/linked_list/linked_list_from_stack.py similarity index 100% rename from src/abstract_structures/adt/linked_lists/linked_list_from_stack.py rename to src/abstract_structures/linked_list/linked_list_from_stack.py diff --git a/src/abstract_structures/adt/linked_lists/linkedlist.py b/src/abstract_structures/linked_list/linkedlist.py similarity index 100% rename from src/abstract_structures/adt/linked_lists/linkedlist.py rename to src/abstract_structures/linked_list/linkedlist.py diff --git a/src/abstract_structures/adt/linked_lists/node.py b/src/abstract_structures/linked_list/node.py similarity index 100% rename from src/abstract_structures/adt/linked_lists/node.py rename to src/abstract_structures/linked_list/node.py diff --git a/src/abstract_structures/adt/linked_lists/ordered_list.py b/src/abstract_structures/linked_list/ordered_list.py similarity index 100% rename from src/abstract_structures/adt/linked_lists/ordered_list.py rename to src/abstract_structures/linked_list/ordered_list.py diff --git a/src/abstract_structures/adt/queues/__init__.py b/src/abstract_structures/queues/__init__.py similarity index 100% rename from src/abstract_structures/adt/queues/__init__.py rename to src/abstract_structures/queues/__init__.py diff --git a/src/abstract_structures/adt/queues/deque.py b/src/abstract_structures/queues/deque.py similarity index 100% rename from src/abstract_structures/adt/queues/deque.py rename to src/abstract_structures/queues/deque.py diff --git a/src/abstract_structures/adt/queues/linked_queue.py b/src/abstract_structures/queues/linked_queue.py similarity index 100% rename from src/abstract_structures/adt/queues/linked_queue.py rename to src/abstract_structures/queues/linked_queue.py diff --git a/src/abstract_structures/adt/queues/palindrome_checker_with_deque.py b/src/abstract_structures/queues/palindrome_checker_with_deque.py similarity index 100% rename from src/abstract_structures/adt/queues/palindrome_checker_with_deque.py rename to src/abstract_structures/queues/palindrome_checker_with_deque.py diff --git a/src/abstract_structures/adt/queues/queue.py b/src/abstract_structures/queues/queue.py similarity index 100% rename from src/abstract_structures/adt/queues/queue.py rename to src/abstract_structures/queues/queue.py diff --git a/src/abstract_structures/adt/queues/queue_from_two_stacks.py b/src/abstract_structures/queues/queue_from_two_stacks.py similarity index 100% rename from src/abstract_structures/adt/queues/queue_from_two_stacks.py rename to src/abstract_structures/queues/queue_from_two_stacks.py diff --git a/src/abstract_structures/adt/queues/rotating_array.py b/src/abstract_structures/queues/rotating_array.py similarity index 100% rename from src/abstract_structures/adt/queues/rotating_array.py rename to src/abstract_structures/queues/rotating_array.py diff --git a/src/abstract_structures/adt/stacks/__init__.py b/src/abstract_structures/stacks/__init__.py similarity index 100% rename from src/abstract_structures/adt/stacks/__init__.py rename to src/abstract_structures/stacks/__init__.py diff --git a/src/abstract_structures/adt/stacks/banlance_parenthesis_str_stack.py b/src/abstract_structures/stacks/banlance_parenthesis_str_stack.py similarity index 100% rename from src/abstract_structures/adt/stacks/banlance_parenthesis_str_stack.py rename to src/abstract_structures/stacks/banlance_parenthesis_str_stack.py diff --git a/src/abstract_structures/adt/stacks/dec2bin_with_stack.py b/src/abstract_structures/stacks/dec2bin_with_stack.py similarity index 100% rename from src/abstract_structures/adt/stacks/dec2bin_with_stack.py rename to src/abstract_structures/stacks/dec2bin_with_stack.py diff --git a/src/abstract_structures/adt/stacks/linked_stack.py b/src/abstract_structures/stacks/linked_stack.py similarity index 100% rename from src/abstract_structures/adt/stacks/linked_stack.py rename to src/abstract_structures/stacks/linked_stack.py diff --git a/src/abstract_structures/adt/stacks/reverse_string_with_stack.py b/src/abstract_structures/stacks/reverse_string_with_stack.py similarity index 100% rename from src/abstract_structures/adt/stacks/reverse_string_with_stack.py rename to src/abstract_structures/stacks/reverse_string_with_stack.py diff --git a/src/abstract_structures/adt/stacks/set_of_stacks.py b/src/abstract_structures/stacks/set_of_stacks.py similarity index 100% rename from src/abstract_structures/adt/stacks/set_of_stacks.py rename to src/abstract_structures/stacks/set_of_stacks.py diff --git a/src/abstract_structures/adt/stacks/stack.py b/src/abstract_structures/stacks/stack.py similarity index 100% rename from src/abstract_structures/adt/stacks/stack.py rename to src/abstract_structures/stacks/stack.py diff --git a/src/abstract_structures/adt/stacks/stack_with_min.py b/src/abstract_structures/stacks/stack_with_min.py similarity index 100% rename from src/abstract_structures/adt/stacks/stack_with_min.py rename to src/abstract_structures/stacks/stack_with_min.py diff --git a/src/graphs_and_trees/trees_graphs/binary_search_tree.py b/src/graphs_and_trees/trees_basics/binary_search_tree.py similarity index 100% rename from src/graphs_and_trees/trees_graphs/binary_search_tree.py rename to src/graphs_and_trees/trees_basics/binary_search_tree.py diff --git a/src/graphs_and_trees/trees_graphs/binary_tree.py b/src/graphs_and_trees/trees_basics/binary_tree.py similarity index 100% rename from src/graphs_and_trees/trees_graphs/binary_tree.py rename to src/graphs_and_trees/trees_basics/binary_tree.py diff --git a/src/graphs_and_trees/trees_graphs/traversals.py b/src/graphs_and_trees/trees_basics/traversals.py similarity index 100% rename from src/graphs_and_trees/trees_graphs/traversals.py rename to src/graphs_and_trees/trees_basics/traversals.py diff --git a/src/searching_and_sorting/searching/binary_search.py b/src/searching_and_sorting/searching/binary_search.py index 404a59e..9bca2f4 100644 --- a/src/searching_and_sorting/searching/binary_search.py +++ b/src/searching_and_sorting/searching/binary_search.py @@ -1,24 +1,24 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" def binary_search(seq, key): ''' binary search iterative algorithm ''' ''' observe that the index is returned ''' - hi = len(seq) + hi = len(seq) lo = 0 while lo < hi: mid = (hi+lo) // 2 if seq[mid] == key: return mid elif key < seq[mid]: - hi = mid + hi = mid else: lo = mid + 1 - return None - + def binary_search_rec(seq, key, lo=0, hi=None): ''' binary search recursive algorithm ''' diff --git a/src/searching_and_sorting/searching_adv/binary_search_matrix.py b/src/searching_and_sorting/searching/binary_search_matrix.py similarity index 87% rename from src/searching_and_sorting/searching_adv/binary_search_matrix.py rename to src/searching_and_sorting/searching/binary_search_matrix.py index 70a3f8a..5408e93 100644 --- a/src/searching_and_sorting/searching_adv/binary_search_matrix.py +++ b/src/searching_and_sorting/searching/binary_search_matrix.py @@ -1,9 +1,11 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + ''' Searches an element in a matrix where in every row, the values are increasing from left to right, but the last number in a row is smaller than the first number in the next row. - + (1) The naive brute force solution (sequential search) scan all numbers and cost O(nm). However, since the numbers are already sorted, the matrix can be viewed as a 1D sorted array. The binary search algorithm is suitable. The efficiency is O(logmn). >>> m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] @@ -12,20 +14,20 @@ >>> binary_search_matrix_rec(m, 12) >>> binary_search_matrix_iter(m, 6) (1, 2) - >>> binary_search_matrix_iter(m, 12) + >>> binary_search_matrix_iter(m, 12) >>> binary_search_matrix_iter(m, 1) (0, 0) - + (2) Another solution is "discarding" arrays in the way. The efficiency is O(logm). >>> m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> searching_matrix(m, 6) (1, 2) - >>> searching_matrix(m, 12) - -''' - + >>> searching_matrix(m, 12) -def binary_search_matrix_rec(m, key, lo=0, hi=None): +''' + + +def binary_search_matrix_rec(m, key, lo=0, hi=None): if not m: return None rows = len(m) cols = len(m[0]) @@ -47,7 +49,7 @@ def binary_search_matrix_iter(m, key): rows = len(m) cols = len(m[0]) lo, hi = 0, rows*cols - while lo < hi: + while lo < hi: mid = (hi + lo)//2 row = mid//rows col = mid%rows @@ -62,14 +64,14 @@ def searching_matrix(m, key): if not m: return None rows = len(m) cols = len(m[0]) - i, j = 0, cols -1 + i, j = 0, cols -1 while i < rows and j > 0: item = m[i][j] if key == item: return (i, j) elif key < item: j -= 1 else: i += 1 return None - + if __name__ == '__main__': import doctest diff --git a/src/searching_and_sorting/searching_adv/find_item_rotated_sorted_array.py b/src/searching_and_sorting/searching/find_item_rotated_sorted_array.py similarity index 62% rename from src/searching_and_sorting/searching_adv/find_item_rotated_sorted_array.py rename to src/searching_and_sorting/searching/find_item_rotated_sorted_array.py index e8a24fb..26024d2 100644 --- a/src/searching_and_sorting/searching_adv/find_item_rotated_sorted_array.py +++ b/src/searching_and_sorting/searching/find_item_rotated_sorted_array.py @@ -1,33 +1,19 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + + ''' Given a sorted array that was rotated, find an item with binary search: - >>> l1 = [3, 4, 5, 6, 7, 1, 2] - >>> find_element_rot_array(l1, 7) - 4 - >>> find_element_rot_array(l1, 3) - 0 - >>> find_element_rot_array(l1, 4) - 1 - >>> find_element_rot_array(l1, 5) - 2 - >>> find_element_rot_array(l1, 6) - 3 - >>> find_element_rot_array(l1, 1) - 5 - >>> find_element_rot_array(l1, 2) - 6 - >>> find_element_rot_array(l1, 8) - ''' def find_element_rot_array(seq, key, lo=0, hi=None): hi = hi or len(seq) if hi <= lo: return None # base case: <= for odd and even numbers! - mid = (hi + lo) // 2 + mid = (hi + lo) // 2 if key == seq[mid]: return mid - + # if left is ordered --> we work here if seq[lo] <= seq[mid]: # now, is the key there? @@ -36,7 +22,7 @@ def find_element_rot_array(seq, key, lo=0, hi=None): else: # all the other cases return find_element_rot_array(seq, key, mid+1, hi) - + # right is ordered --> we work here else: # now, is the key there? @@ -46,11 +32,13 @@ def find_element_rot_array(seq, key, lo=0, hi=None): # all the other cases return find_element_rot_array(seq, key, lo, mid) - - - + +def test_find_element_rot_array(): + l1 = [3, 4, 5, 6, 7, 1, 2] + assert(find_element_rot_array(l1, 7) == 4 ) + print("Tests passed!") + if __name__ == '__main__': - import doctest - doctest.testmod() + test_find_element_rot_array() diff --git a/src/searching_and_sorting/searching/find_max_unimodal_array.py b/src/searching_and_sorting/searching/find_max_unimodal_array.py index 04c07c5..c38215e 100644 --- a/src/searching_and_sorting/searching/find_max_unimodal_array.py +++ b/src/searching_and_sorting/searching/find_max_unimodal_array.py @@ -1,6 +1,8 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + def find_max_unimodal_array(A): @@ -19,7 +21,7 @@ def find_max_unimodal_array(A): def test_find_max_unimodal_array(): - seq = [1, 2, 5, 6, 7, 10, 12, 9, 8, 7, 6] + seq = [1, 2, 5, 6, 7, 10, 12, 9, 8, 7, 6] assert(find_max_unimodal_array(seq) == 12) print('Tests passed!') @@ -28,5 +30,5 @@ if __name__ == '__main__': test_find_max_unimodal_array() - - + + diff --git a/src/searching_and_sorting/searching/find_sqrt_bin_search.py b/src/searching_and_sorting/searching/find_sqrt_bin_search.py index 99fec1c..84fcfd1 100644 --- a/src/searching_and_sorting/searching/find_sqrt_bin_search.py +++ b/src/searching_and_sorting/searching/find_sqrt_bin_search.py @@ -1,6 +1,9 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + + diff --git a/src/searching_and_sorting/searching_adv/find_str_array_with_empty_str.py b/src/searching_and_sorting/searching/find_str_array_with_empty_str.py similarity index 59% rename from src/searching_and_sorting/searching_adv/find_str_array_with_empty_str.py rename to src/searching_and_sorting/searching/find_str_array_with_empty_str.py index 6ccdc9b..90b1245 100644 --- a/src/searching_and_sorting/searching_adv/find_str_array_with_empty_str.py +++ b/src/searching_and_sorting/searching/find_str_array_with_empty_str.py @@ -1,21 +1,13 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + ''' Given a sorted an array with empty strings, we use binary search to find some string (since the list is sorted): --> we deal with the empty strings with strip and then run to left and right, or move mid to the closed non-empty str (remember that the index must be conserved): - >>> l1 = ['acre', 'ball', '', 'coach', '', 'cut', ''] - >>> find_str_array_with_empty_str(l1, l1[0]) - 0 - >>> find_str_array_with_empty_str(l1, l1[1]) - 1 - >>> find_str_array_with_empty_str(l1, l1[3]) - 3 - >>> find_str_array_with_empty_str(l1, l1[5]) - 5 - >>> find_str_array_with_empty_str(l1, 'bla') ''' @@ -25,28 +17,33 @@ def find_str_array_with_empty_str(seq, s1): lo = 0 while hi > lo: mid = (hi+lo)//2 - + if seq[mid] == '': while True: left = mid-1 right = mid+1 if left < lo or right > hi: return None - elif right < hi and seq[right]: + elif right < hi and seq[right]: mid = right break - elif left > lo and seq[left]: + elif left > lo and seq[left]: mid = left break right += 1 left -= 1 - + if s1 == seq[mid] == s1: return mid - elif s1 < seq[mid]: hi = mid + elif s1 < seq[mid]: hi = mid else: lo = mid + 1 - - + + + +def test_find_str_array_with_empty_str(): + seq = ['acre', 'ball', '', 'coach', '', 'cut', ''] + key = seq[1] + assert(find_str_array_with_empty_str(seq, key) == 1) + print('Tests passed!') + if __name__ == '__main__': - import doctest - doctest.testmod() - + test_find_str_array_with_empty_str() diff --git a/src/searching_and_sorting/searching/find_time_occurence_list.py b/src/searching_and_sorting/searching/find_time_occurence_list.py index 17c3137..291fdb9 100644 --- a/src/searching_and_sorting/searching/find_time_occurence_list.py +++ b/src/searching_and_sorting/searching/find_time_occurence_list.py @@ -1,6 +1,7 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" def binary_serch_counting(lst1, k, lo=0, hi=None): @@ -15,26 +16,26 @@ def binary_serch_counting(lst1, k, lo=0, hi=None): else: return mid return -1 - - + + def find_time_occurrence_list(seq, k): - """ find how many times a k element appears in a sorted list. One way of doing this is using - collections.OrderedDict to no mess with the sorting, and add entries for every count. This + """ find how many times a k element appears in a sorted list. One way of doing this is using + collections.OrderedDict to no mess with the sorting, and add entries for every count. This should be O(n). It has a O(1) space complexity since the size of the dict is fixed. - Another way, since the array is sorted, it to use binary search, since this is only O(logn). + Another way, since the array is sorted, it to use binary search, since this is only O(logn). """ index_some_k = binary_serch_counting(seq, k) count = 1 sizet = len(seq) - + for i in range(index_some_k+1, sizet): # go up if seq[i] == k: count +=1 - else: break - + else: break + for i in range(index_some_k-1, -1, -1): # go down if seq[i] == k: count +=1 - else: break - + else: break + return count diff --git a/src/searching_and_sorting/searching/interserction_two_arrays.py b/src/searching_and_sorting/searching/interserction_two_arrays.py index 0fa91cf..485634a 100644 --- a/src/searching_and_sorting/searching/interserction_two_arrays.py +++ b/src/searching_and_sorting/searching/interserction_two_arrays.py @@ -1,6 +1,9 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + + ''' using sets ''' @@ -9,9 +12,9 @@ def intersection_two_arrays_sets(seq1, seq2): set1 = set(seq1) set2 = set(seq2) return set1.intersection(set2) #same as list(set1 & set2 - - - + + + ''' using merge sort ''' def intersection_two_arrays_ms(seq1, seq2): @@ -23,19 +26,19 @@ def intersection_two_arrays_ms(seq1, seq2): seq2.pop() elif seq1[-1] > seq2[-1]: seq1.pop() - else: + else: seq2.pop() res.reverse() return res - - - - + + + + ''' using binary search ''' - + def binary_search(seq, key, lo=0, hi=None): ''' binary search iterative algorithm ''' - hi = hi or len(seq) + hi = hi or len(seq) while lo < hi: mid = (hi+lo) // 2 if seq[mid] == key: @@ -43,31 +46,31 @@ def binary_search(seq, key, lo=0, hi=None): elif key > seq[mid]: lo = mid + 1 else: - hi = mid + hi = mid return None - + def intersection_two_arrays_bs(seq1, seq2): ''' if A small and B is too large, we can do a binary search on each entry in B ''' ''' only works if sorted and the small sequence has not larger nmbers!!!''' if len(seq1) > len(seq2): seq, key = seq1, seq2 else: seq, key = seq2, seq1 - + intersec = [] for item in key: - if binary_search(seq, item): - intersec.append(item) - return intersec - + if binary_search(seq, item): + intersec.append(item) + return intersec + def test_intersection_two_arrays(module_name='this module'): seq1 = [1,2,3,5,7,8] seq2 = [3,5,6] - + assert(set(intersection_two_arrays_sets(seq1,seq2)) == set([3,5])) assert(intersection_two_arrays_bs(seq1,seq2) == [3,5]) assert(intersection_two_arrays_ms(seq1,seq2) == [3,5]) - + s = 'Tests in {name} have {con}!' print(s.format(name=module_name, con='passed')) diff --git a/src/searching_and_sorting/searching/ordered_sequential_search.py b/src/searching_and_sorting/searching/ordered_sequential_search.py index a252986..4f799d8 100644 --- a/src/searching_and_sorting/searching/ordered_sequential_search.py +++ b/src/searching_and_sorting/searching/ordered_sequential_search.py @@ -1,6 +1,9 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + + def ordered_sequential_search(seq, n): @@ -19,7 +22,7 @@ def test_ordered_sequential_search(module_name='this module'): n2 = 7 assert(ordered_sequential_search(seq, n1) == True) assert(ordered_sequential_search(seq, n2) == False) - + s = 'Tests in {name} have {con}!' print(s.format(name=module_name, con='passed')) diff --git a/src/searching_and_sorting/searching/searching_in_a_matrix.py b/src/searching_and_sorting/searching/searching_in_a_matrix.py index d3cdfbd..7b9c2bb 100644 --- a/src/searching_and_sorting/searching/searching_in_a_matrix.py +++ b/src/searching_and_sorting/searching/searching_in_a_matrix.py @@ -1,16 +1,19 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + + import numpy def searching_in_a_matrix(m1, value): - """ searches an element in a matrix where in every row, the values are increasing from left to + """ searches an element in a matrix where in every row, the values are increasing from left to right, but the last number in a row is smaller than the first number in the next row. The naive brute force solution scan all numbers and cost O(nm). However, since the numbers are already sorted, the matrix can be viewed as a 1D sorted array. The binary search algorithm is suitable. The efficience is O(logmn).""" - + rows = len(m1) cols = len(m1[0]) lo = 0 @@ -24,7 +27,7 @@ def searching_in_a_matrix(m1, value): elif v > value: hi = mid else: lo = mid+1 return False - + def test_searching_in_a_matrix(): diff --git a/src/searching_and_sorting/searching/sequential_search.py b/src/searching_and_sorting/searching/sequential_search.py index 9cc2947..cb876cb 100644 --- a/src/searching_and_sorting/searching/sequential_search.py +++ b/src/searching_and_sorting/searching/sequential_search.py @@ -1,6 +1,8 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + def sequential_search(seq, n): @@ -16,7 +18,7 @@ def test_sequential_search(module_name='this module'): n2 = 7 assert(sequential_search(seq, n1) == True) assert(sequential_search(seq, n2) == False) - + s = 'Tests in {name} have {con}!' print(s.format(name=module_name, con='passed')) @@ -29,4 +31,4 @@ if __name__ == '__main__': Case Best Case Worst Case Average Case item is present 1 n n2 item is not present n n n -"""" +""" \ No newline at end of file diff --git a/src/searching_and_sorting/searching_adv/binary_search.py b/src/searching_and_sorting/searching_adv/binary_search.py deleted file mode 100644 index b942f79..0000000 --- a/src/searching_and_sorting/searching_adv/binary_search.py +++ /dev/null @@ -1,112 +0,0 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail - -''' A recursive and an iterative example of binary search in Python. - Remember: sequence must be sorted! You can return True/False or the index: - >>> l1 = [1, 2, 3, 4, 5, 6, 7] - >>> binary_search_rec(l1, 1) - 0 - >>> binary_search_rec(l1, 2) - 1 - >>> binary_search_rec(l1, 3) - 2 - >>> binary_search_rec(l1, 4) - 3 - >>> binary_search_rec(l1, 5) - 4 - >>> binary_search_rec(l1, 6) - 5 - >>> binary_search_rec(l1, 7) - 6 - >>> binary_search_rec(l1, 8) - >>> l1 = [1, 2, 3, 4, 5, 6] - >>> binary_search_rec(l1, 1) - 0 - >>> binary_search_rec(l1, 2) - 1 - >>> binary_search_rec(l1, 3) - 2 - >>> binary_search_rec(l1, 4) - 3 - >>> binary_search_rec(l1, 5) - 4 - >>> binary_search_rec(l1, 6) - 5 - >>> binary_search_rec(l1, 7) - >>> l1 = [1, 2, 3, 4, 5, 6, 7] - >>> binary_search_iter(l1, 1) - 0 - >>> binary_search_iter(l1, 2) - 1 - >>> binary_search_iter(l1, 3) - 2 - >>> binary_search_iter(l1, 4) - 3 - >>> binary_search_iter(l1, 5) - 4 - >>> binary_search_iter(l1, 6) - 5 - >>> binary_search_iter(l1, 7) - 6 - >>> binary_search_iter(l1, 8) - >>> l1 = [1, 2, 3, 4, 5, 6] - >>> binary_search_iter(l1, 1) - 0 - >>> binary_search_iter(l1, 2) - 1 - >>> binary_search_iter(l1, 3) - 2 - >>> binary_search_iter(l1, 4) - 3 - >>> binary_search_iter(l1, 5) - 4 - >>> binary_search_iter(l1, 6) - 5 - >>> binary_search_iter(l1, 7) -''' - - -def binary_search_iter(seq, key): - hi, lo = len(seq), 0 - while lo < hi: # here is self.data[i]) and left or i largest = (right < n and self.data[right] > self.data[largest]) and right or largest if i != largest: diff --git a/src/searching_and_sorting/sorting/heap_sort.py b/src/searching_and_sorting/sorting/heap_sort.py new file mode 100644 index 0000000..38c2ff5 --- /dev/null +++ b/src/searching_and_sorting/sorting/heap_sort.py @@ -0,0 +1,85 @@ +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + + +''' Heapsort using Pythons libraries''' + +import heapq + +def heap_sort1(seq): + ''' heap sort with Python's heapq ''' + h = [] + for value in seq: + heapq.heappush(h, value) + return [heapq.heappop(h) for i in range(len(h))] + + +def test_heap_sort1(): + seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2] + assert(heap_sort1(seq) == sorted(seq)) + print('Tests passed!') + + + +''' Heapsort using my Heap class ''' + +from heap import Heap + +def heap_sort2(seq): + heap = Heap(seq) + + res = [] + for i in range(len(seq)): + res.insert(0, heap.extract_max()) + + return res + + +def test_heap_sort2(): + seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2] + print heap_sort2(seq) + print('Tests passed!') + + +''' A third way of doing heap sort ''' + +def heap_sort3(seq): + for start in range((len(seq)-2)//2, -1, -1): + siftdown(seq, start, len(seq)-1) + for end in range(len(seq)-1, 0, -1): + seq[end], seq[0] = seq[0], seq[end] + siftdown(seq, 0, end - 1) + return seq + +def siftdown(seq, start, end): + root = start + while True: + child = root * 2 + 1 + if child > end: break + if child + 1 <= end and seq[child] < seq[child + 1]: + child += 1 + if seq[root] < seq[child]: + seq[root], seq[child] = seq[child], seq[root] + root = child + else: + break + + + +def test_heap_sort3(): + seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2] + assert(heap_sort3(seq) == sorted(seq)) + print('Tests passed!') + + + + + + + +if __name__ == '__main__': + test_heap_sort1() + test_heap_sort2() + test_heap_sort3() diff --git a/src/searching_and_sorting/sorting/heap_sort1.py b/src/searching_and_sorting/sorting/heap_sort1.py deleted file mode 100644 index 7f174bb..0000000 --- a/src/searching_and_sorting/sorting/heap_sort1.py +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail - -import heapq - -def heap_sort1(seq): - ''' heap sort with Python's heapq ''' - h = [] - for value in seq: - heapq.heappush(h, value) - return [heapq.heappop(h) for i in range(len(h))] - - -def test_heap_sort1(): - seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2] - assert(heap_sort1(seq) == sorted(seq)) - print('Tests passed!') - - -if __name__ == '__main__': - test_heap_sort1() - diff --git a/src/searching_and_sorting/sorting/heap_sort2.py b/src/searching_and_sorting/sorting/heap_sort2.py deleted file mode 100644 index e7df361..0000000 --- a/src/searching_and_sorting/sorting/heap_sort2.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail - -from heap import Heap - -def heap_sort2(seq): - heap = Heap(seq) - - res = [] - for i in range(len(seq)): - res.insert(0, heap.extract_max()) - - return res - - -def test_heap_sort2(): - seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2] - assert(heap_sort2(seq) == sorted(seq)) - print('Tests passed!') - - -if __name__ == '__main__': - test_heap_sort2() diff --git a/src/searching_and_sorting/sorting/heap_sort3.py b/src/searching_and_sorting/sorting/heap_sort3.py deleted file mode 100644 index 9e4ff2a..0000000 --- a/src/searching_and_sorting/sorting/heap_sort3.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail - -def heap_sort3(seq): - for start in range((len(seq)-2)//2, -1, -1): - siftdown(seq, start, len(seq)-1) - for end in range(len(seq)-1, 0, -1): - seq[end], seq[0] = seq[0], seq[end] - siftdown(seq, 0, end - 1) - return seq - -def siftdown(seq, start, end): - root = start - while True: - child = root * 2 + 1 - if child > end: break - if child + 1 <= end and seq[child] < seq[child + 1]: - child += 1 - if seq[root] < seq[child]: - seq[root], seq[child] = seq[child], seq[root] - root = child - else: - break - - - -def test_heap_sort(): - seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2] - assert(heap_sort3(seq) == sorted(seq)) - print('Tests passed!') - - -if __name__ == '__main__': - test_heap_sort3() - diff --git a/src/searching_and_sorting/sorting/insertion_sort.py b/src/searching_and_sorting/sorting/insertion_sort.py index 3dd522e..e67130c 100644 --- a/src/searching_and_sorting/sorting/insertion_sort.py +++ b/src/searching_and_sorting/sorting/insertion_sort.py @@ -1,6 +1,8 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + @@ -38,7 +40,7 @@ if __name__ == '__main__': - - - - + + + + diff --git a/src/searching_and_sorting/sorting/merge_sort.py b/src/searching_and_sorting/sorting/merge_sort.py index 46a27dd..d234bde 100644 --- a/src/searching_and_sorting/sorting/merge_sort.py +++ b/src/searching_and_sorting/sorting/merge_sort.py @@ -1,25 +1,40 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + + +''' Some examples of how to implement Merge Sort in Python + --> RUNTIME: WORST/BEST/AVERAGE Is O(nlogn) + --> space complexity is O(n) for arrays + --> not in place, good for large arrays + >>> seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2] + >>> merge_sort(seq) == sorted(seq) + True + >>> seq2 = [3, 3, 3, 3, 3, 3, 3, 3] + >>> merge_sort(seq2) == sorted(seq2) + True + >>> seq3 = [] + >>> merge_sort(seq3) == sorted(seq3) + True +''' - def merge_sort(seq): if len(seq) <= 1: return seq - mid = len(seq)//2 + mid = len(seq)//2 lft, rgt = seq[:mid], seq[mid:] if len(lft)>1: lft = merge_sort(lft) if len(rgt)>1: rgt = merge_sort(rgt) - + res = [] - while lft and rgt: + while lft and rgt: if lft [-1]>= rgt[-1]: res.append(lft.pop()) - else: + else: res.append(rgt.pop()) res.reverse() - return(lft or rgt) + res - + return(lft or rgt) + res @@ -29,5 +44,57 @@ def test_merge_sort(): print('Tests passed!') +''' + We could also divide this sort into two parts, separating + the merge part in another function +''' + +def merge_sort_sep(seq): + if len(seq) < 2 : return seq # base case + mid = len(seq)//2 + left, right = None, None # we could have declared the arrays here, + # but this would allocate unecessary extra space + if seq[:mid]: left = merge_sort(seq[:mid]) + if seq[mid:]: right = merge_sort(seq[mid:]) # notice that mid is included! + + return merge(left, right) # merge iteratively + +def merge(left, right): + if not left or not right: return left or right # nothing to be merged + result = [] + i, j = 0, 0 + while i < len(left) and j < len(right): + if left[i] <= right[j]: + result.append(left[i]) + i += 1 + else: + result.append(right[j]) + j += 1 + if left[i:] : result.extend(left[i:]) # REMEMBER TO TO ENXTEND NOT APPEND + if right[j:] : result.extend(right[j:]) + return result + + + + +''' The two following merge functions are O(2n)=O(n) and O(n) respectively. They + illustrate many features in Python that ''' +def merge_2n(left, right): + if not left or not right: return left or right # nothing to be merged + result = [] + while left and right: + if left[-1] >= right[-1]: + result.append(left.pop()) + else: + result.append(right.pop()) + result.reverse() + return (left or right) + result + + + + + + + if __name__ == '__main__': test_merge_sort() diff --git a/src/searching_and_sorting/sorting_adv/merge_sorted_things.py b/src/searching_and_sorting/sorting/merge_sorted_things.py similarity index 90% rename from src/searching_and_sorting/sorting_adv/merge_sorted_things.py rename to src/searching_and_sorting/sorting/merge_sorted_things.py index fb935df..c2cc384 100644 --- a/src/searching_and_sorting/sorting_adv/merge_sorted_things.py +++ b/src/searching_and_sorting/sorting/merge_sorted_things.py @@ -1,6 +1,7 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" ''' --> In the case of two arrays: we can merge two arrays using the merge function from the merge sort --> we can do this for files too, merging each two @@ -16,27 +17,27 @@ result.append(right.pop()) result.reverse() return (left or right) + result - - + + 2) If we can't modify or we want to in place, we need two pointers: >>> l1 = [1, 2, 3, 4, 5, 6, 7] >>> l2 = [2, 4, 5, 8] >>> merge(l1, l2) [1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 8] - - + + 3) For example, in the case we have a big array filled 0s in the end, and another array with the size of the number of 0s: >>> l1 = [1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0] >>> l2 = [2, 4, 5, 8] >>> merge_two_arrays_inplace(l1, l2) [1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 8] - - + + 4) If we want to merge sorted files (and we have plenty of RAM to load all files): >>> list_files = ['1.dat', '2.dat', '3.dat'] >>> merge_files(list_files) [1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 7, 8] -''' +''' def merge(left, right): if not left or not right: return left or right # nothing to be merged @@ -54,15 +55,15 @@ def merge(left, right): return result - + def merge_two_arrays_inplace(l1, l2): if not l1 or not l2: return l1 or l2 # nothing to be merged - p2 = len(l2) - 1 - p1 = len(l1) - len(l2) - 1 + p2 = len(l2) - 1 + p1 = len(l1) - len(l2) - 1 p12 = len(l1) - 1 while p2 >= 0 and p1 >= 0: item_to_be_merged = l2[p2] - item_bigger_array = l1[p1] + item_bigger_array = l1[p1] if item_to_be_merged < item_bigger_array: l1[p12] = item_bigger_array p1 -= 1 @@ -81,11 +82,11 @@ def merge_files(list_files): with open(filename, 'r') as file: for line in file: aux.append(int(line)) - result.append(aux) + result.append(aux) final.extend(result.pop()) for l in result: final = merge(l, final) - return final + return final if __name__ == '__main__': diff --git a/src/searching_and_sorting/sorting/quick_sort.py b/src/searching_and_sorting/sorting/quick_sort.py index ee4d0c1..0d25d6a 100644 --- a/src/searching_and_sorting/sorting/quick_sort.py +++ b/src/searching_and_sorting/sorting/quick_sort.py @@ -1,6 +1,27 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" + + +''' Some examples of how to implement Quick Sort in Python + --> RUNTIME: BEST/AVERAGE Is O(nlogn), WORST is O(n2) + --> the first example is not in place, the second is in place + --> test with two element arrays, identical values + + Quick sort in place: + 1) select pivot as the index = 0 + 2) start pointer1 at index = 1 and pointer2 in the last element + 3) while pointer1 < pointer2: + if value in pointer1 <= pivot + swap value in pointer1 with value in pointer2 and advanced pointer2 + else + advance pointer1 + 4) now the array is like this: + [pivot, larger than pivot, smaller than pivot] + 5) swap the pivot where pointer 1 stop + 6) do recursively for [smaller] + [pivot] + [larger] +''' @@ -11,22 +32,52 @@ def quick_sort(seq): left = quick_sort([x for x in seq[1:] if x < pivot]) right = quick_sort([x for x in seq[1:] if x >= pivot]) return left + [pivot] + right - - + + +''' slightly different in the way we get the pivot''' +def quick_sort(seq): + if len(seq) < 2 : return seq + mid = len(seq)//2 + pi = seq[mid] + seq = seq[:mid] + seq[mid+1:] + left = quick_sort([x for x in seq if x <= pi]) # REMEMBER TO INCLUDE X (OR IN RIGHT) + right = quick_sort([x for x in seq if x > pi]) + return left + [pi] + right + + + + """ we can also divide them into two functions """ def partition(seq): - pi,seq = seq[0],seq[1:] - lo = [x for x in seq if x <= pi] - hi = [x for x in seq if x > pi] - return lo, pi, hi + pi,seq = seq[0],seq[1:] + lo = [x for x in seq if x <= pi] + hi = [x for x in seq if x > pi] + return lo, pi, hi def quick_sort_divided(seq): - if len(seq) < 2: return seq - lo, pi, hi = partition(seq) + if len(seq) < 2: return seq + lo, pi, hi = partition(seq) return quick_sort_divided(lo) + [pi] + quick_sort_divided(hi) - +''' quick_sort in place ''' +def quick_sort_in(seq): + if len(seq) < 2 : return seq + if len(seq) == 2 and seq[0] > seq[1]: + seq[0], seq[1] = seq[1], seq[0] # problems when only 2 elements because of swap + pivot = seq[0] # start at the ends because we don't know how many elements + p1, p2 = 1, len(seq) -1 # set pointers at both ends + while p1 < p2: # must be < or out of range + if seq[p1] <= pivot: # must be <= because of pivot swap + seq[p1], seq[p2] = seq[p2], seq[p1] + p2 -= 1 + else: + p1 += 1 + seq[0], seq[p1] = seq[p1], pivot + return quick_sort_in(seq[p1+1:]) + [seq[p1]] + quick_sort_in(seq[:p1]) + + + def test_quick_sort(): seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2] diff --git a/src/searching_and_sorting/sorting/selection_sort.py b/src/searching_and_sorting/sorting/selection_sort.py index 7c6ddac..72478c4 100644 --- a/src/searching_and_sorting/sorting/selection_sort.py +++ b/src/searching_and_sorting/sorting/selection_sort.py @@ -1,6 +1,7 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" def selection_sort(seq): @@ -25,7 +26,7 @@ if __name__ == '__main__': - - - - + + + + diff --git a/src/searching_and_sorting/sorting_adv/sort_anagrams_together.py b/src/searching_and_sorting/sorting/sort_anagrams_together.py similarity index 89% rename from src/searching_and_sorting/sorting_adv/sort_anagrams_together.py rename to src/searching_and_sorting/sorting/sort_anagrams_together.py index c0b5b42..890a7b8 100644 --- a/src/searching_and_sorting/sorting_adv/sort_anagrams_together.py +++ b/src/searching_and_sorting/sorting/sort_anagrams_together.py @@ -1,6 +1,7 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail +#!/usr/bin/python + +__author__ = "Mari Wahl" +__email__ = "marina.w4hl@gmail.com" ''' A method to sort an array so that all the anagrams are together. Since we only want the anagrams to be grouped, we can use a dictionary for this task. This @@ -13,21 +14,21 @@ from collections import defaultdict def sort_anagrams_together(l1): result = [] - + # step 1 save the anagrams together dict_aux = defaultdict(list) # rememebr to indicate the type for word in l1: key = ''.join(sorted(word)) # need to sort the strings and join it dict_aux[key].append(word) # because only sorted give a list of each char - + # step 2 print the anagrams. Note that if you want everything - # sorted you would have to sort the keys and insert the angrams after that + # sorted you would have to sort the keys and insert the angrams after that for key in dict_aux: result.extend(dict_aux[key]) - + return result - - + + if __name__ == '__main__': import doctest diff --git a/src/searching_and_sorting/sorting_adv/merge_sort.py b/src/searching_and_sorting/sorting_adv/merge_sort.py deleted file mode 100644 index 7797b22..0000000 --- a/src/searching_and_sorting/sorting_adv/merge_sort.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail - -''' Some examples of how to implement Merge Sort in Python - --> RUNTIME: WORST/BEST/AVERAGE Is O(nlogn) - --> space complexity is O(n) for arrays - --> not in place, good for large arrays - >>> seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2] - >>> merge_sort(seq) == sorted(seq) - True - >>> seq2 = [3, 3, 3, 3, 3, 3, 3, 3] - >>> merge_sort(seq2) == sorted(seq2) - True - >>> seq3 = [] - >>> merge_sort(seq3) == sorted(seq3) - True -''' - -''' This is the main function that keep dividing the seq ''' -def merge_sort(seq): - if len(seq) < 2 : return seq # base case - mid = len(seq)//2 - left, right = None, None # we could have declared the arrays here, - # but this would allocate unecessary extra space - if seq[:mid]: left = merge_sort(seq[:mid]) - if seq[mid:]: right = merge_sort(seq[mid:]) # notice that mid is included! - - return merge(left, right) # merge iteratively - - -''' The two following merge functions are O(2n)=O(n) and O(n) respectively. They - illustrate many features in Python that ''' -def merge_2n(left, right): - if not left or not right: return left or right # nothing to be merged - result = [] - while left and right: - if left[-1] >= right[-1]: - result.append(left.pop()) - else: - result.append(right.pop()) - result.reverse() - return (left or right) + result - - -def merge(left, right): - if not left or not right: return left or right # nothing to be merged - result = [] - i, j = 0, 0 - while i < len(left) and j < len(right): - if left[i] <= right[j]: - result.append(left[i]) - i += 1 - else: - result.append(right[j]) - j += 1 - if left[i:] : result.extend(left[i:]) # REMEMBER TO TO ENXTEND NOT APPEND - if right[j:] : result.extend(right[j:]) - return result - - - - - - -if __name__ == '__main__': - import doctest - doctest.testmod() - - diff --git a/src/searching_and_sorting/sorting_adv/quick_sort.py b/src/searching_and_sorting/sorting_adv/quick_sort.py deleted file mode 100644 index 201aa0c..0000000 --- a/src/searching_and_sorting/sorting_adv/quick_sort.py +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/python3 -# mari von steinkirch @2013 -# steinkirch at gmail - - -''' Some examples of how to implement Quick Sort in Python - --> RUNTIME: BEST/AVERAGE Is O(nlogn), WORST is O(n2) - --> the first example is not in place, the second is in place - --> test with two element arrays, identical values - - Quick sort in place: - 1) select pivot as the index = 0 - 2) start pointer1 at index = 1 and pointer2 in the last element - 3) while pointer1 < pointer2: - if value in pointer1 <= pivot - swap value in pointer1 with value in pointer2 and advanced pointer2 - else - advance pointer1 - 4) now the array is like this: - [pivot, larger than pivot, smaller than pivot] - 5) swap the pivot where pointer 1 stop - 6) do recursively for [smaller] + [pivot] + [larger] - - >>> seq = [3, 5, 2, 6, 8, 1, 0, 3, 5, 6, 2] - >>> quick_sort(seq) == sorted(seq) - True - >>> quick_sort([3, 3, 3, 3, 3, 3]) == [3, 3, 3, 3, 3, 3] - True - >>> quick_sort([]) == [] - True - >>> quick_sort([2,1]) == [1, 2] - True - >>> quick_sort_in(seq) == sorted(seq) - True - >>> quick_sort_in([3, 3, 3, 3, 3, 3]) == [3, 3, 3, 3, 3, 3] - True - >>> quick_sort_in([]) == [] - True - >>> quick_sort_in([2,1]) == [1, 2] - True -''' - -def quick_sort(seq): - if len(seq) < 2 : return seq - mid = len(seq)//2 - pi = seq[mid] - seq = seq[:mid] + seq[mid+1:] - left = quick_sort([x for x in seq if x <= pi]) # REMEMBER TO INCLUDE X (OR IN RIGHT) - right = quick_sort([x for x in seq if x > pi]) - return left + [pi] + right - - - - -def quick_sort_in(seq): - if len(seq) < 2 : return seq - if len(seq) == 2 and seq[0] > seq[1]: - seq[0], seq[1] = seq[1], seq[0] # problems when only 2 elements because of swap - pivot = seq[0] # start at the ends because we don't know how many elements - p1, p2 = 1, len(seq) -1 # set pointers at both ends - while p1 < p2: # must be < or out of range - if seq[p1] <= pivot: # must be <= because of pivot swap - seq[p1], seq[p2] = seq[p2], seq[p1] - p2 -= 1 - else: - p1 += 1 - seq[0], seq[p1] = seq[p1], pivot - return quick_sort_in(seq[p1+1:]) + [seq[p1]] + quick_sort_in(seq[:p1]) - - - -if __name__ == '__main__': - import doctest - doctest.testmod()