From 8028db76d2da0f81248639aa5872feeb41ae09a9 Mon Sep 17 00:00:00 2001 From: John Washam Date: Thu, 20 Oct 2016 14:31:09 -0700 Subject: [PATCH 1/7] Minor changes to quicksort. Added new test cases. --- quick_sort/main.py | 47 +++++++++++++++++++++++++--------------- quick_sort/quick_sort.py | 10 ++++----- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/quick_sort/main.py b/quick_sort/main.py index 7ff33b8..60e1e35 100644 --- a/quick_sort/main.py +++ b/quick_sort/main.py @@ -13,6 +13,27 @@ def is_sorted(numbers): return True +def check_sort(original): + numbers = original[:] + + qs = QuickSort(numbers) + output = qs.sort() + + print(output) + + if is_sorted(output): + print("** SUCCESS! **") + else: + print("Uh oh - not in order.") + + if contain_same_ints(original, numbers): + print("** Contain the same elements! **") + else: + print("Uh oh - something is missing.") + + print("---") + + def contain_same_ints(arr1, arr2): for i in arr1: found = False @@ -24,29 +45,21 @@ def contain_same_ints(arr1, arr2): return True + def main(): - original = [325432, 989, 547510, 3, -93, 189019, 5042, 123, + check_sort([325432, 989, 547510, 3, -93, 189019, 5042, 123, 597, 42, 7506, 184, 184, 2409, 45, 824, 4, -2650, 9, 662, 3928, -170, 45358, 395, - 842, 7697, 110, 14, 99, 221] + 842, 7697, 110, 14, 99, 221]) - numbers = original[:] + check_sort([9, 9, 9, 9, 9, 9, 9, 9, 9, 9]) - qs = QuickSort(numbers) - output = qs.sort() + check_sort([3, 5, 7, 9, 23, 25, 34, 53, 77, 199]) - if is_sorted(output): - print("** SUCCESS! **") - else: - print("Uh oh - not in order.") - - if contain_same_ints(original, numbers): - print("** Contain the same elements! **") - else: - print("Uh oh - something is missing.") - - print(output) + check_sort([3, 5, 7]) + check_sort([3, 5]) + check_sort([3]) if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/quick_sort/quick_sort.py b/quick_sort/quick_sort.py index d069a11..b2d34d6 100644 --- a/quick_sort/quick_sort.py +++ b/quick_sort/quick_sort.py @@ -2,7 +2,6 @@ class QuickSort(object): - def __init__(self, numbers): self.values = numbers self.count = len(self.values) @@ -22,16 +21,15 @@ def quick_sort(self, left, right): pivot = self.values[pivot_index] - while i <= j: + while i < j: while self.values[i] < pivot: i += 1 while self.values[j] > pivot: j -= 1 if i <= j: - if i < j: - temp = self.values[i] - self.values[i] = self.values[j] - self.values[j] = temp + temp = self.values[i] + self.values[i] = self.values[j] + self.values[j] = temp i += 1 j -= 1 From d659e7ec382d5ea245a3b43bfdd219ecdc9a1324 Mon Sep 17 00:00:00 2001 From: John Washam Date: Fri, 28 Oct 2016 10:32:27 -0700 Subject: [PATCH 2/7] Renamed directory. --- {interview => puzzles}/generate_numbers.py | 0 {interview => puzzles}/sorting_linear.py | 0 {interview => puzzles}/sorting_linear_optimized.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename {interview => puzzles}/generate_numbers.py (100%) rename {interview => puzzles}/sorting_linear.py (100%) rename {interview => puzzles}/sorting_linear_optimized.py (100%) diff --git a/interview/generate_numbers.py b/puzzles/generate_numbers.py similarity index 100% rename from interview/generate_numbers.py rename to puzzles/generate_numbers.py diff --git a/interview/sorting_linear.py b/puzzles/sorting_linear.py similarity index 100% rename from interview/sorting_linear.py rename to puzzles/sorting_linear.py diff --git a/interview/sorting_linear_optimized.py b/puzzles/sorting_linear_optimized.py similarity index 100% rename from interview/sorting_linear_optimized.py rename to puzzles/sorting_linear_optimized.py From 078f49b087d8365f1eabde1a1a80c27bb1a405b9 Mon Sep 17 00:00:00 2001 From: John Washam Date: Fri, 28 Oct 2016 16:39:28 -0700 Subject: [PATCH 3/7] Formatting and comments. --- .../maximum_increasing_subsequence.py | 23 +++++++++++-------- graphs/directed_graph_list.py | 17 +++++++------- hashes/hash-test.py | 4 ++-- {interview => puzzles}/.gitignore | 0 4 files changed, 25 insertions(+), 19 deletions(-) rename {interview => puzzles}/.gitignore (100%) diff --git a/dynamic_programming/maximum_increasing_subsequence.py b/dynamic_programming/maximum_increasing_subsequence.py index 0be3fa1..7cf8387 100644 --- a/dynamic_programming/maximum_increasing_subsequence.py +++ b/dynamic_programming/maximum_increasing_subsequence.py @@ -1,15 +1,20 @@ + def longest_increasing_subsequence(sequence): - sequence_length = len(sequence) - T = [1 for i in range(sequence_length)] + """ + Returns the length of the longest increasing non-contiguous sequence + """ + length = len(sequence) + counts = [1 for _ in range(length)] + + for i in range(1, length): + for j in range(0, i): + if sequence[j] < sequence[i]: + counts[i] = max(counts[i], counts[j] + 1) - for index_i in range(1, sequence_length): - for index_j in range(0, index_i): - if sequence[index_j] < sequence[index_i]: - T[index_i] = max(T[index_i], T[index_j] + 1) + return max(counts) - return max(T) if __name__ == '__main__': - sequence = [1, 101, 10, 2, 3, 100, 4] - assert 4 == longest_increasing_subsequence(sequence) + sequence = [1, 101, 10, 2, 3, 100, 4, 6, 2] + assert 5 == longest_increasing_subsequence(sequence) diff --git a/graphs/directed_graph_list.py b/graphs/directed_graph_list.py index f0265ee..04f3b4e 100644 --- a/graphs/directed_graph_list.py +++ b/graphs/directed_graph_list.py @@ -181,7 +181,7 @@ def topological_sort(self): def strongly_connected_components(self): """ Compute the vertices in the strongly connected components - :return list of lists, one for each components vertices: + :return list of lists, one for each component's vertices: """ stack = self.scc_dfs_forward_pass() components = self.scc_dfs_reverse_pass(stack) @@ -310,14 +310,14 @@ def test_dfs(): def test_bfs(): dg1 = get_test_graph_1() p1 = dg1.bfs() - assert(p1 == {1: 0, 2: 1, 4: 2, 5: 0, 6: 2, 8: 5}) + assert (p1 == {1: 0, 2: 1, 4: 2, 5: 0, 6: 2, 8: 5}) def test_contains_cycle(): - assert(get_test_graph_1().contains_cycle() == False) - assert(get_test_graph_2().contains_cycle() == False) - assert(get_test_graph_3().contains_cycle() == False) - assert(get_test_graph_4().contains_cycle() == True) + assert (get_test_graph_1().contains_cycle() == False) + assert (get_test_graph_2().contains_cycle() == False) + assert (get_test_graph_3().contains_cycle() == False) + assert (get_test_graph_4().contains_cycle() == True) def test_topological_sort(): @@ -329,10 +329,10 @@ def test_topological_sort(): def test_strongly_connected_components(): dg = get_test_graph_5() - assert(dg.contains_cycle()) + assert (dg.contains_cycle()) components = dg.strongly_connected_components() - assert(components == [[10, 11, 9, 8], [7], [0], [1, 3, 2], [6, 4, 5]]) + assert (components == [[10, 11, 9, 8], [7], [0], [1, 3, 2], [6, 4, 5]]) def main(): @@ -344,5 +344,6 @@ def main(): print("Tests complete.") + if __name__ == "__main__": main() diff --git a/hashes/hash-test.py b/hashes/hash-test.py index 7f304b3..9239c26 100644 --- a/hashes/hash-test.py +++ b/hashes/hash-test.py @@ -3,6 +3,7 @@ import scipy import matplotlib.pyplot as plt + # This script will test multiple hashes on strings to see if we get a uniform distribution @@ -93,9 +94,7 @@ def polyhash_noprime(word, a, m): return abs(hash % m) - def show_distribution(buckets, title): - counts = {} for v in buckets: if v in counts.keys(): @@ -147,5 +146,6 @@ def main(): show_distribution(buckets, "Bucket size distribution - PolyHash, no prime") + if __name__ == "__main__": main() diff --git a/interview/.gitignore b/puzzles/.gitignore similarity index 100% rename from interview/.gitignore rename to puzzles/.gitignore From 91cea47f4c6d838545fcb10055939b1784e4f5e1 Mon Sep 17 00:00:00 2001 From: John Washam Date: Fri, 28 Oct 2016 17:32:19 -0700 Subject: [PATCH 4/7] Minor changes. --- bit_manipulation/bits.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/bit_manipulation/bits.py b/bit_manipulation/bits.py index 36ae118..8d71275 100644 --- a/bit_manipulation/bits.py +++ b/bit_manipulation/bits.py @@ -1,25 +1,28 @@ - def hamming_distance(x, y): difference = x ^ y count = 0 - while difference != 0: - count += 1 + while difference: # this removes ones from right to left (least to most significant) difference &= difference - 1 + count += 1 return count -# Kernighan method +# Wegner method def hamming_weight(x): + if x < 0: + return None + count = 0 - while x != 0: - count += 1 + while x: x &= x - 1 + count += 1 + return count def pop_count(i): - i = i - ((i >> 1) & 0x55555555) + i -= ((i >> 1) & 0x55555555) i = (i & 0x33333333) + ((i >> 2) & 0x33333333) return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24 @@ -205,5 +208,6 @@ def main(): print("swap 213, 14", swap_ints(213, 14)) print("swap 872, 992", swap_ints(872, 992)) + if __name__ == "__main__": main() From 82f349bffe32b4eca0fbe1001f7096773a58c926 Mon Sep 17 00:00:00 2001 From: Asutosh Date: Fri, 6 Oct 2017 16:39:55 +0530 Subject: [PATCH 5/7] updated comment for .remove() in case if there are multiple entries of the passed value, the .remove() method only removes the first occurrence of the passed value. --- arrays/array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arrays/array.py b/arrays/array.py index 0d54968..a376048 100644 --- a/arrays/array.py +++ b/arrays/array.py @@ -11,7 +11,7 @@ def array_test(): print(ar) print("Index of 4: ", ar.index(4)) # index of given value - ar.remove(4) # remove item with given value + ar.remove(4) # remove the first occurence of item with given value print("Removed 4: ", ar) ar.reverse() From 73b91f78d6705d6d8215dcb3296317aee1da6caf Mon Sep 17 00:00:00 2001 From: ifbizo Date: Tue, 21 Nov 2017 18:06:21 -0800 Subject: [PATCH 6/7] Delete all instances of value --- linked_lists/linked_list.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/linked_lists/linked_list.py b/linked_lists/linked_list.py index 9c8e1cd..61b57ce 100644 --- a/linked_lists/linked_list.py +++ b/linked_lists/linked_list.py @@ -52,10 +52,8 @@ def delete(self, value): prev.set_next(current.get_next()) else: self.head_ = current.get_next() - break - else: - prev = current - current = current.get_next() + prev = current + current = current.get_next() # Pushes an item on the front of the list. def push(self, value): From 083ce97fff39c627c29623980e3e888ca0f3083c Mon Sep 17 00:00:00 2001 From: Prasang-money <65109534+Prasang-money@users.noreply.github.com> Date: Sun, 11 Jul 2021 23:37:01 +0530 Subject: [PATCH 7/7] Update linked_list.py # Deletes all instances of given value in list. changes in that particular file when we are deleting a node in that particular case we do not need to update the prev value otherwise prev will be pointed to the deleted node. So in the updated code, we will update prev only if the current node value is not equal to the target value. and the current node will be updated at every iteration --- linked_lists/linked_list.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/linked_lists/linked_list.py b/linked_lists/linked_list.py index 61b57ce..5d0c3ce 100644 --- a/linked_lists/linked_list.py +++ b/linked_lists/linked_list.py @@ -52,7 +52,8 @@ def delete(self, value): prev.set_next(current.get_next()) else: self.head_ = current.get_next() - prev = current + else + prev = current current = current.get_next() # Pushes an item on the front of the list.