From 6574504eedb64aeaa7176fccde0008086d20b875 Mon Sep 17 00:00:00 2001 From: Martmists Date: Tue, 9 Oct 2018 22:16:07 +0200 Subject: [PATCH 01/47] Add Stooge, pidgeonhole and cocktail shaker sort Signed-off-by: Martmists --- .editorconfig | 3 +- allalgorithms/.editorconfig | 3 +- allalgorithms/sorting/__init__.py | 3 ++ allalgorithms/sorting/cocktail_shaker_sort.py | 30 +++++++++++++++++++ allalgorithms/sorting/pidgeonhole_sort.py | 23 ++++++++++++++ allalgorithms/sorting/stooge_sort.py | 21 +++++++++++++ docs/sorting/cocktail-shaker-sort.md | 30 +++++++++++++++++++ docs/sorting/pidgeonhole-sort.md | 30 +++++++++++++++++++ docs/sorting/stooge-sort.md | 30 +++++++++++++++++++ tests/test_sorting.py | 21 ++++++++++--- 10 files changed, 188 insertions(+), 6 deletions(-) create mode 100644 allalgorithms/sorting/cocktail_shaker_sort.py create mode 100644 allalgorithms/sorting/pidgeonhole_sort.py create mode 100644 allalgorithms/sorting/stooge_sort.py create mode 100644 docs/sorting/cocktail-shaker-sort.md create mode 100644 docs/sorting/pidgeonhole-sort.md create mode 100644 docs/sorting/stooge-sort.md diff --git a/.editorconfig b/.editorconfig index dec6b5b..4c3931a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,7 +8,8 @@ root = true [*] -indent_style = tab +indent_style = space +indent_size = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true diff --git a/allalgorithms/.editorconfig b/allalgorithms/.editorconfig index 4d5a7f0..3e4ba08 100644 --- a/allalgorithms/.editorconfig +++ b/allalgorithms/.editorconfig @@ -7,7 +7,8 @@ root = true [*] -indent_style = tab +indent_style = space +indent_size = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true diff --git a/allalgorithms/sorting/__init__.py b/allalgorithms/sorting/__init__.py index d84d4f4..f249576 100644 --- a/allalgorithms/sorting/__init__.py +++ b/allalgorithms/sorting/__init__.py @@ -2,3 +2,6 @@ from .insertion_sort import insertion_sort from .selection_sort import selection_sort from .bubble_sort import bubble_sort +from .pidgeonhole_sort import pidgeonhole_sort +from .stooge_sort import stooge_sort +from .cocktail_shaker_sort import cocktail_shaker_sort diff --git a/allalgorithms/sorting/cocktail_shaker_sort.py b/allalgorithms/sorting/cocktail_shaker_sort.py new file mode 100644 index 0000000..1a0252f --- /dev/null +++ b/allalgorithms/sorting/cocktail_shaker_sort.py @@ -0,0 +1,30 @@ +# -*- coding: UTF-8 -*- +# +# Cocktail Shaker Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: Martmists +# Github: @martmists +# + + +def cocktail_shaker_sort(data): + upper = len(data) - 1 + lower = 0 + + no_swap = False + while upper - lower > 1 and not no_swap: + no_swap = True + for j in range(lower, upper): + if data[j + 1] < data[j]: + data[j + 1], data[j] = data[j], data[j + 1] + no_swap = False + upper = upper - 1 + + for j in range(upper, lower, -1): + if data[j - 1] > data[j]: + data[j - 1], data[j] = data[j], data[j - 1] + no_swap = False + lower = lower + 1 + + return data diff --git a/allalgorithms/sorting/pidgeonhole_sort.py b/allalgorithms/sorting/pidgeonhole_sort.py new file mode 100644 index 0000000..aa0e709 --- /dev/null +++ b/allalgorithms/sorting/pidgeonhole_sort.py @@ -0,0 +1,23 @@ +# -*- coding: UTF-8 -*- +# +# Pidgeonhole Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: Martmists +# Github: @martmists +# + + +def pidgeonhole_sort(data): + minimum = min(data) + size = max(data) - minimum + 1 + holes = [0] * size + for item in data: + holes[item - minimum] += 1 + i = 0 + for count in range(size): + while holes[count] > 0: + holes[count] -= 1 + data[i] = count + minimum + i += 1 + return data diff --git a/allalgorithms/sorting/stooge_sort.py b/allalgorithms/sorting/stooge_sort.py new file mode 100644 index 0000000..499f8e6 --- /dev/null +++ b/allalgorithms/sorting/stooge_sort.py @@ -0,0 +1,21 @@ +# -*- coding: UTF-8 -*- +# +# Stooge Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: Martmists +# Github: @martmists +# + + +def stooge_sort(seq, start=0, end=None): + if end is None: + end = len(seq) - 1 + if seq[start] > seq[end]: + seq[start], seq[end] = seq[end], seq[start] + if (end - start + 1) > 2: + third = (end - start + 1) // 3 + stooge_sort(seq, start, end-third) + stooge_sort(seq, start+third, end) + stooge_sort(seq, start, end-third) + return seq diff --git a/docs/sorting/cocktail-shaker-sort.md b/docs/sorting/cocktail-shaker-sort.md new file mode 100644 index 0000000..8d8841c --- /dev/null +++ b/docs/sorting/cocktail-shaker-sort.md @@ -0,0 +1,30 @@ +# Cocktail Shaker Sort + +Cocktail shaker sort, also known as bidirectional bubble sort, cocktail sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle sort, or shuttle sort, is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than a bubble sort, and solves the problem of turtles in bubble sorts. It provides only marginal performance improvements, and does not improve asymptotic performance; like the bubble sort, it is not of practical interest (insertion sort is preferred for simple sorts), though it finds some use in education. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import cocktail_shaker_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(cocktail_shaker_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### cocktail_shaker_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array diff --git a/docs/sorting/pidgeonhole-sort.md b/docs/sorting/pidgeonhole-sort.md new file mode 100644 index 0000000..8fa568e --- /dev/null +++ b/docs/sorting/pidgeonhole-sort.md @@ -0,0 +1,30 @@ +# Pidgeonhole Sort + +Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists of elements where the number of elements (n) and the length of the range of possible key values (N) are approximately the same. It requires O(n + N) time. It is similar to counting sort, but differs in that it "moves items twice: once to the bucket array and again to the final destination [whereas] counting sort builds an auxiliary array then uses the array to compute each item's final destination and move the item there." + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import pidgeonhole_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(pidgeonhole_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### pidgeonhole_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array diff --git a/docs/sorting/stooge-sort.md b/docs/sorting/stooge-sort.md new file mode 100644 index 0000000..816fdea --- /dev/null +++ b/docs/sorting/stooge-sort.md @@ -0,0 +1,30 @@ +# Stooge Sort + +Stooge sort is a recursive sorting algorithm. It is notable for its exceptional bad time complexity of O(n^(log 3 / log 1.5)) = O(n^2.7095...). The running time of the algorithm is thus slower compared to reasonable sorting algorithms, and is slower than Bubble sort, a canonical example of a fairly inefficient sort. It is however more efficient than Slowsort. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import stooge_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(stooge_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### stooge_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array diff --git a/tests/test_sorting.py b/tests/test_sorting.py index e11902b..7fe083c 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -1,12 +1,16 @@ import unittest from allalgorithms.sorting import ( - bubble_sort, - insertion_sort, - merge_sort, - selection_sort + bubble_sort, + insertion_sort, + merge_sort, + selection_sort, + pidgeonhole_sort, + stooge_sort, + cocktail_shaker_sort ) + class TestSorting(unittest.TestCase): def test_merge_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], merge_sort([7, 3, 2, 19, -44, 1])) @@ -20,6 +24,15 @@ def test_insertion_sort(self): def test_selection_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], selection_sort([7, 3, 2, 19, -44, 1])) + def test_pidgeonhole_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], pidgeonhole_sort([7, 3, 2, 19, -44, 1])) + + def test_stooge_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], stooge_sort([7, 3, 2, 19, -44, 1])) + + def test_cocktail_shaker_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], cocktail_shaker_sort([7, 3, 2, 19, -44, 1])) + if __name__ == "__main__": unittest.main() From 49248a059970d9096e50c5ee0ac54141a59f3de8 Mon Sep 17 00:00:00 2001 From: Carlos Abraham Date: Wed, 10 Oct 2018 11:27:15 -0400 Subject: [PATCH 02/47] add new algorithms on the tree --- docs/readme.md | 12 +++++++++--- readme.md | 13 +++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/readme.md b/docs/readme.md index 2995cc4..0f37fe0 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -55,10 +55,16 @@ print(binary_search(arr, 3)) # Tree -- Searches +- ### Searches - [Binary Search](searches/binary-search) -- Sorting - - [Merge Sort](sorting/merge-sort) +- ### Sorting + - [Bubble Sort](sorting/bubble-sort) + - [Cocktail Shaker Sort](sorting/cocktail-shaker-sort) + - [Insertion Sort](sorting/insertion-sort) + - [Merge Sort](sorting/merge-sort) + - [Pidgeonhole Sort](sorting/pidgeonhole-sort) + - [Selection Sort](sorting/selection-sort) + - [Stooge Sort](sorting/stooge-sort) # Related diff --git a/readme.md b/readme.md index 2a480f2..e43ba69 100644 --- a/readme.md +++ b/readme.md @@ -55,11 +55,16 @@ print(binary_search(arr, 3)) # Tree -- Searches +- ### Searches - [Binary Search](https://python.allalgorithms.com/searches/binary-search) -- Sorting - - [Merge Sort](https://python.allalgorithms.com/sorting/merge-sort) - +- ### Sorting + - [Bubble Sort](https://python.allalgorithms.com/sorting/bubble-sort) + - [Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort) + - [Insertion Sort](https://python.allalgorithms.com/sorting/insertion-sort) + - [Merge Sort](https://python.allalgorithms.com/sorting/merge-sort) + - [Pidgeonhole Sort](https://python.allalgorithms.com/sorting/pidgeonhole-sort) + - [Selection Sort](https://python.allalgorithms.com/sorting/selection-sort) + - [Stooge Sort](https://python.allalgorithms.com/sorting/stooge-sort) # Related From 7cf18d379bcea5cc98c616dc46039753377e6591 Mon Sep 17 00:00:00 2001 From: Carlos Abraham Date: Wed, 10 Oct 2018 11:33:48 -0400 Subject: [PATCH 03/47] type from 'pidgeonhole' to 'pigeonhole' sort --- .../sorting/{pidgeonhole_sort.py => pigeonhole_sort.py} | 5 ++--- docs/readme.md | 2 +- docs/sorting/{pidgeonhole-sort.md => pigeonhole-sort.md} | 8 ++++---- readme.md | 2 +- tests/test_sorting.py | 4 ++-- 5 files changed, 10 insertions(+), 11 deletions(-) rename allalgorithms/sorting/{pidgeonhole_sort.py => pigeonhole_sort.py} (88%) rename docs/sorting/{pidgeonhole-sort.md => pigeonhole-sort.md} (84%) diff --git a/allalgorithms/sorting/pidgeonhole_sort.py b/allalgorithms/sorting/pigeonhole_sort.py similarity index 88% rename from allalgorithms/sorting/pidgeonhole_sort.py rename to allalgorithms/sorting/pigeonhole_sort.py index aa0e709..c7ce64f 100644 --- a/allalgorithms/sorting/pidgeonhole_sort.py +++ b/allalgorithms/sorting/pigeonhole_sort.py @@ -1,14 +1,13 @@ # -*- coding: UTF-8 -*- # -# Pidgeonhole Sort Algorithm +# Pigeonhole Sort Algorithm # The All ▲lgorithms library for python # # Contributed by: Martmists # Github: @martmists # - -def pidgeonhole_sort(data): +def pigeonhole_sort(data): minimum = min(data) size = max(data) - minimum + 1 holes = [0] * size diff --git a/docs/readme.md b/docs/readme.md index 0f37fe0..867bc1e 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -62,7 +62,7 @@ print(binary_search(arr, 3)) - [Cocktail Shaker Sort](sorting/cocktail-shaker-sort) - [Insertion Sort](sorting/insertion-sort) - [Merge Sort](sorting/merge-sort) - - [Pidgeonhole Sort](sorting/pidgeonhole-sort) + - [Pigeonhole Sort](sorting/pigeonhole-sort) - [Selection Sort](sorting/selection-sort) - [Stooge Sort](sorting/stooge-sort) diff --git a/docs/sorting/pidgeonhole-sort.md b/docs/sorting/pigeonhole-sort.md similarity index 84% rename from docs/sorting/pidgeonhole-sort.md rename to docs/sorting/pigeonhole-sort.md index 8fa568e..d74631b 100644 --- a/docs/sorting/pidgeonhole-sort.md +++ b/docs/sorting/pigeonhole-sort.md @@ -1,4 +1,4 @@ -# Pidgeonhole Sort +# Pigeonhole Sort Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists of elements where the number of elements (n) and the length of the range of possible key values (N) are approximately the same. It requires O(n + N) time. It is similar to counting sort, but differs in that it "moves items twice: once to the bucket array and again to the final destination [whereas] counting sort builds an auxiliary array then uses the array to compute each item's final destination and move the item there." @@ -11,17 +11,17 @@ pip install allalgorithms ## Usage ```py -from allalgorithms.sorting import pidgeonhole_sort +from allalgorithms.sorting import pigeonhole_sort arr = [77, 2, 10, -2, 1, 7] -print(pidgeonhole_sort(arr)) +print(pigeonhole_sort(arr)) # -> [-2, 1, 2, 7, 10, 77] ``` ## API -### pidgeonhole_sort(array) +### pigeonhole_sort(array) > Returns a sorted array diff --git a/readme.md b/readme.md index e43ba69..cfbee77 100644 --- a/readme.md +++ b/readme.md @@ -62,7 +62,7 @@ print(binary_search(arr, 3)) - [Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort) - [Insertion Sort](https://python.allalgorithms.com/sorting/insertion-sort) - [Merge Sort](https://python.allalgorithms.com/sorting/merge-sort) - - [Pidgeonhole Sort](https://python.allalgorithms.com/sorting/pidgeonhole-sort) + - [Pigeonhole Sort](https://python.allalgorithms.com/sorting/pigeonhole-sort) - [Selection Sort](https://python.allalgorithms.com/sorting/selection-sort) - [Stooge Sort](https://python.allalgorithms.com/sorting/stooge-sort) diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 7fe083c..6c1913a 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -5,7 +5,7 @@ insertion_sort, merge_sort, selection_sort, - pidgeonhole_sort, + pigeonhole_sort, stooge_sort, cocktail_shaker_sort ) @@ -24,7 +24,7 @@ def test_insertion_sort(self): def test_selection_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], selection_sort([7, 3, 2, 19, -44, 1])) - def test_pidgeonhole_sort(self): + def test_pigeonhole_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], pidgeonhole_sort([7, 3, 2, 19, -44, 1])) def test_stooge_sort(self): From d8ddec42a5d328e92df8445333207a5ec7fe7f9a Mon Sep 17 00:00:00 2001 From: Carlos Abraham Date: Wed, 10 Oct 2018 11:42:48 -0400 Subject: [PATCH 04/47] fix function typo on TestSorting --- tests/test_sorting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 6c1913a..0f84a4f 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -25,7 +25,7 @@ def test_selection_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], selection_sort([7, 3, 2, 19, -44, 1])) def test_pigeonhole_sort(self): - self.assertEqual([-44, 1, 2, 3, 7, 19], pidgeonhole_sort([7, 3, 2, 19, -44, 1])) + self.assertEqual([-44, 1, 2, 3, 7, 19], pigeonhole_sort([7, 3, 2, 19, -44, 1])) def test_stooge_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], stooge_sort([7, 3, 2, 19, -44, 1])) From 64a55d1f193f0f4f4c40e952039350ff3cc7b9a8 Mon Sep 17 00:00:00 2001 From: Carlos Abraham Date: Wed, 10 Oct 2018 12:01:07 -0400 Subject: [PATCH 05/47] add algorithms to changelog --- changelog.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 558ec82..71baf9e 100644 --- a/changelog.md +++ b/changelog.md @@ -8,7 +8,23 @@ Date: October 9, 2018 ### Algorithms: -- Sorting +- ### Sorting - Bubble Sort -- Searches +- ### Searches - Merge Sort + +# Version `0.0.1` + +Date: TO ADDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD, 2018 + +### Algorithms: + +Added: + +- ### Searches + - Bubble Sort + - Cocktail Shaker Sort + - Insertion Sort + - Pigeonhole Sort + - Selection Sort + - Stooge Sort From 90aa055a2f7f5072c8248f32505ef5c36da95110 Mon Sep 17 00:00:00 2001 From: Pratham Date: Wed, 10 Oct 2018 21:41:35 +0530 Subject: [PATCH 06/47] Adding Tree Sort --- allalgorithms/sorting/tree_sort.py | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 allalgorithms/sorting/tree_sort.py diff --git a/allalgorithms/sorting/tree_sort.py b/allalgorithms/sorting/tree_sort.py new file mode 100644 index 0000000..5113cbb --- /dev/null +++ b/allalgorithms/sorting/tree_sort.py @@ -0,0 +1,47 @@ +class BinaryTreeNode(object): + #initial values for value,left and right + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + +# inserting a new node in the binary tree +def insert(tree, item): + # if no initial element in the tree + if tree == None: + tree = BinaryTreeNode(item) + else: + if (item < tree.value): + # if left branch of the tree is empty + if (tree.left == None): + tree.left = BinaryTreeNode(item) + else: + insert(tree.left, item) + else: + # if right branch of the tree is empty + if (tree.right == None): + tree.right = BinaryTreeNode(item) + else: + insert(tree.right, item) + return tree + + +# funtion for the inorder traversal of the binary tree +def in_order_traversal(tree): + if (tree.left != None): + in_order_traversal(tree.left) + print(tree.value) + if (tree.right != None): + in_order_traversal(tree.right) + + +if __name__ == '__main__': + x = list(map(int,input().split(" "))) + # root node + t = insert(None, x[0]); + # inserting all elements in the binary tree + for i in x[1:]: + insert(t,i) + # the results of the inorder traversal of a binary tree is a sorted + in_order_traversal(t) From 4bb8b55dfe57016c03d170015378acca51e536ad Mon Sep 17 00:00:00 2001 From: Pratham Date: Wed, 10 Oct 2018 21:50:09 +0530 Subject: [PATCH 07/47] Added Tree Sort --- allalgorithms/sorting/tree_sort.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/allalgorithms/sorting/tree_sort.py b/allalgorithms/sorting/tree_sort.py index 5113cbb..8e325eb 100644 --- a/allalgorithms/sorting/tree_sort.py +++ b/allalgorithms/sorting/tree_sort.py @@ -36,8 +36,7 @@ def in_order_traversal(tree): in_order_traversal(tree.right) -if __name__ == '__main__': - x = list(map(int,input().split(" "))) +def TreeSort(x): # root node t = insert(None, x[0]); # inserting all elements in the binary tree From 0694fbf9ad83a85b75a68c2be145ec38161c2641 Mon Sep 17 00:00:00 2001 From: Pratham Date: Wed, 10 Oct 2018 21:52:54 +0530 Subject: [PATCH 08/47] Added Tree Sort --- allalgorithms/sorting/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/allalgorithms/sorting/__init__.py b/allalgorithms/sorting/__init__.py index f249576..8dadeb4 100644 --- a/allalgorithms/sorting/__init__.py +++ b/allalgorithms/sorting/__init__.py @@ -5,3 +5,4 @@ from .pidgeonhole_sort import pidgeonhole_sort from .stooge_sort import stooge_sort from .cocktail_shaker_sort import cocktail_shaker_sort +from .tree_sort import TreeSort From 46e5ecbaf411240d674a1573c161943087a0ca1e Mon Sep 17 00:00:00 2001 From: Pratham1807 <35078777+Pratham1807@users.noreply.github.com> Date: Wed, 10 Oct 2018 21:55:31 +0530 Subject: [PATCH 09/47] Update __init__.py --- allalgorithms/sorting/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/allalgorithms/sorting/__init__.py b/allalgorithms/sorting/__init__.py index 8dadeb4..3c45246 100644 --- a/allalgorithms/sorting/__init__.py +++ b/allalgorithms/sorting/__init__.py @@ -5,4 +5,4 @@ from .pidgeonhole_sort import pidgeonhole_sort from .stooge_sort import stooge_sort from .cocktail_shaker_sort import cocktail_shaker_sort -from .tree_sort import TreeSort +from .tree_sort import tree_sort From ade7d9b80ab49e5ed31a4f669f962511d9d2148f Mon Sep 17 00:00:00 2001 From: Pratham1807 <35078777+Pratham1807@users.noreply.github.com> Date: Wed, 10 Oct 2018 21:56:37 +0530 Subject: [PATCH 10/47] Added Tree Sort --- allalgorithms/sorting/tree_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/allalgorithms/sorting/tree_sort.py b/allalgorithms/sorting/tree_sort.py index 8e325eb..d5b3cbd 100644 --- a/allalgorithms/sorting/tree_sort.py +++ b/allalgorithms/sorting/tree_sort.py @@ -36,7 +36,7 @@ def in_order_traversal(tree): in_order_traversal(tree.right) -def TreeSort(x): +def tree_sort(x): # root node t = insert(None, x[0]); # inserting all elements in the binary tree From 4744f6de06ea93c3b03a21d10e3c4b5f6ecd9e76 Mon Sep 17 00:00:00 2001 From: Carlos Abraham Date: Wed, 10 Oct 2018 12:28:03 -0400 Subject: [PATCH 11/47] add contributing guide --- .github/code-of-conduct.md | 40 ++++++++++++ .github/contributing.md | 102 +++++++++++++++++++++++++++++++ .github/issue_template.md | 21 +++++++ .github/pull_request_template.md | 23 +++++++ docs/readme.md | 4 +- readme.md | 2 + 6 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 .github/code-of-conduct.md create mode 100644 .github/contributing.md create mode 100644 .github/issue_template.md create mode 100644 .github/pull_request_template.md diff --git a/.github/code-of-conduct.md b/.github/code-of-conduct.md new file mode 100644 index 0000000..f809c8b --- /dev/null +++ b/.github/code-of-conduct.md @@ -0,0 +1,40 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/.github/contributing.md b/.github/contributing.md new file mode 100644 index 0000000..28fa900 --- /dev/null +++ b/.github/contributing.md @@ -0,0 +1,102 @@ +## Contributing + +> Please note that this project is released with a [Contributor Code of Conduct](code-of-conduct.md). By participating in this project you agree to abide by its terms. + +## See + +- [General Rules](#general-rules) +- [All ▲lgorithms Structure](#all-▲lgorithms-structure) +- [Adding new algorithms](adding-new-algorithms) +- [Style](#style) +- [Adding Documentation](#adding-documentation) +- [Run it online](#run-it-online) + +### General Rules + +- As much as possible, try to follow the existing format of markdown and code. + +### All ▲lgorithms Python Library Structure + +- Directories and files are all in lower case letter. +- Files are separated by an underscore (`_`) following the `snake_case` style. +- Directories in documentation are separated by a minus or hyphen (`-`) following `kebeab-case` style. + +> We follow this structure + +``` +├── allalgorithms +│ │── sorting +| | │── bubble_sort.py +| | └── merge_sort.py +│ └── searches +| │── binary_search.py +| └── linear_search.py +├── docs +│ │── sorting +| | │── bubble-sort.md +| | └── merge-sort.md +│ └── searches +| │── binary-search.md +| └── linear-search.md +└── tests + │── test_searches.py + └── test_sorting.py +``` + +### Adding new algorithms + +- Make your pull requests to be **specific** and **focused**. Instead of contributing "several algorithms" all at once contribute them all one by one separately (i.e. one pull request for "Binary Search", another one +for "Bubble Sort" and so on). +- Describe what you do in code using **comments**. + +### Style + +This repository follow the [PEP8 Style Gide for Python](https://www.python.org/dev/peps/pep-0008/), so make sure you lint your code before adding a new pull request. + +Each `.py` file should have the following header. (no for testing files) + +```py +# -*- coding: UTF-8 -*- +# +# Binary search works for a sorted array. +# The All ▲lgorithms library for python +# +# Contributed by: Carlos Abraham Hernandez +# Github: @abranhe +# +``` + +If the algorithm is modified, this should be included there also. + +```py +# Contributed by: Carlos Abraham Hernandez +# Github: @abranhe +# +# Modified by: Your Name +# Github: @yourgithubusername +``` + +If the algorithm have been modified by multiple contributors, that should be included as follow. + +```py +# Contributed by: Carlos Abraham Hernandez +# Github: @abranhe +# +# Modifiers: +# Your Name, @yourgithubusername +# Your friend's name, @yourfriendongithub +``` + +### Adding Documentation + +Please make sure if you add an algorithm, you also add the required +documentation for it the `/docs` directory. + +Follow some of the examples already added. + +If you are modifying an algorithm make sure you add a benchmark using [Repl.it](https://repl.it/) for the maintainers to have it easy to review it. + + +#### Lastly and not less important: + +Make sure you start ⭐️ the project and follow [@abranhe](https://git.io/abranhe) diff --git a/.github/issue_template.md b/.github/issue_template.md new file mode 100644 index 0000000..9bfede2 --- /dev/null +++ b/.github/issue_template.md @@ -0,0 +1,21 @@ + + +This issue is: + + + +- [ ] A new Algorithm +- [ ] An update to an existing algorithm. +- [ ] An error found +- [ ] A proposal +- [ ] A question +- [ ] Other (Describe below*) + +**Description:** + + diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..b85077b --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,23 @@ + + +This pull request is: + + + +- [ ] A new Algorithm +- [ ] An update to an existing algorithm. +- [ ] An error fix +- [ ] Other (Describe below*) + +This pull request fixes: + + + +**Changes:** + + diff --git a/docs/readme.md b/docs/readme.md index 867bc1e..f755458 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -31,7 +31,9 @@ - Actively maintained - Because All Algorithms should easy to use in Python -Read the detailed documentation at [python.allalgorithms.com](https://python.allalgorithms.com) or see [Tree](#tree). +Read the detailed documentation at [python.allalgorithms.com](https://python.allalgorithms.com) or see [Tree](#tree). + +**Want to contribute?** [GET STARTED HERE](https://github.com/abranhe/python-lib/tree/master/.github/contributing.md) ## Install diff --git a/readme.md b/readme.md index cfbee77..1d5be4f 100644 --- a/readme.md +++ b/readme.md @@ -33,6 +33,8 @@ Read the detailed documentation at [python.allalgorithms.com](https://python.allalgorithms.com) or see the [`docs`](https://github.com/abranhe/python-lib/blob/master/docs) directory on Github. See [Tree](#tree). +**Want to contribute?** [GET STARTED HERE](https://github.com/abranhe/python-lib/tree/master/.github/contributing.md) + ## Install ``` From 4ea4898092d2283312d17a74677c9c76097ea437 Mon Sep 17 00:00:00 2001 From: Carlos Abraham Date: Wed, 10 Oct 2018 12:31:32 -0400 Subject: [PATCH 12/47] fix broken link --- .github/contributing.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/contributing.md b/.github/contributing.md index 28fa900..23022fe 100644 --- a/.github/contributing.md +++ b/.github/contributing.md @@ -5,11 +5,10 @@ ## See - [General Rules](#general-rules) -- [All ▲lgorithms Structure](#all-▲lgorithms-structure) -- [Adding new algorithms](adding-new-algorithms) +- [All ▲lgorithms Structure](#all-lgorithms-structure) +- [Adding new algorithms](#adding-new-algorithms) - [Style](#style) - [Adding Documentation](#adding-documentation) -- [Run it online](#run-it-online) ### General Rules From 5c7ec3e751c93ea7852eff673fe13b4fab842741 Mon Sep 17 00:00:00 2001 From: Carlos Abraham Date: Wed, 10 Oct 2018 12:34:44 -0400 Subject: [PATCH 13/47] =?UTF-8?q?fix=20broken=20link=20=F0=9F=A4=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/contributing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/contributing.md b/.github/contributing.md index 23022fe..963fbb0 100644 --- a/.github/contributing.md +++ b/.github/contributing.md @@ -5,7 +5,7 @@ ## See - [General Rules](#general-rules) -- [All ▲lgorithms Structure](#all-lgorithms-structure) +- [All ▲lgorithms Structure](#all-lgorithms-python-library-structure) - [Adding new algorithms](#adding-new-algorithms) - [Style](#style) - [Adding Documentation](#adding-documentation) From bd23e92411bac35fe89f9af4da6a3e175409fd00 Mon Sep 17 00:00:00 2001 From: Pratham Date: Thu, 11 Oct 2018 02:12:54 +0530 Subject: [PATCH 14/47] added docs and test --- docs/sorting/tree-sort.md | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_sorting.py | 6 +++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 docs/sorting/tree-sort.md diff --git a/docs/sorting/tree-sort.md b/docs/sorting/tree-sort.md new file mode 100644 index 0000000..bd87af6 --- /dev/null +++ b/docs/sorting/tree-sort.md @@ -0,0 +1,38 @@ +# Tree Sort + +A tree sort is a sort algorithm that builds a binary search tree from the elements to be sorted, and then traverses the tree (in-order) so that the elements come out in sorted order. It has two phases: +1. Frist is creating a binary search tree using the given array elements. +2. Second phase is traversing the given binary search tree in inorder, thus resulting in a sorted array. + +**Performance** + +The average number of comparisions for this method is O(nlogn). But in worst case, number of comparisions is reduced by O(n^2), a case which arrives when the tree is skewed. + + + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import tree_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(tree_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### tree_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array \ No newline at end of file diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 7fe083c..90ef887 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -7,7 +7,8 @@ selection_sort, pidgeonhole_sort, stooge_sort, - cocktail_shaker_sort + cocktail_shaker_sort, + tree_sort ) @@ -32,6 +33,9 @@ def test_stooge_sort(self): def test_cocktail_shaker_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], cocktail_shaker_sort([7, 3, 2, 19, -44, 1])) + + def tree_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1])) if __name__ == "__main__": From 4a73197daf4f906139ae2b13cdc965cd922fe848 Mon Sep 17 00:00:00 2001 From: dieterpl Date: Wed, 10 Oct 2018 23:20:30 +0200 Subject: [PATCH 15/47] Added fibonacci search --- allalgorithms/searches/__init__.py | 1 + allalgorithms/searches/fibonacci_search.py | 33 ++++++++++++++++++++++ changelog.md | 7 +++++ docs/searches/fibonacci-search.md | 33 ++++++++++++++++++++++ readme.md | 1 + tests/test_searches.py | 13 +++++++-- 6 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 allalgorithms/searches/fibonacci_search.py create mode 100644 docs/searches/fibonacci-search.md diff --git a/allalgorithms/searches/__init__.py b/allalgorithms/searches/__init__.py index dad7119..dc0f547 100644 --- a/allalgorithms/searches/__init__.py +++ b/allalgorithms/searches/__init__.py @@ -1 +1,2 @@ from .binary_search import * +from .fibonacci_search import * diff --git a/allalgorithms/searches/fibonacci_search.py b/allalgorithms/searches/fibonacci_search.py new file mode 100644 index 0000000..5afd388 --- /dev/null +++ b/allalgorithms/searches/fibonacci_search.py @@ -0,0 +1,33 @@ +# -*- coding: UTF-8 -*- +# +# Fibonacci search works for a sorted array. +# The All ▲lgorithms library for python +# +# Contributed by: dieterpl +# Github: @dieterpl +# +def fibonacci_search(arr, query): + fib2, fib1 = 0, 1 + fib = fib2 + fib1 + hi = len(arr) - 1 + while fib <= hi: + fib2 = fib1 + fib1 = fib + fib = fib2 + fib1 + offset = -1 + while fib > 1: + i = min(offset + fib2, hi) + if arr[i] < query: + fib = fib1 + fib1 = fib2 + fib2 = fib - fib1 + offset = i + elif arr[i] > query: + fib = fib2 + fib1 = fib1 - fib2 + fib2 = fib - fib1 + else: + return i + if fib1 and arr[offset + 1] == query: + return offset + 1 + return None diff --git a/changelog.md b/changelog.md index 71baf9e..528a93c 100644 --- a/changelog.md +++ b/changelog.md @@ -28,3 +28,10 @@ Added: - Pigeonhole Sort - Selection Sort - Stooge Sort + +# Version `0.0.2` + +Date: October 10, 2018 + +- ### Sorting + - Fibonacci Search diff --git a/docs/searches/fibonacci-search.md b/docs/searches/fibonacci-search.md new file mode 100644 index 0000000..fc09267 --- /dev/null +++ b/docs/searches/fibonacci-search.md @@ -0,0 +1,33 @@ +# Fibonacci Search + +In computer science, the Fibonacci search technique is a method of searching a sorted array using a divide and conquer algorithm that narrows down possible locations with the aid of Fibonacci numbers. (Wikipedia) +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.searches import fibonacci_search + +arr = [-2, 1, 2, 7, 10, 77] + +print(fibonacci_search(arr, 7)) +# -> 3 + +print(fibonacci_search(arr, 3)) +# -> None +``` + +## API + +### fibonacci_search(array, query) + +> Return array index if its found, otherwise returns `None` + +##### Params: + +- `array`: Sorted Array +- `query`: Element to search for in the array diff --git a/readme.md b/readme.md index 1d5be4f..fdf6385 100644 --- a/readme.md +++ b/readme.md @@ -59,6 +59,7 @@ print(binary_search(arr, 3)) - ### Searches - [Binary Search](https://python.allalgorithms.com/searches/binary-search) + - [Fibonacci Search](https://python.allalgorithms.com/searches/fibonacci-search) - ### Sorting - [Bubble Sort](https://python.allalgorithms.com/sorting/bubble-sort) - [Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort) diff --git a/tests/test_searches.py b/tests/test_searches.py index 622beeb..dfc96e1 100644 --- a/tests/test_searches.py +++ b/tests/test_searches.py @@ -1,7 +1,9 @@ import unittest -from allalgorithms.searches import binary_search - +from allalgorithms.sorting import ( + binary_search, + fibonacci_search +) class TestSearches(unittest.TestCase): @@ -12,6 +14,13 @@ def test_binary_search(self): self.assertEqual(None, binary_search(arr, 8)) self.assertEqual(None, binary_search(arr, -1)) + def test_fibonacci_search(self): + arr = [1, 2, 3, 7, 10, 19, 27, 77] + self.assertEqual(3, fibonacci_search(arr, 7)) + self.assertEqual(7, fibonacci_search(arr, 77)) + self.assertEqual(None, fibonacci_search(arr, 8)) + self.assertEqual(None, fibonacci_search(arr, -1)) + if __name__ == '__main__': unittest.main() From c2ef611ab7ebdad0d3996dba16a79e4ea5383b42 Mon Sep 17 00:00:00 2001 From: dieterpl Date: Wed, 10 Oct 2018 23:33:51 +0200 Subject: [PATCH 16/47] Fixed import to searches instead of sorting --- tests/test_searches.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_searches.py b/tests/test_searches.py index dfc96e1..5ac707d 100644 --- a/tests/test_searches.py +++ b/tests/test_searches.py @@ -1,6 +1,6 @@ import unittest -from allalgorithms.sorting import ( +from allalgorithms.searches import ( binary_search, fibonacci_search ) From cba2a6711f82b3493683c7d8dbe87d5f5fff531a Mon Sep 17 00:00:00 2001 From: pieromoto Date: Thu, 11 Oct 2018 00:06:18 +0200 Subject: [PATCH 17/47] Added jump search --- allalgorithms/searches/__init__.py | 1 + allalgorithms/searches/jump_search.py | 25 +++++++++++++++++++ changelog.md | 11 +++++++++ docs/searches/jump-search.md | 35 +++++++++++++++++++++++++++ readme.md | 1 + tests/test_searches.py | 9 ++++++- 6 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 allalgorithms/searches/jump_search.py create mode 100644 docs/searches/jump-search.md diff --git a/allalgorithms/searches/__init__.py b/allalgorithms/searches/__init__.py index dad7119..9f9d0fa 100644 --- a/allalgorithms/searches/__init__.py +++ b/allalgorithms/searches/__init__.py @@ -1 +1,2 @@ from .binary_search import * +from .jump_search import * diff --git a/allalgorithms/searches/jump_search.py b/allalgorithms/searches/jump_search.py new file mode 100644 index 0000000..39a0cc0 --- /dev/null +++ b/allalgorithms/searches/jump_search.py @@ -0,0 +1,25 @@ +# -*- coding: UTF-8 -*- +# +# Jump search works for a sorted array. +# The All ▲lgorithms library for python +# +# Contributed by: pieromoto +# Github: @pieromoto +# +import math + +def jump_search( arr, query): + arr_len = len(arr) + prev = 0 + step = int(math.sqrt(arr_len)) + + for i in range(step-1, arr_len, step): + if(arr[i] >= query): + break + prev = i + + for j in range(prev, arr_len): + if(arr[j] == query): + return j + + return None \ No newline at end of file diff --git a/changelog.md b/changelog.md index 71baf9e..25f8cb5 100644 --- a/changelog.md +++ b/changelog.md @@ -28,3 +28,14 @@ Added: - Pigeonhole Sort - Selection Sort - Stooge Sort + +# Version `0.0.2` + +Date: October 10, 2018 + +### Algorithms: + +Added: + +- ### Searches + - Jump Search diff --git a/docs/searches/jump-search.md b/docs/searches/jump-search.md new file mode 100644 index 0000000..f7ece99 --- /dev/null +++ b/docs/searches/jump-search.md @@ -0,0 +1,35 @@ +# Jump Search + +In computer science, jump search is a search algorithm for sorted array which find the element by jumping ahead by fixed steps or skipping some elements in place of searching all elements. +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.searches import jump_search + +arr = [-2, 1, 2, 7, 10, 77] + +print(jump_search(arr, 7)) +# -> 3 + +print(jump_search(arr, 3)) +# -> None +``` + +## API + +### binary_search(array, query) + +> Return array index if its found, otherwise returns `None` + +##### Params: + +- `arr`: Sorted Array +- `query`: Element to search for in the array + + diff --git a/readme.md b/readme.md index 1d5be4f..e31cefe 100644 --- a/readme.md +++ b/readme.md @@ -59,6 +59,7 @@ print(binary_search(arr, 3)) - ### Searches - [Binary Search](https://python.allalgorithms.com/searches/binary-search) + - [Jump Search](https://python.allalgorithms.com/searches/jump-search) - ### Sorting - [Bubble Sort](https://python.allalgorithms.com/sorting/bubble-sort) - [Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort) diff --git a/tests/test_searches.py b/tests/test_searches.py index 622beeb..dddd19f 100644 --- a/tests/test_searches.py +++ b/tests/test_searches.py @@ -1,6 +1,6 @@ import unittest -from allalgorithms.searches import binary_search +from allalgorithms.searches import (binary_search, jump_search) class TestSearches(unittest.TestCase): @@ -12,6 +12,13 @@ def test_binary_search(self): self.assertEqual(None, binary_search(arr, 8)) self.assertEqual(None, binary_search(arr, -1)) + def test_jump_search(self): + arr = [1, 2, 3, 7, 10, 19, 27, 77] + self.assertEqual(3, binary_search(arr, 7)) + self.assertEqual(7, binary_search(arr, 77)) + self.assertEqual(None, binary_search(arr, 8)) + self.assertEqual(None, binary_search(arr, -1)) + if __name__ == '__main__': unittest.main() From 14bf84b8ee1db45f66805ee6f6897cf82b0404b7 Mon Sep 17 00:00:00 2001 From: dieterpl Date: Thu, 11 Oct 2018 00:13:24 +0200 Subject: [PATCH 18/47] Added lines --- allalgorithms/searches/fibonacci_search.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/allalgorithms/searches/fibonacci_search.py b/allalgorithms/searches/fibonacci_search.py index 5afd388..0d9aa01 100644 --- a/allalgorithms/searches/fibonacci_search.py +++ b/allalgorithms/searches/fibonacci_search.py @@ -6,6 +6,8 @@ # Contributed by: dieterpl # Github: @dieterpl # + + def fibonacci_search(arr, query): fib2, fib1 = 0, 1 fib = fib2 + fib1 From 93e176dd2079c0b1777d3aa0efbbc85344581e42 Mon Sep 17 00:00:00 2001 From: dieterpl Date: Thu, 11 Oct 2018 00:21:49 +0200 Subject: [PATCH 19/47] Added a palindrome checker --- allalgorithms/string/__init__.py | 2 ++ allalgorithms/string/palindrome_check.py | 17 +++++++++++ changelog.md | 13 +++++++++ docs/string/palindrome-check.md | 36 ++++++++++++++++++++++++ tests/test_string.py | 17 +++++++++++ 5 files changed, 85 insertions(+) create mode 100644 allalgorithms/string/__init__.py create mode 100644 allalgorithms/string/palindrome_check.py create mode 100644 docs/string/palindrome-check.md create mode 100644 tests/test_string.py diff --git a/allalgorithms/string/__init__.py b/allalgorithms/string/__init__.py new file mode 100644 index 0000000..6d8b504 --- /dev/null +++ b/allalgorithms/string/__init__.py @@ -0,0 +1,2 @@ +from .palindrome_check import * + diff --git a/allalgorithms/string/palindrome_check.py b/allalgorithms/string/palindrome_check.py new file mode 100644 index 0000000..a9a5991 --- /dev/null +++ b/allalgorithms/string/palindrome_check.py @@ -0,0 +1,17 @@ +# -*- coding: UTF-8 -*- +# +# Checks if string is a palindrome +# The All ▲lgorithms library for python +# +# Contributed by: dieterpl +# Github: @dieterpl +# +import re + +def palindrome_check(s): + s = re.sub(r'[^\w]', '', s) + if len(s) < 2: + return True + if s[0].lower() != s[-1].lower(): + return False + return palindrome_check(s[1:-1]) diff --git a/changelog.md b/changelog.md index 71baf9e..e40a1bf 100644 --- a/changelog.md +++ b/changelog.md @@ -28,3 +28,16 @@ Added: - Pigeonhole Sort - Selection Sort - Stooge Sort + +# Version `0.0.2` + +Date: October 10, 2018 + +### Algorithms: + +Added: + +- ### String + - Palindrome Checker + + diff --git a/docs/string/palindrome-check.md b/docs/string/palindrome-check.md new file mode 100644 index 0000000..6e6cc7f --- /dev/null +++ b/docs/string/palindrome-check.md @@ -0,0 +1,36 @@ +# Palindrome Check + +A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar or the number 10201. (Wikipedia) + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.string import palindrome_check + +str = "10201" + +print(palindrome_check(str) +# -> True + +str = "test" + +print(palindrome_check(str) +# -> False +``` + +## API + +### palindrome_check(string) + +> Return True if string is a palindrome, False otherwise + +##### Params: + +- `string`: Input String + diff --git a/tests/test_string.py b/tests/test_string.py new file mode 100644 index 0000000..998daf0 --- /dev/null +++ b/tests/test_string.py @@ -0,0 +1,17 @@ +import unittest + +from allalgorithms.string import palindrome_check + + +class TestSorting(unittest.TestCase): + + def test_palindrome_check(self): + self.assertEqual(True, palindrome_check("a")) + self.assertEqual(True, palindrome_check("10201")) + self.assertEqual(False, palindrome_check("test")) + self.assertEqual(True, palindrome_check("Mr. Owl ate my metal worm")) + self.assertEqual(True, palindrome_check("Was it a car or a cat I saw?")) + self.assertEqual(False, palindrome_check("How are you?")) + +if __name__ == "__main__": + unittest.main() From 6669e314757bffce34bb358a2d6cd06d4c924f21 Mon Sep 17 00:00:00 2001 From: dieterpl Date: Thu, 11 Oct 2018 00:24:34 +0200 Subject: [PATCH 20/47] Added readme text --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 1d5be4f..355a72a 100644 --- a/readme.md +++ b/readme.md @@ -67,6 +67,8 @@ print(binary_search(arr, 3)) - [Pigeonhole Sort](https://python.allalgorithms.com/sorting/pigeonhole-sort) - [Selection Sort](https://python.allalgorithms.com/sorting/selection-sort) - [Stooge Sort](https://python.allalgorithms.com/sorting/stooge-sort) +- ### String + - [Palindrome Check](https://python.allalgorithms.com/string/palindrom-check) # Related From 14116dc4320e9d4ab6d4a7274ea97577f0de09c2 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 10 Oct 2018 21:10:53 -0400 Subject: [PATCH 21/47] fixed api docs jump_search --- docs/searches/jump-search.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/searches/jump-search.md b/docs/searches/jump-search.md index f7ece99..d439278 100644 --- a/docs/searches/jump-search.md +++ b/docs/searches/jump-search.md @@ -1,6 +1,7 @@ # Jump Search In computer science, jump search is a search algorithm for sorted array which find the element by jumping ahead by fixed steps or skipping some elements in place of searching all elements. + ## Install ``` @@ -23,7 +24,7 @@ print(jump_search(arr, 3)) ## API -### binary_search(array, query) +### jump_search(array, query) > Return array index if its found, otherwise returns `None` From 53c6a1ea66272f5c8489cf1ae3060691085af535 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 10 Oct 2018 21:11:59 -0400 Subject: [PATCH 22/47] jump_search will be included on `v0.0.1` --- changelog.md | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/changelog.md b/changelog.md index 25f8cb5..36058d9 100644 --- a/changelog.md +++ b/changelog.md @@ -28,14 +28,4 @@ Added: - Pigeonhole Sort - Selection Sort - Stooge Sort - -# Version `0.0.2` - -Date: October 10, 2018 - -### Algorithms: - -Added: - -- ### Searches - Jump Search From 2ae94f9c86a82c15230d76c6ff7e901ff0cb93bc Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 10 Oct 2018 21:12:58 -0400 Subject: [PATCH 23/47] add jump search to tree --- docs/readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/readme.md b/docs/readme.md index f755458..e29f3da 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -59,6 +59,7 @@ print(binary_search(arr, 3)) - ### Searches - [Binary Search](searches/binary-search) + - [Jump Search](searches/jump-search) - ### Sorting - [Bubble Sort](sorting/bubble-sort) - [Cocktail Shaker Sort](sorting/cocktail-shaker-sort) From 4a883c7cb83b62c22ee28a4c86eeedd75e2361c7 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 10 Oct 2018 22:03:22 -0400 Subject: [PATCH 24/47] add fibonacci search docs on main readme --- docs/readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/readme.md b/docs/readme.md index e29f3da..80e8f33 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -59,6 +59,7 @@ print(binary_search(arr, 3)) - ### Searches - [Binary Search](searches/binary-search) + - [Fibonacci Search](searches/fibonacci-search) - [Jump Search](searches/jump-search) - ### Sorting - [Bubble Sort](sorting/bubble-sort) From ce87b436e3b6374647ee2bbaa61762653b0a9c6b Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 10 Oct 2018 22:09:35 -0400 Subject: [PATCH 25/47] adding forgotten `,` --- tests/test_searches.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_searches.py b/tests/test_searches.py index 3193b1b..41120f7 100644 --- a/tests/test_searches.py +++ b/tests/test_searches.py @@ -2,7 +2,7 @@ from allalgorithms.searches import ( binary_search, - fibonacci_search + fibonacci_search, jump_search ) @@ -30,4 +30,4 @@ def test_jump_search(self): self.assertEqual(None, binary_search(arr, -1)) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() From 8f875f5254db0c5c19934125e6cbc4ec8e556efd Mon Sep 17 00:00:00 2001 From: Pratham Date: Thu, 11 Oct 2018 21:14:06 +0530 Subject: [PATCH 26/47] Modified tree_sort.py --- allalgorithms/sorting/tree_sort.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/allalgorithms/sorting/tree_sort.py b/allalgorithms/sorting/tree_sort.py index d5b3cbd..3a2ffb3 100644 --- a/allalgorithms/sorting/tree_sort.py +++ b/allalgorithms/sorting/tree_sort.py @@ -1,3 +1,4 @@ + class BinaryTreeNode(object): #initial values for value,left and right def __init__(self, value): @@ -26,21 +27,22 @@ def insert(tree, item): insert(tree.right, item) return tree - # funtion for the inorder traversal of the binary tree -def in_order_traversal(tree): +def in_order_traversal(tree,a): if (tree.left != None): - in_order_traversal(tree.left) - print(tree.value) + in_order_traversal(tree.left,a) + a.append(tree.value) if (tree.right != None): - in_order_traversal(tree.right) + in_order_traversal(tree.right,a) -def tree_sort(x): +def TreeSort(x): # root node t = insert(None, x[0]); # inserting all elements in the binary tree for i in x[1:]: insert(t,i) # the results of the inorder traversal of a binary tree is a sorted - in_order_traversal(t) + a = [] + in_order_traversal(t,a) + return a From 9a90a70124a6dacc338dfc6f1cf4e18a9b284840 Mon Sep 17 00:00:00 2001 From: Pratham Date: Thu, 11 Oct 2018 21:27:58 +0530 Subject: [PATCH 27/47] modified tree_sort --- allalgorithms/sorting/tree_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/allalgorithms/sorting/tree_sort.py b/allalgorithms/sorting/tree_sort.py index 3a2ffb3..341c305 100644 --- a/allalgorithms/sorting/tree_sort.py +++ b/allalgorithms/sorting/tree_sort.py @@ -1,4 +1,3 @@ - class BinaryTreeNode(object): #initial values for value,left and right def __init__(self, value): @@ -36,7 +35,7 @@ def in_order_traversal(tree,a): in_order_traversal(tree.right,a) -def TreeSort(x): +def tree_sort(x): # root node t = insert(None, x[0]); # inserting all elements in the binary tree @@ -46,3 +45,4 @@ def TreeSort(x): a = [] in_order_traversal(t,a) return a + From 9f755dc52f29326b348cbda92eda5af2b8a9b815 Mon Sep 17 00:00:00 2001 From: Pratham Date: Thu, 11 Oct 2018 21:40:54 +0530 Subject: [PATCH 28/47] modified tree_sort --- tests/test_sorting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 90ef887..dc4cc80 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -35,7 +35,7 @@ def test_cocktail_shaker_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], cocktail_shaker_sort([7, 3, 2, 19, -44, 1])) def tree_sort(self): - self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1])) + self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1])) if __name__ == "__main__": From 18fb7beb9e8569b976c491a73b4234318b7cdf1d Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Fri, 19 Oct 2018 23:24:55 -0400 Subject: [PATCH 29/47] rename-repository --- readme.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/readme.md b/readme.md index b25dfe4..3e6c152 100644 --- a/readme.md +++ b/readme.md @@ -11,11 +11,11 @@

- - + + - Coverage Status + Coverage Status

@@ -33,7 +33,7 @@ Read the detailed documentation at [python.allalgorithms.com](https://python.allalgorithms.com) or see the [`docs`](https://github.com/abranhe/python-lib/blob/master/docs) directory on Github. See [Tree](#tree). -**Want to contribute?** [GET STARTED HERE](https://github.com/abranhe/python-lib/tree/master/.github/contributing.md) +**Want to contribute?** [GET STARTED HERE](https://github.com/abranhe/allalgorithms-python/tree/master/.github/contributing.md) ## Install @@ -75,7 +75,7 @@ print(binary_search(arr, 3)) # Related -- [javascript-lib](https://github.com/abranhe/javascript-lib): All ▲lgorithms Javascript library +- [allalgorithms-javascript](https://github.com/abranhe/allalgorithms-javascript): All ▲lgorithms Javascript library # Maintainers @@ -90,10 +90,10 @@ print(binary_search(arr, 3)) [1]: https://cdn.abranhe.com/projects/algorithms/badge.svg -[2]: https://github.com/abranhe/python-lib +[2]: https://github.com/abranhe/allalgorithms-python [3]: https://avatars3.githubusercontent.com/u/21347264?s=50 [4]: https://github.com/abranhe -[5]: https://github.com/abranhe/python-lib/blob/master/LICENSE +[5]: https://github.com/abranhe/allalgorithms-python/blob/master/license

From ea8154be9f17aadb7470efafa2af516bc244bcab Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Fri, 19 Oct 2018 23:26:30 -0400 Subject: [PATCH 30/47] new link source --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index c8eca1f..5a95e27 100644 --- a/setup.py +++ b/setup.py @@ -27,6 +27,6 @@ 'Programming Language :: Python :: 3.6', ), project_urls={ - 'Source': 'https://github.com/abranhe/python-lib', + 'Source': 'https://github.com/abranhe/allalgorithms-python', }, ) From 670e2a3ca2df3f0fcfc1c6e833c0e0575db26a20 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Fri, 19 Oct 2018 23:37:22 -0400 Subject: [PATCH 31/47] readme on docs --- docs/readme.md | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/docs/readme.md b/docs/readme.md index 80e8f33..9929e55 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -11,11 +11,11 @@

- - + + - Coverage Status + Coverage Status

@@ -29,11 +29,11 @@ - Why not 😂 - Clean and focused - Actively maintained -- Because All Algorithms should easy to use in Python +- Because All Algorithms should be easy to use in Python -Read the detailed documentation at [python.allalgorithms.com](https://python.allalgorithms.com) or see [Tree](#tree). +Read the detailed documentation at [python.allalgorithms.com](https://python.allalgorithms.com) or see the [`docs`](https://github.com/abranhe/allalgorithms-python/blob/master/docs) directory on Github. See [Tree](#tree). -**Want to contribute?** [GET STARTED HERE](https://github.com/abranhe/python-lib/tree/master/.github/contributing.md) +**Want to contribute?** [GET STARTED HERE](https://github.com/abranhe/allalgorithms-python/tree/master/.github/contributing.md) ## Install @@ -58,22 +58,24 @@ print(binary_search(arr, 3)) # Tree - ### Searches - - [Binary Search](searches/binary-search) - - [Fibonacci Search](searches/fibonacci-search) - - [Jump Search](searches/jump-search) + - [Binary Search](https://python.allalgorithms.com/searches/binary-search) + - [Fibonacci Search](https://python.allalgorithms.com/searches/fibonacci-search) + - [Jump Search](https://python.allalgorithms.com/searches/jump-search) + - ### Sorting - - [Bubble Sort](sorting/bubble-sort) - - [Cocktail Shaker Sort](sorting/cocktail-shaker-sort) - - [Insertion Sort](sorting/insertion-sort) - - [Merge Sort](sorting/merge-sort) - - [Pigeonhole Sort](sorting/pigeonhole-sort) - - [Selection Sort](sorting/selection-sort) - - [Stooge Sort](sorting/stooge-sort) - + - [Bubble Sort](https://python.allalgorithms.com/sorting/bubble-sort) + - [Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort) + - [Insertion Sort](https://python.allalgorithms.com/sorting/insertion-sort) + - [Merge Sort](https://python.allalgorithms.com/sorting/merge-sort) + - [Pigeonhole Sort](https://python.allalgorithms.com/sorting/pigeonhole-sort) + - [Selection Sort](https://python.allalgorithms.com/sorting/selection-sort) + - [Stooge Sort](https://python.allalgorithms.com/sorting/stooge-sort) +- ### String + - [Palindrome Check](https://python.allalgorithms.com/string/palindrom-check) # Related -- [javascript-lib](https://github.com/abranhe/javascript-lib): All ▲lgorithms Javascript library +- [allalgorithms-javascript](https://github.com/abranhe/allalgorithms-javascript): All ▲lgorithms Javascript library # Maintainers @@ -88,10 +90,10 @@ print(binary_search(arr, 3)) [1]: https://cdn.abranhe.com/projects/algorithms/badge.svg -[2]: https://github.com/abranhe/python-lib +[2]: https://github.com/abranhe/allalgorithms-python [3]: https://avatars3.githubusercontent.com/u/21347264?s=50 [4]: https://github.com/abranhe -[5]: https://github.com/abranhe/python-lib/blob/master/LICENSE +[5]: https://github.com/abranhe/allalgorithms-python/blob/master/license

From cb3a5d760a86ce91df5f2319cae3cdfb59756f48 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Fri, 19 Oct 2018 23:37:42 -0400 Subject: [PATCH 32/47] little link fix --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 3e6c152..9929e55 100644 --- a/readme.md +++ b/readme.md @@ -31,7 +31,7 @@ - Actively maintained - Because All Algorithms should be easy to use in Python -Read the detailed documentation at [python.allalgorithms.com](https://python.allalgorithms.com) or see the [`docs`](https://github.com/abranhe/python-lib/blob/master/docs) directory on Github. See [Tree](#tree). +Read the detailed documentation at [python.allalgorithms.com](https://python.allalgorithms.com) or see the [`docs`](https://github.com/abranhe/allalgorithms-python/blob/master/docs) directory on Github. See [Tree](#tree). **Want to contribute?** [GET STARTED HERE](https://github.com/abranhe/allalgorithms-python/tree/master/.github/contributing.md) From 4d662fc9c4b99a7ec64d958f5a8c966db38ef0f7 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Tue, 23 Oct 2018 15:03:51 -0400 Subject: [PATCH 33/47] changes to changelog --- changelog.md | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/changelog.md b/changelog.md index bca1a83..a46e7e3 100644 --- a/changelog.md +++ b/changelog.md @@ -1,32 +1,23 @@
-Algorithms +

Algorithms + +Changelog +

-# Version `0.0.0` +# `0.0.1` -Date: October 9, 2018 - -### Algorithms: - -- ### Searches - - Merge Sort - -- ### Sorting - - Bubble Sort - -# Version `0.0.1` - -Date: TO ADDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD, 2018 +Date: **TODO** -### Algorithms: +> Algorithms: Added: -- ### Searches +- **Searches** - Fibonacci Search - Jump Search -- ### Sorting +- **Sorting** - Bubble Sort - Cocktail Shaker Sort - Insertion Sort @@ -34,5 +25,17 @@ Added: - Selection Sort - Stooge Sort -- ### String - - Palindrome Checker \ No newline at end of file +- **String** + - Palindrome Checker + +# `0.0.0` + +Date: October 9, 2018 + +> Algorithms: + +- **Searches** + - Merge Sort + +- **Sorting** + - Bubble Sort From e11a5488f32f9d66bbe023e118e5a35fb5b811ba Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Tue, 23 Oct 2018 15:11:14 -0400 Subject: [PATCH 34/47] add docs for changelog --- .github/contributing.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/contributing.md b/.github/contributing.md index 963fbb0..d8d5c29 100644 --- a/.github/contributing.md +++ b/.github/contributing.md @@ -50,6 +50,8 @@ for "Bubble Sort" and so on). ### Style +

Please DO NOT EDIT CHANGELOG on your pull requests, this is must be edited by one of the write access maintainers when they going to drop a new release.

+ This repository follow the [PEP8 Style Gide for Python](https://www.python.org/dev/peps/pep-0008/), so make sure you lint your code before adding a new pull request. Each `.py` file should have the following header. (no for testing files) From 5fc4641c8cbe82d122409771b2518ada27afecc5 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sat, 23 Feb 2019 18:58:07 +0100 Subject: [PATCH 35/47] fixed test runner --- .../sorting/{pigeonhole_sort.py => pidgeonhole_sort.py} | 2 +- tests/test_sorting.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename allalgorithms/sorting/{pigeonhole_sort.py => pidgeonhole_sort.py} (94%) diff --git a/allalgorithms/sorting/pigeonhole_sort.py b/allalgorithms/sorting/pidgeonhole_sort.py similarity index 94% rename from allalgorithms/sorting/pigeonhole_sort.py rename to allalgorithms/sorting/pidgeonhole_sort.py index c7ce64f..47cfeb1 100644 --- a/allalgorithms/sorting/pigeonhole_sort.py +++ b/allalgorithms/sorting/pidgeonhole_sort.py @@ -7,7 +7,7 @@ # Github: @martmists # -def pigeonhole_sort(data): +def pidgeonhole_sort(data): minimum = min(data) size = max(data) - minimum + 1 holes = [0] * size diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 127f341..c1c41da 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -5,7 +5,7 @@ insertion_sort, merge_sort, selection_sort, - pigeonhole_sort, + pidgeonhole_sort, stooge_sort, cocktail_shaker_sort, tree_sort @@ -26,7 +26,7 @@ def test_selection_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], selection_sort([7, 3, 2, 19, -44, 1])) def test_pigeonhole_sort(self): - self.assertEqual([-44, 1, 2, 3, 7, 19], pigeonhole_sort([7, 3, 2, 19, -44, 1])) + self.assertEqual([-44, 1, 2, 3, 7, 19], pidgeonhole_sort([7, 3, 2, 19, -44, 1])) def test_stooge_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], stooge_sort([7, 3, 2, 19, -44, 1])) From 551774ec8fa0c5dff80b9fd3a51fe157c19fae9e Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sat, 23 Feb 2019 19:20:02 +0100 Subject: [PATCH 36/47] correct name again --- allalgorithms/sorting/__init__.py | 2 +- .../sorting/{pidgeonhole_sort.py => pigeonhole_sort.py} | 2 +- tests/test_sorting.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename allalgorithms/sorting/{pidgeonhole_sort.py => pigeonhole_sort.py} (94%) diff --git a/allalgorithms/sorting/__init__.py b/allalgorithms/sorting/__init__.py index 3c45246..17da872 100644 --- a/allalgorithms/sorting/__init__.py +++ b/allalgorithms/sorting/__init__.py @@ -2,7 +2,7 @@ from .insertion_sort import insertion_sort from .selection_sort import selection_sort from .bubble_sort import bubble_sort -from .pidgeonhole_sort import pidgeonhole_sort +from .pigeonhole_sort import pigeonhole_sort from .stooge_sort import stooge_sort from .cocktail_shaker_sort import cocktail_shaker_sort from .tree_sort import tree_sort diff --git a/allalgorithms/sorting/pidgeonhole_sort.py b/allalgorithms/sorting/pigeonhole_sort.py similarity index 94% rename from allalgorithms/sorting/pidgeonhole_sort.py rename to allalgorithms/sorting/pigeonhole_sort.py index 47cfeb1..c7ce64f 100644 --- a/allalgorithms/sorting/pidgeonhole_sort.py +++ b/allalgorithms/sorting/pigeonhole_sort.py @@ -7,7 +7,7 @@ # Github: @martmists # -def pidgeonhole_sort(data): +def pigeonhole_sort(data): minimum = min(data) size = max(data) - minimum + 1 holes = [0] * size diff --git a/tests/test_sorting.py b/tests/test_sorting.py index c1c41da..127f341 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -5,7 +5,7 @@ insertion_sort, merge_sort, selection_sort, - pidgeonhole_sort, + pigeonhole_sort, stooge_sort, cocktail_shaker_sort, tree_sort @@ -26,7 +26,7 @@ def test_selection_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], selection_sort([7, 3, 2, 19, -44, 1])) def test_pigeonhole_sort(self): - self.assertEqual([-44, 1, 2, 3, 7, 19], pidgeonhole_sort([7, 3, 2, 19, -44, 1])) + self.assertEqual([-44, 1, 2, 3, 7, 19], pigeonhole_sort([7, 3, 2, 19, -44, 1])) def test_stooge_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], stooge_sort([7, 3, 2, 19, -44, 1])) From a7c77faa3bc678f9bcd2289ca46248805fe68090 Mon Sep 17 00:00:00 2001 From: Brian Hopper Date: Tue, 1 Oct 2019 14:24:43 -0700 Subject: [PATCH 37/47] Create quicksort.py Quick and dirty implementation of a quicksort algorithm --- allalgorithms/sorting/quicksort.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 allalgorithms/sorting/quicksort.py diff --git a/allalgorithms/sorting/quicksort.py b/allalgorithms/sorting/quicksort.py new file mode 100644 index 0000000..28582e9 --- /dev/null +++ b/allalgorithms/sorting/quicksort.py @@ -0,0 +1,21 @@ +def partition(xs, start, end): + follower = leader = start + while leader < end: + if xs[leader] <= xs[end]: + xs[follower], xs[leader] = xs[leader], xs[follower] + follower += 1 + leader += 1 + xs[follower], xs[end] = xs[end], xs[follower] + return follower + +def _quicksort(xs, start, end): + if start >= end: + return + p = partition(xs, start, end) + _quicksort(xs, start, p-1) + _quicksort(xs, p+1, end) + +def quicksort(xs): + _quicksort(xs, 0, len(xs)-1) + +# To use: create a list and send it to quicksort: quicksort(list placed here) From 66963819d6669a62bfa263b3c501f3b719a20a0a Mon Sep 17 00:00:00 2001 From: Becky Wilson Date: Tue, 1 Oct 2019 22:56:06 +0100 Subject: [PATCH 38/47] added find max value algorithm --- allalgorithms/numeric/__init__.py | 1 + allalgorithms/numeric/max_numbers.py | 16 ++++++++++++++++ tests/test_numeric.py | 13 +++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 allalgorithms/numeric/__init__.py create mode 100644 allalgorithms/numeric/max_numbers.py create mode 100644 tests/test_numeric.py diff --git a/allalgorithms/numeric/__init__.py b/allalgorithms/numeric/__init__.py new file mode 100644 index 0000000..cd1f5cc --- /dev/null +++ b/allalgorithms/numeric/__init__.py @@ -0,0 +1 @@ +from .max_numbers import find_max \ No newline at end of file diff --git a/allalgorithms/numeric/max_numbers.py b/allalgorithms/numeric/max_numbers.py new file mode 100644 index 0000000..122734a --- /dev/null +++ b/allalgorithms/numeric/max_numbers.py @@ -0,0 +1,16 @@ +# -*- coding: UTF-8 -*- +# +# Numeric Algorithms +# The All ▲lgorithms library for python +# +# Contributed by: Becky +# Github: @beckyw5 +# + + +def find_max (L): + max = 0 + for x in L: + if x > max: + max = x + return max diff --git a/tests/test_numeric.py b/tests/test_numeric.py new file mode 100644 index 0000000..2be532d --- /dev/null +++ b/tests/test_numeric.py @@ -0,0 +1,13 @@ +import unittest + +from allalgorithms.numeric import find_max + + +class TestMax(unittest.TestCase): + + def test_find_max_value(self): + test_list = [3, 1, 8, 7, 4] + self.assertEqual(8, find_max(test_list)) + +if __name__ == "__main__": + unittest.main() From 49e476b1c56896449270157e38d7aaf17a27f370 Mon Sep 17 00:00:00 2001 From: Brian Hopper Date: Tue, 1 Oct 2019 16:58:24 -0700 Subject: [PATCH 39/47] Update quicksort.py Added the required header, which I originally forgot to include! --- allalgorithms/sorting/quicksort.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/allalgorithms/sorting/quicksort.py b/allalgorithms/sorting/quicksort.py index 28582e9..6990462 100644 --- a/allalgorithms/sorting/quicksort.py +++ b/allalgorithms/sorting/quicksort.py @@ -1,3 +1,11 @@ +# -*- coding: UTF-8 -*- +# +# Quick Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: Brian D. Hopper +# Github: @bubbabeans +# def partition(xs, start, end): follower = leader = start while leader < end: From 341b64b79a6d173cf8162968b01b0adaa80b23d5 Mon Sep 17 00:00:00 2001 From: Brian Hopper Date: Tue, 1 Oct 2019 17:09:41 -0700 Subject: [PATCH 40/47] Quicksort documentation --- docs/sorting/Quicksort | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 docs/sorting/Quicksort diff --git a/docs/sorting/Quicksort b/docs/sorting/Quicksort new file mode 100644 index 0000000..e985551 --- /dev/null +++ b/docs/sorting/Quicksort @@ -0,0 +1,26 @@ +Quicksort + +A quicksort is a quicker method of sorting, and there are four different ways of implementing it. The example given uses a pivot point. + +A pivot point is created in the middle of an array, and all larger items go after the pivot point, and smaller items are placed in front +of the pivot point. + +The pivot point is then moved to the middle of either the smaller or larger items, and the sort is run again on that half. + +This continues over and over again until everything is in the proper place. + +Usage +from allalgorithms.sorting import quicksort + +arr = [77, 2, 10, -2, 1, 7] + +print(quicksort(arr)) +# -> [-2, 1, 2, 7, 10, 77] + +API + +quicksort(array) +Returns a sorted array + +Params: +array: Unsorted Array From 73512c91ff391761fd14e425fa603ab5878c18fc Mon Sep 17 00:00:00 2001 From: elias Date: Wed, 2 Oct 2019 02:11:48 +0200 Subject: [PATCH 41/47] added shell sort --- allalgorithms/sorting/shell_sort.py | 30 +++++++++++++++++++++++++++++ docs/sorting/shell-sort.md | 29 ++++++++++++++++++++++++++++ tests/test_sorting.py | 5 ++++- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 allalgorithms/sorting/shell_sort.py create mode 100644 docs/sorting/shell-sort.md diff --git a/allalgorithms/sorting/shell_sort.py b/allalgorithms/sorting/shell_sort.py new file mode 100644 index 0000000..b415af8 --- /dev/null +++ b/allalgorithms/sorting/shell_sort.py @@ -0,0 +1,30 @@ +# -*- coding: UTF-8 -*- +# +# Shell Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: Elias +# Github: @eliasbayona +# + +def shell_sort(arr): + n = len(arr) + h = int(n/2) + + + while h > 0: + for i in range(h,n): + + temp = arr[i] + j = i + + while j >= h and arr[j-h] >temp: + arr[j] = arr[j-h] + j -= h + + arr[j] = temp + + h = int(h/2) + + return arr + diff --git a/docs/sorting/shell-sort.md b/docs/sorting/shell-sort.md new file mode 100644 index 0000000..f984897 --- /dev/null +++ b/docs/sorting/shell-sort.md @@ -0,0 +1,29 @@ +# Selection Sort + +In computer science, shell sort improves upon insertion sort by moving out of order elements more than one position at a time. It has a best case O(n log n) time complexity, and for other cases, it depends on the gap sequence. According to Poonen Theorem, worst case complexity for shell sort is Θ(NlogN)^2/(log logN)^2) or Θ(NlogN)^2/log logN) or Θ(N(logN)^2) or something in between. +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import shell_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(shell_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### selection_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 127f341..32c9435 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -8,7 +8,8 @@ pigeonhole_sort, stooge_sort, cocktail_shaker_sort, - tree_sort + tree_sort, + shell_sort, ) @@ -37,6 +38,8 @@ def test_cocktail_shaker_sort(self): def tree_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1])) + def test_shell_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], shell_sort([7, 3, 2, 19, -44, 1])) if __name__ == "__main__": unittest.main() From f0e5895d804f3e2733896be613ed7db7d2199f6f Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 1 Oct 2019 22:26:32 -0300 Subject: [PATCH 42/47] Fixing a repository url for all js algorithms --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 9929e55..36c01ba 100644 --- a/readme.md +++ b/readme.md @@ -61,7 +61,7 @@ print(binary_search(arr, 3)) - [Binary Search](https://python.allalgorithms.com/searches/binary-search) - [Fibonacci Search](https://python.allalgorithms.com/searches/fibonacci-search) - [Jump Search](https://python.allalgorithms.com/searches/jump-search) - + - ### Sorting - [Bubble Sort](https://python.allalgorithms.com/sorting/bubble-sort) - [Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort) @@ -75,7 +75,7 @@ print(binary_search(arr, 3)) # Related -- [allalgorithms-javascript](https://github.com/abranhe/allalgorithms-javascript): All ▲lgorithms Javascript library +- [allalgorithms-js](https://github.com/abranhe/allalgorithms-js): All ▲lgorithms Javascript library # Maintainers From 3daa67584e73e3dea4d3173d2b9539105a7977d4 Mon Sep 17 00:00:00 2001 From: ninexball <12503741+ninexball@users.noreply.github.com> Date: Tue, 1 Oct 2019 23:18:00 -0500 Subject: [PATCH 43/47] add hamming distance algorithm --- allalgorithms/string/__init__.py | 2 +- allalgorithms/string/hamming_dist.py | 14 ++++++++++++ docs/string/hamming-dist.md | 32 ++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 allalgorithms/string/hamming_dist.py create mode 100644 docs/string/hamming-dist.md diff --git a/allalgorithms/string/__init__.py b/allalgorithms/string/__init__.py index 6d8b504..0774298 100644 --- a/allalgorithms/string/__init__.py +++ b/allalgorithms/string/__init__.py @@ -1,2 +1,2 @@ from .palindrome_check import * - +from .hamming_dist import * diff --git a/allalgorithms/string/hamming_dist.py b/allalgorithms/string/hamming_dist.py new file mode 100644 index 0000000..c2811e1 --- /dev/null +++ b/allalgorithms/string/hamming_dist.py @@ -0,0 +1,14 @@ +# -*- coding: UTF-8 -*- +# +# Binary search works for a sorted array. +# The All ▲lgorithms library for python +# +# Contributed by: ninexball +# Github: @ninexball +# + +def hamming_dist(seq1: str, seq2: str) -> int: + """Compare hamming distance of two strings""" + if len(seq1) != len(seq2): + raise ValueError("length of strings are not the same") + return sum(c1 != c2 for c1, c2 in zip(seq1, seq2)) diff --git a/docs/string/hamming-dist.md b/docs/string/hamming-dist.md new file mode 100644 index 0000000..124a824 --- /dev/null +++ b/docs/string/hamming-dist.md @@ -0,0 +1,32 @@ +# Hamming Distance + +In informatics, Hamming distance is the number of positions where the characters differ between two strings of equal length + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +``` +>>> from allalgorithms.sorting import hamming_dist + +>>> hamming_dist("hello world", "hello wario") +3 +``` + +## API + +### hamming_dist(seq1, seq2) + +> Returns an integer + +> Raises a ValueError if strings are of unequal length + +##### Params: + +- `seq1`: first string to compare +- `seq2`: second string to compare + From 48467961a3c23c3798480bb6090b27b8895a54bf Mon Sep 17 00:00:00 2001 From: Andrade Junior Date: Wed, 2 Oct 2019 01:33:51 -0300 Subject: [PATCH 44/47] Add is unique algorithm --- allalgorithms/string/__init__.py | 2 +- allalgorithms/string/is_unique.py | 16 ++++++++++++++ docs/string/is-unique.md | 36 +++++++++++++++++++++++++++++++ tests/test_string.py | 11 +++++++++- 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 allalgorithms/string/is_unique.py create mode 100644 docs/string/is-unique.md diff --git a/allalgorithms/string/__init__.py b/allalgorithms/string/__init__.py index 6d8b504..301d40c 100644 --- a/allalgorithms/string/__init__.py +++ b/allalgorithms/string/__init__.py @@ -1,2 +1,2 @@ from .palindrome_check import * - +from .is_unique import * \ No newline at end of file diff --git a/allalgorithms/string/is_unique.py b/allalgorithms/string/is_unique.py new file mode 100644 index 0000000..986a30c --- /dev/null +++ b/allalgorithms/string/is_unique.py @@ -0,0 +1,16 @@ +# -*- coding: UTF-8 -*- +# +# Check if a string has all unique characters. +# The All ▲lgorithms library for python +# +# Contributed by: José E. Andrade Jr. +# Github: @andradejunior +# + +def is_unique(string_to_check): + character_set = set() + for character in string_to_check: + if character in character_set: + return False + character_set.add(character) + return True diff --git a/docs/string/is-unique.md b/docs/string/is-unique.md new file mode 100644 index 0000000..7dc439f --- /dev/null +++ b/docs/string/is-unique.md @@ -0,0 +1,36 @@ +# Is Unique + +This algorithms checks if a string has all unique characters in O(n) time. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.string import is_unique + +str = "abcdefg" + +print(is_unique(str) +# -> True + +str = "test" + +print(is_unique(str) +# -> False +``` + +## API + +### is_unique(string) + +> Return True if string has all unique characters, False otherwise + +##### Params: + +- `string`: Input String + diff --git a/tests/test_string.py b/tests/test_string.py index 998daf0..3cca91c 100644 --- a/tests/test_string.py +++ b/tests/test_string.py @@ -1,6 +1,6 @@ import unittest -from allalgorithms.string import palindrome_check +from allalgorithms.string import palindrome_check, is_unique class TestSorting(unittest.TestCase): @@ -13,5 +13,14 @@ def test_palindrome_check(self): self.assertEqual(True, palindrome_check("Was it a car or a cat I saw?")) self.assertEqual(False, palindrome_check("How are you?")) + def test_is_unique(self): + self.assertEqual(True, is_unique("abcdefg")) + self.assertEqual(True, is_unique("1234567")) + self.assertEqual(True, is_unique("algorithms")) + self.assertEqual(False, is_unique("abcdefa")) + self.assertEqual(False, is_unique("abddefg")) + self.assertEqual(False, is_unique("12345567")) + + if __name__ == "__main__": unittest.main() From 454cb1098ec16874db58ffb7622ab8f18e255f26 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Wed, 2 Oct 2019 00:49:06 -0400 Subject: [PATCH 45/47] converting the file to markdown --- docs/sorting/{Quicksort => quicksort.md} | 25 ++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) rename docs/sorting/{Quicksort => quicksort.md} (80%) diff --git a/docs/sorting/Quicksort b/docs/sorting/quicksort.md similarity index 80% rename from docs/sorting/Quicksort rename to docs/sorting/quicksort.md index e985551..e09bc38 100644 --- a/docs/sorting/Quicksort +++ b/docs/sorting/quicksort.md @@ -1,4 +1,4 @@ -Quicksort +# Quicksort A quicksort is a quicker method of sorting, and there are four different ways of implementing it. The example given uses a pivot point. @@ -9,18 +9,31 @@ The pivot point is then moved to the middle of either the smaller or larger item This continues over and over again until everything is in the proper place. -Usage +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py from allalgorithms.sorting import quicksort arr = [77, 2, 10, -2, 1, 7] print(quicksort(arr)) # -> [-2, 1, 2, 7, 10, 77] +``` -API +## API +``` quicksort(array) -Returns a sorted array +``` + +> Returns a sorted array + +##### Params: -Params: -array: Unsorted Array +- `array`: Sorted Array From f60068de6d505187a9a33630ac759efda0d7fc4a Mon Sep 17 00:00:00 2001 From: Andy <28946117+DatHydroGuy@users.noreply.github.com> Date: Wed, 2 Oct 2019 13:06:44 +0100 Subject: [PATCH 46/47] Added Heap sort, tests, and documentation. Fixed test call for tree sort. --- allalgorithms/sorting/__init__.py | 1 + allalgorithms/sorting/heap_sort.py | 48 ++++++++++++++++++++++++++++++ docs/sorting/heap-sort.md | 33 ++++++++++++++++++++ tests/test_sorting.py | 12 ++++++-- 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 allalgorithms/sorting/heap_sort.py create mode 100644 docs/sorting/heap-sort.md diff --git a/allalgorithms/sorting/__init__.py b/allalgorithms/sorting/__init__.py index 17da872..9d4d453 100644 --- a/allalgorithms/sorting/__init__.py +++ b/allalgorithms/sorting/__init__.py @@ -6,3 +6,4 @@ from .stooge_sort import stooge_sort from .cocktail_shaker_sort import cocktail_shaker_sort from .tree_sort import tree_sort +from .heap_sort import heap_sort diff --git a/allalgorithms/sorting/heap_sort.py b/allalgorithms/sorting/heap_sort.py new file mode 100644 index 0000000..a5b9e34 --- /dev/null +++ b/allalgorithms/sorting/heap_sort.py @@ -0,0 +1,48 @@ +# -*- coding: UTF-8 -*- +# +# Heap Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: DatHydroGuy +# Github: @DatHydroGuy +# + + +def build_heap(array_to_sort, array_length, index): + """ + Build a heap, where each node has two child nodes, and a root node is greater than both child nodes. + """ + largest = index # Flag the largest element as the last (root) element + left = 2 * index + 1 # Calculate index of left child node + right = 2 * index + 2 # Calculate index of right child node + + # See if left child of root exists and is greater than root + if left < array_length and array_to_sort[index] < array_to_sort[left]: + largest = left + + # See if right child of root exists and is greater than root + if right < array_length and array_to_sort[largest] < array_to_sort[right]: + largest = right + + # If a larger element than root was found, swap with root so that root holds the new largest value + if largest != index: + array_to_sort[index], array_to_sort[largest] = array_to_sort[largest], array_to_sort[index] # swap + + # Re-build the heap under the new largest root node + build_heap(array_to_sort, array_length, largest) + + +def heap_sort(array_to_sort): + """ + Builds a max-heap, then continuously removes the largest element and re-builds the heap until sorted + """ + array_length = len(array_to_sort) + + # Build a max-heap to sort the elements into order + for index in range(array_length // 2 - 1, -1, -1): + build_heap(array_to_sort, array_length, index) + + # One by one extract elements + for index in range(array_length - 1, 0, -1): + array_to_sort[index], array_to_sort[0] = array_to_sort[0], array_to_sort[index] # swap + build_heap(array_to_sort, index, 0) diff --git a/docs/sorting/heap-sort.md b/docs/sorting/heap-sort.md new file mode 100644 index 0000000..09edef6 --- /dev/null +++ b/docs/sorting/heap-sort.md @@ -0,0 +1,33 @@ +# Heap Sort + +Heap sort is a comparison-based sorting algorithm which operates on a Binary Heap data structure. It can be regarded as a version of Selection sort which is improved by use of the heap data structure rather than a linear-time search. + +Heap sort is typically slower than Quicksort, but it does have a better worst-case scenario of O(n log n). It is an in-place algorithm, but does not produce a stable sort. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import heap_sort + +arr = [77, 2, 10, -2, 1, 7] +heap_sort(arr) + +print(arr) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### heap_sort(array) + +> Performs an in-place sort of the passed-in array + +##### Params: + +- `array`: Unsorted Array diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 127f341..7113065 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -8,7 +8,8 @@ pigeonhole_sort, stooge_sort, cocktail_shaker_sort, - tree_sort + tree_sort, + heap_sort ) @@ -33,10 +34,15 @@ def test_stooge_sort(self): def test_cocktail_shaker_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], cocktail_shaker_sort([7, 3, 2, 19, -44, 1])) - - def tree_sort(self): + + def test_tree_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1])) + def test_heap_sort(self): + array = [7, 3, 2, 19, -44, 1] + heap_sort(array) + self.assertEqual([-44, 1, 2, 3, 7, 19], array) + if __name__ == "__main__": unittest.main() From ee3e0ccc627ce10c01a39fd749cf4b084d635db7 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Sat, 5 Oct 2019 22:56:10 -0400 Subject: [PATCH 47/47] fixing small typo --- tests/test_sorting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 03a1c41..680a943 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -9,7 +9,7 @@ stooge_sort, cocktail_shaker_sort, tree_sort, - heap_sort + heap_sort, shell_sort, )