Repository: keon/algorithms
Pull request #308 (2018-06-05 14:40:24)

Created bogo_sort.py in algorithms/sort
Although bogo_sorting is not useful for sorting, it can be used in training through comparison with the algorithms that actually used. It is also used as an example of logic type programming.

coveralls: ## Pull Request Test Coverage Report for [Build 385](https://coveralls.io/builds/17328761)

* **2** of **2**   **(100.0%)**  changed or added relevant lines in **1** file are covered.
* **1** unchanged line in **1** file lost coverage.
* Overall coverage remained the same at **91.725%**

---


|  Files with Coverage Reduction | New Missed Lines | % |
| :-----|--------------|--: |
| [tests/test_sort.py](https://coveralls.io/builds/17328761/source?filename=tests%2Ftest_sort.py#L70) | 1 | 96.55% |
<!-- | **Total:** | **1** |  | -->

|  Totals | [![Coverage Status](https://coveralls.io/builds/17328761/badge)](https://coveralls.io/builds/17328761) |
| :-- | --: |
| Change from base [Build 384](https://coveralls.io/builds/17326970): |  0.0% |
| Covered Lines: | 2871 |
| Relevant Lines: | 3130 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/README.md b/README.md
index 3c9cefe..72aeaa2 100644
--- a/README.md
+++ b/README.md
@@ -174,6 +174,7 @@ If you want to uninstall algorithms, it is as simple as:
     - [base_conversion](algorithms/maths/base_conversion.py)
     - [combination](algorithms/maths/combination.py)
     - [extended_gcd](algorithms/maths/extended_gcd.py)
+    - [factorial](algorithms/maths/factorial.py)    
     - [gcd/lcm](algorithms/maths/gcd.py)
     - [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
     - [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
@@ -219,6 +220,7 @@ If you want to uninstall algorithms, it is as simple as:
     - [randomized_set](algorithms/set/randomized_set.py)
     - [set_covering](algorithms/set/set_covering.py)
 - [sort](algorithms/sort)
+    - [bogo_sort](algorithms/sort/bogo_sort.py)
     - [bubble_sort](algorithms/sort/bubble_sort.py)
     - [bucket_sort](algorithms/sort/bucket_sort.py)
     - [comb_sort](algorithms/sort/comb_sort.py)
diff --git a/README_CN.md b/README_CN.md
index 8f7aee0..8683e65 100644
--- a/README_CN.md
+++ b/README_CN.md
@@ -165,6 +165,7 @@ pip3 uninstall -y algorithms
 - [math：数学问题](algorithms/maths)
     - [extended_gcd：扩展欧几里得算法](algorithms/maths/extended_gcd.py)
     - [combination](algorithms/maths/combination.py)
+    - [factorial](algorithms/maths/factorial.py)
     - [gcd/lcm：最大公约数和最小公倍数](algorithms/maths/gcd.py)
     - [prime_test：主要测试](algorithms/maths/prime_test.py)
     - [primes_sieve_of_eratosthenes：埃拉托色尼的质数筛](algorithms/maths/primes_sieve_of_eratosthenes.py)
@@ -201,6 +202,7 @@ pip3 uninstall -y algorithms
     - [randomized_set：随机集合](algorithms/set/randomized_set.py)
     - [set_covering：集合覆盖](algorithms/set/set_covering.py)
 - [sort：排序](algorithms/sort)
+    - [bogo_sort](algorithms/sort/bogo_sort.py)
     - [bubble_sort：冒泡排序](algorithms/sort/bubble_sort.py)
     - [bucket_sort](algorithms/sort/bucket_sort.py)
     - [comb_sort：梳排序](algorithms/sort/comb_sort.py)
diff --git a/README_GE.md b/README_GE.md
index da3272b..22c3703 100644
--- a/README_GE.md
+++ b/README_GE.md
@@ -181,6 +181,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
     - [base_conversion](algorithms/maths/base_conversion.py)
     - [combination](algorithms/maths/combination.py)
     - [extended_gcd](algorithms/maths/extended_gcd.py)
+    - [factorial](algorithms/maths/factorial.py)
     - [gcd/lcm](algorithms/maths/gcd.py)
     - [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
     - [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
@@ -226,6 +227,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
     - [randomized_set](algorithms/set/randomized_set.py)
     - [set_covering](algorithms/set/set_covering.py)
 - [sort](algorithms/sort)
+    - [bogo_sort](algorithms/sort/bogo_sort.py)
     - [bubble_sort](algorithms/sort/bubble_sort.py)
     - [bucket_sort](algorithms/sort/bucket_sort.py)
     - [comb_sort](algorithms/sort/comb_sort.py)
diff --git a/README_JP.md b/README_JP.md
index 9eb25e0..c9ba3ff 100644
--- a/README_JP.md
+++ b/README_JP.md
@@ -171,6 +171,7 @@ if __name__ == "__main__":
     - [base_conversion](algorithms/maths/base_conversion.py)
     - [combination](algorithms/maths/combination.py)
     - [extended_gcd](algorithms/maths/extended_gcd.py)
+    - [factorial](algorithms/maths/factorial.py)
     - [gcd/lcm](algorithms/maths/gcd.py)
     - [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
     - [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
@@ -216,6 +217,7 @@ if __name__ == "__main__":
     - [randomized_set](algorithms/set/randomized_set.py)
     - [set_covering](algorithms/set/set_covering.py)
 - [sort : ソート](algorithms/sort)
+    - [bogo_sort](algorithms/sort/bogo_sort.py)
     - [bubble_sort](algorithms/sort/bubble_sort.py)
     - [bucket_sort](algorithms/sort/bucket_sort.py)
     - [comb_sort](algorithms/sort/comb_sort.py)
diff --git a/README_KR.md b/README_KR.md
index 7dd07f2..89f8292 100644
--- a/README_KR.md
+++ b/README_KR.md
@@ -167,6 +167,7 @@ if __name__ == "__main__":
     - [base_conversion](algorithms/maths/base_conversion.py)
     - [combination](algorithms/maths/combination.py)
     - [extended_gcd](algorithms/maths/extended_gcd.py)
+    - [factorial](algorithms/maths/factorial.py)
     - [gcd/lcm](algorithms/maths/gcd.py)
     - [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
     - [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
@@ -212,6 +213,7 @@ if __name__ == "__main__":
     - [randomized_set](algorithms/set/randomized_set.py)
     - [set_covering](algorithms/set/set_covering.py)
 - [sort : 정렬 알고리즘](algorithms/sort)
+    - [bogo_sort](algorithms/sort/bogo_sort.py)
     - [bubble_sort](algorithms/sort/bubble_sort.py)
     - [comb_sort](algorithms/sort/comb_sort.py)
     - [counting_sort](algorithms/sort/counting_sort.py)
diff --git a/algorithms/maths/__init__.py b/algorithms/maths/__init__.py
index d791ad8..2bf44a7 100644
--- a/algorithms/maths/__init__.py
+++ b/algorithms/maths/__init__.py
@@ -1,5 +1,6 @@
 from .base_conversion import *
 from .extended_gcd import *
+from .factorial import *
 from .gcd import *
 from .generate_strobogrammtic import *
 from .is_strobogrammatic import *
diff --git a/algorithms/maths/factorial.py b/algorithms/maths/factorial.py
new file mode 100644
index 0000000..2cd1a34
--- /dev/null
+++ b/algorithms/maths/factorial.py
@@ -0,0 +1,20 @@
+#
+# This function calculates n!
+# Factorial function not works in less than 0
+#
+
+def factorial(n):
+
+    result = 1
+    for i in range(2, n+1):
+        result *= i
+
+    return result
+
+
+def factorial_recur(n):
+    if n == 0:
+        return 1
+    
+    return n * factorial(n-1)
+    
diff --git a/algorithms/sort/__init__.py b/algorithms/sort/__init__.py
index d86e2bc..c7e0de2 100644
--- a/algorithms/sort/__init__.py
+++ b/algorithms/sort/__init__.py
@@ -1,3 +1,4 @@
+from .bogo_sort import *
 from .bubble_sort import *
 from .comb_sort import *
 from .counting_sort import *
diff --git a/algorithms/sort/bogo_sort.py b/algorithms/sort/bogo_sort.py
new file mode 100644
index 0000000..074a6af
--- /dev/null
+++ b/algorithms/sort/bogo_sort.py
@@ -0,0 +1,22 @@
+import random
+
+def bogo_sort(arr):
+    """Bogo Sort
+        Best Case Complexity: O(n)
+        Worst Case Complexity: O(∞)
+        Average Case Complexity: O(n(n-1)!)
+    """
+    def is_sorted(arr):
+        #check the array is inorder
+        i = 0
+        arr_len = len(arr)
+        while i+1 < arr_len:
+            if arr[i] > arr[i+1]:
+                return False
+            i += 1
+        return True
+    while not is_sorted(arr):
+        random.shuffle(arr)
+    return arr
+
+


Test patch:
diff --git a/tests/test_maths.py b/tests/test_maths.py
index fbf0772..a5ca21f 100644
--- a/tests/test_maths.py
+++ b/tests/test_maths.py
@@ -1,6 +1,7 @@
 from algorithms.maths import (
     int2base, base2int,
     extended_gcd,
+    factorial, factorial_recur,
     gcd, lcm,
     gen_strobogrammatic, strobogrammatic_in_range,
     is_strobogrammatic, is_strobogrammatic2,
@@ -205,7 +206,7 @@ class TestRSA(unittest.TestCase):
 
 class TestCombination(unittest.TestCase):
     """[summary]
-    Test for the file rsa.py
+    Test for the file combination.py
 
     Arguments:
         unittest {[type]} -- [description]
@@ -215,5 +216,25 @@ class TestCombination(unittest.TestCase):
         self.assertEqual(10, combination(5, 2))
         self.assertEqual(252, combination(10, 5))
         
+class TestFactorial(unittest.TestCase):
+    """[summary]
+    Test for the file factorial.py
+
+    Arguments:
+        unittest {[type]} -- [description]
+    """
+
+    def test_factorial(self):
+        self.assertEqual(1, factorial(0))
+        self.assertEqual(120, factorial(5))
+        self.assertEqual(3628800, factorial(10))
+        
+    def test_factorial_recur(self):
+        self.assertEqual(1, factorial_recur(0))
+        self.assertEqual(120, factorial_recur(5))
+        self.assertEqual(3628800, factorial_recur(10))
+        
 if __name__ == "__main__":
     unittest.main()
+    
+    
diff --git a/tests/test_sort.py b/tests/test_sort.py
index 4665259..adbb8f1 100644
--- a/tests/test_sort.py
+++ b/tests/test_sort.py
@@ -1,4 +1,5 @@
 from algorithms.sort import (
+    bogo_sort,
     bubble_sort,
     comb_sort,
     counting_sort,
@@ -16,6 +17,10 @@ import unittest
 
 
 class TestSuite(unittest.TestCase):
+    def test_bogo_sort(self):
+        self.assertEqual([1, 5, 23],
+                         bogo_sort([1, 23, 5]))
+   
     def test_bubble_sort(self):
         self.assertEqual([1, 5, 23, 57, 65, 1232],
                          bubble_sort([1, 5, 65, 23, 57, 1232]))