From 4ece6d80de01eb276732345ed4b4ae55cbf82071 Mon Sep 17 00:00:00 2001 From: sohail Date: Tue, 9 Oct 2018 20:13:50 +0530 Subject: [PATCH 1/3] heap sorting added --- .../allalgorithms/searches/__init__.py | 1 + .../allalgorithms/searches/binary_search.py | 20 ++++ .../allalgorithms/sorting/__init__.py | 1 + .../allalgorithms/sorting/heap.py | 40 ++++++++ .../allalgorithms/sorting/merge_sort.py | 26 ++++++ python-lib-master/changelog.md | 14 +++ python-lib-master/docs/CNAME | 1 + python-lib-master/docs/readme.md | 92 +++++++++++++++++++ .../docs/searches/binary-search.md | 34 +++++++ python-lib-master/docs/sorting/merge-sort.md | 30 ++++++ python-lib-master/license | 21 +++++ python-lib-master/readme.md | 92 +++++++++++++++++++ python-lib-master/setup.py | 32 +++++++ python-lib-master/tests/test_searches.py | 17 ++++ python-lib-master/tests/test_sorting.py | 12 +++ python-lib-master/tests_requirements.txt | 6 ++ python-lib-master/tox.ini | 64 +++++++++++++ 17 files changed, 503 insertions(+) create mode 100644 python-lib-master/allalgorithms/searches/__init__.py create mode 100644 python-lib-master/allalgorithms/searches/binary_search.py create mode 100644 python-lib-master/allalgorithms/sorting/__init__.py create mode 100644 python-lib-master/allalgorithms/sorting/heap.py create mode 100644 python-lib-master/allalgorithms/sorting/merge_sort.py create mode 100644 python-lib-master/changelog.md create mode 100644 python-lib-master/docs/CNAME create mode 100644 python-lib-master/docs/readme.md create mode 100644 python-lib-master/docs/searches/binary-search.md create mode 100644 python-lib-master/docs/sorting/merge-sort.md create mode 100644 python-lib-master/license create mode 100644 python-lib-master/readme.md create mode 100644 python-lib-master/setup.py create mode 100644 python-lib-master/tests/test_searches.py create mode 100644 python-lib-master/tests/test_sorting.py create mode 100644 python-lib-master/tests_requirements.txt create mode 100644 python-lib-master/tox.ini diff --git a/python-lib-master/allalgorithms/searches/__init__.py b/python-lib-master/allalgorithms/searches/__init__.py new file mode 100644 index 0000000..dad7119 --- /dev/null +++ b/python-lib-master/allalgorithms/searches/__init__.py @@ -0,0 +1 @@ +from .binary_search import * diff --git a/python-lib-master/allalgorithms/searches/binary_search.py b/python-lib-master/allalgorithms/searches/binary_search.py new file mode 100644 index 0000000..b0ba9e4 --- /dev/null +++ b/python-lib-master/allalgorithms/searches/binary_search.py @@ -0,0 +1,20 @@ +# -*- coding: UTF-8 -*- +# +# Binary search works for a sorted array. +# The All ▲lgorithms library for python +# +# Contributed by: Carlos Abraham Hernandez +# Github: @abranhe +# +def binary_search(arr, query): + lo, hi = 0, len(arr) - 1 + while lo <= hi: + mid = (hi + lo) // 2 + val = arr[mid] + if val == query: + return mid + elif val < query: + lo = mid + 1 + else: + hi = mid - 1 + return None diff --git a/python-lib-master/allalgorithms/sorting/__init__.py b/python-lib-master/allalgorithms/sorting/__init__.py new file mode 100644 index 0000000..173bb67 --- /dev/null +++ b/python-lib-master/allalgorithms/sorting/__init__.py @@ -0,0 +1 @@ +from .merge_sort import * diff --git a/python-lib-master/allalgorithms/sorting/heap.py b/python-lib-master/allalgorithms/sorting/heap.py new file mode 100644 index 0000000..aa9beda --- /dev/null +++ b/python-lib-master/allalgorithms/sorting/heap.py @@ -0,0 +1,40 @@ +def heapify(arr, n, i): + largest = i + l = 2 * i + 1 + r = 2 * i + 2 + + + if l < n and arr[i] < arr[l]: + largest = l + + + if r < n and arr[largest] < arr[r]: + largest = r + + + if largest != i: + arr[i],arr[largest] = arr[largest],arr[i] # swap + + + heapify(arr, n, largest) + + +def heapSort(arr): + n = len(arr) + + + for i in range(n, -1, -1): + heapify(arr, n, i) + + + for i in range(n-1, 0, -1): + arr[i], arr[0] = arr[0], arr[i] # swap + heapify(arr, i, 0) + + +arr = [ 12, 11, 13, 5, 6, 7] +heapSort(arr) +n = len(arr) +print ("Sorted array is") +for i in range(n): + print ("%d" %arr[i]), \ No newline at end of file diff --git a/python-lib-master/allalgorithms/sorting/merge_sort.py b/python-lib-master/allalgorithms/sorting/merge_sort.py new file mode 100644 index 0000000..ec80a5c --- /dev/null +++ b/python-lib-master/allalgorithms/sorting/merge_sort.py @@ -0,0 +1,26 @@ +# -*- coding: UTF-8 -*- +# +# Merge Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: Carlos Abraham Hernandez +# Github: @abranhe +# +def merge_sort(arr): + if len(arr) == 1: + return arr + + left = merge_sort(arr[:(len(arr)//2)]) + right = merge_sort(arr[(len(arr)//2):]) + out = [] + + while bool(left) or bool(right): + if bool(left) ^ bool(right): + alias = left if left else right + else: + alias = left if left[0] < right[0] else right + + out.append(alias[0]) + alias.remove(alias[0]) + + return out diff --git a/python-lib-master/changelog.md b/python-lib-master/changelog.md new file mode 100644 index 0000000..558ec82 --- /dev/null +++ b/python-lib-master/changelog.md @@ -0,0 +1,14 @@ +
+Algorithms +
+ +# Version `0.0.0` + +Date: October 9, 2018 + +### Algorithms: + +- Sorting + - Bubble Sort +- Searches + - Merge Sort diff --git a/python-lib-master/docs/CNAME b/python-lib-master/docs/CNAME new file mode 100644 index 0000000..e92b9fe --- /dev/null +++ b/python-lib-master/docs/CNAME @@ -0,0 +1 @@ +python.allalgorithms.com \ No newline at end of file diff --git a/python-lib-master/docs/readme.md b/python-lib-master/docs/readme.md new file mode 100644 index 0000000..2995cc4 --- /dev/null +++ b/python-lib-master/docs/readme.md @@ -0,0 +1,92 @@ +
+ + +
+
+
+
+ The All ▲lgorithms Python library +
+
+
+ +

+ + + + + Coverage Status +

+ +

+
+
+ python.allalgorithms.com +

+ +# Why? + +- Why not 😂 +- Clean and focused +- 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). + +## Install + +``` +pip install allalgorithms +``` + +## Usage Example + +```py +from allalgorithms.searches import binary_search + +arr = [-2, 1, 2, 7, 10, 77] + +print(binary_search(arr, 7)) +# -> 3 + +print(binary_search(arr, 3)) +# -> None +``` + +# Tree + +- Searches + - [Binary Search](searches/binary-search) +- Sorting + - [Merge Sort](sorting/merge-sort) + + +# Related + +- [javascript-lib](https://github.com/abranhe/javascript-lib): All ▲lgorithms Javascript library + +# Maintainers + +|[![Carlos Abraham Logo][3]][4]| +| :--------------------------: | +| [Carlos Abraham][4] | + + +# License + +[MIT][5] License © [Carlos Abraham][4] + + +[1]: https://cdn.abranhe.com/projects/algorithms/badge.svg +[2]: https://github.com/abranhe/python-lib +[3]: https://avatars3.githubusercontent.com/u/21347264?s=50 +[4]: https://github.com/abranhe +[5]: https://github.com/abranhe/python-lib/blob/master/LICENSE + + +
+ + + +
+
diff --git a/python-lib-master/docs/searches/binary-search.md b/python-lib-master/docs/searches/binary-search.md new file mode 100644 index 0000000..1ad9e7a --- /dev/null +++ b/python-lib-master/docs/searches/binary-search.md @@ -0,0 +1,34 @@ +# Binary Search + +In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.searches import binary_search + +arr = [-2, 1, 2, 7, 10, 77] + +print(binary_search(arr, 7)) +# -> 3 + +print(binary_search(arr, 3)) +# -> None +``` + +## API + +### binary_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/python-lib-master/docs/sorting/merge-sort.md b/python-lib-master/docs/sorting/merge-sort.md new file mode 100644 index 0000000..fe17d9c --- /dev/null +++ b/python-lib-master/docs/sorting/merge-sort.md @@ -0,0 +1,30 @@ +# Merge Sort + +In computer science, merge sort is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import merge_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(merge_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### merge_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array diff --git a/python-lib-master/license b/python-lib-master/license new file mode 100644 index 0000000..587f768 --- /dev/null +++ b/python-lib-master/license @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Abraham Hernandez (abranhe.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/python-lib-master/readme.md b/python-lib-master/readme.md new file mode 100644 index 0000000..2a480f2 --- /dev/null +++ b/python-lib-master/readme.md @@ -0,0 +1,92 @@ +
+ + +
+
+
+
+ The All ▲lgorithms Python library +
+
+
+ +

+ + + + + Coverage Status +

+ +

+
+
+ python.allalgorithms.com +

+ +# Why? + +- Why not 😂 +- Clean and focused +- 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). + +## Install + +``` +pip install allalgorithms +``` + +## Usage Example + +```py +from allalgorithms.searches import binary_search + +arr = [-2, 1, 2, 7, 10, 77] + +print(binary_search(arr, 7)) +# -> 3 + +print(binary_search(arr, 3)) +# -> None +``` + +# Tree + +- Searches + - [Binary Search](https://python.allalgorithms.com/searches/binary-search) +- Sorting + - [Merge Sort](https://python.allalgorithms.com/sorting/merge-sort) + + +# Related + +- [javascript-lib](https://github.com/abranhe/javascript-lib): All ▲lgorithms Javascript library + +# Maintainers + +|[![Carlos Abraham Logo][3]][4]| +| :--------------------------: | +| [Carlos Abraham][4] | + + +# License + +[MIT][5] License © [Carlos Abraham][4] + + +[1]: https://cdn.abranhe.com/projects/algorithms/badge.svg +[2]: https://github.com/abranhe/python-lib +[3]: https://avatars3.githubusercontent.com/u/21347264?s=50 +[4]: https://github.com/abranhe +[5]: https://github.com/abranhe/python-lib/blob/master/LICENSE + + +
+ + + +
+
diff --git a/python-lib-master/setup.py b/python-lib-master/setup.py new file mode 100644 index 0000000..c8eca1f --- /dev/null +++ b/python-lib-master/setup.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import setuptools + +with open("readme.md", "r", encoding="utf-8") as readme: + long_description = readme.read() + +setuptools.setup( + name = "allalgorithms", + packages = ["allalgorithms"], + long_description = long_description, + long_description_content_type = "text/markdown", + version = "0.0.0", + description = "All Algorithms in Python", + author = "Carlos Abraham", + author_email = "abraham@abranhe.com", + url = "https://python.allalgorithms.com", + license='MIT', + classifiers=( + "Programming Language :: Python", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.2', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + ), + project_urls={ + 'Source': 'https://github.com/abranhe/python-lib', + }, +) diff --git a/python-lib-master/tests/test_searches.py b/python-lib-master/tests/test_searches.py new file mode 100644 index 0000000..edee228 --- /dev/null +++ b/python-lib-master/tests/test_searches.py @@ -0,0 +1,17 @@ +from allalgorithms.searches import ( + binary_search +) + +import unittest + +class TestSearches(unittest.TestCase): + + def test_binary_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() diff --git a/python-lib-master/tests/test_sorting.py b/python-lib-master/tests/test_sorting.py new file mode 100644 index 0000000..09db9de --- /dev/null +++ b/python-lib-master/tests/test_sorting.py @@ -0,0 +1,12 @@ +from allalgorithms.sorting import ( + merge_sort +) +import unittest + +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])) + +if __name__ == "__main__": + unittest.main() diff --git a/python-lib-master/tests_requirements.txt b/python-lib-master/tests_requirements.txt new file mode 100644 index 0000000..7a3e47c --- /dev/null +++ b/python-lib-master/tests_requirements.txt @@ -0,0 +1,6 @@ +flake8 +python-coveralls +coverage +nose +pytest +tox diff --git a/python-lib-master/tox.ini b/python-lib-master/tox.ini new file mode 100644 index 0000000..85bf226 --- /dev/null +++ b/python-lib-master/tox.ini @@ -0,0 +1,64 @@ +[tox] +envlist = + py34 + py35 + py36 + coverage + +[testenv] +passenv = TRAVIS TRAVIS_* +basepython = + py34: python3.4 + py35: python3.5 + py36: python3.6 + py37: python3.7 +deps = + coverage + coveralls +commands = + coverage run --source=tests,allalgorithms -m unittest discover tests + coverage report -m + coveralls + +[testenv:py34] +passenv = CI TRAVIS TRAVIS_* +basepython = + python3.4 +deps = + pytest +commands = + python3 -m unittest discover tests + python3 -m pytest tests + +[testenv:py35] +passenv = CI TRAVIS TRAVIS_* +basepython = + python3.5 +deps = + pytest +commands = + python3 -m unittest discover tests + python3 -m pytest tests + +[testenv:py36] +passenv = CI TRAVIS TRAVIS_* +basepython = + python3.4 +deps = + pytest +commands = + python3 -m unittest discover tests + python3 -m pytest tests + +[testenv:coverage] +passenv = CI TRAVIS TRAVIS_* +skip_install = True +basepython = + python3.5 +commands = + coverage run --source=tests,allalgorithms -m unittest discover tests + coverage report -m + coveralls +deps = + coverage + coveralls From 8f49b2d655e3408ad3846ec10c9f286425993f55 Mon Sep 17 00:00:00 2001 From: sohail Date: Thu, 11 Oct 2018 19:42:23 +0530 Subject: [PATCH 2/3] Heap Sort added on specific location --- .../allalgorithms/sorting/heapsort.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 python-lib-master/allalgorithms/sorting/heapsort.py diff --git a/python-lib-master/allalgorithms/sorting/heapsort.py b/python-lib-master/allalgorithms/sorting/heapsort.py new file mode 100644 index 0000000..aa9beda --- /dev/null +++ b/python-lib-master/allalgorithms/sorting/heapsort.py @@ -0,0 +1,40 @@ +def heapify(arr, n, i): + largest = i + l = 2 * i + 1 + r = 2 * i + 2 + + + if l < n and arr[i] < arr[l]: + largest = l + + + if r < n and arr[largest] < arr[r]: + largest = r + + + if largest != i: + arr[i],arr[largest] = arr[largest],arr[i] # swap + + + heapify(arr, n, largest) + + +def heapSort(arr): + n = len(arr) + + + for i in range(n, -1, -1): + heapify(arr, n, i) + + + for i in range(n-1, 0, -1): + arr[i], arr[0] = arr[0], arr[i] # swap + heapify(arr, i, 0) + + +arr = [ 12, 11, 13, 5, 6, 7] +heapSort(arr) +n = len(arr) +print ("Sorted array is") +for i in range(n): + print ("%d" %arr[i]), \ No newline at end of file From 08948e797c50ee979f658b57ac92a44ec97dff1e Mon Sep 17 00:00:00 2001 From: sohail Date: Thu, 11 Oct 2018 20:34:32 +0530 Subject: [PATCH 3/3] Add files via upload