Repository: keon/algorithms
Pull request #396 (2018-09-02 07:09:33)

Update is_symmetric.py
(Put an `X` inside the `[ ]` to denote check mark `[X]`.)


- [ ] **If creating a new file :**
  - [ ] added links to it in the README files ?
  - [ ] included tests with it ?
  - [ ] added description (overview of algorithm, time and space compleixty, and possible edge case) in docstrings ?

- [ ] **if done some changes :**
  - [ ] wrote short description in the PR explaining what the changes do ?
  - [X ] Fixes #[issue number] if related to any issue

- [ ] **other**


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

* **0** of **0**   changed or added relevant lines in **0** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage remained the same at **71.476%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/18790995/badge)](https://coveralls.io/builds/18790995) |
| :-- | --: |
| Change from base [Build 729](https://coveralls.io/builds/18554511): |  0.0% |
| Covered Lines: | 4305 |
| Relevant Lines: | 6023 |

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


Patch:
diff --git a/README.md b/README.md
index 7ed8694..6d5b747 100644
--- a/README.md
+++ b/README.md
@@ -75,10 +75,10 @@ If you want to uninstall algorithms, it is as simple as:
     - [n_sum](algorithms/arrays/n_sum.py)
 - [backtrack](algorithms/backtrack)
     - [general_solution.md](algorithms/backtrack/)
+    - [add_operators](algorithms/backtrack/add_operators.py)
     - [anagram](algorithms/backtrack/anagram.py)
     - [array_sum_combinations](algorithms/backtrack/array_sum_combinations.py)
     - [combination_sum](algorithms/backtrack/combination_sum.py)
-    - [expression_add_operators](algorithms/backtrack/expression_add_operators.py)
     - [factor_combinations](algorithms/backtrack/factor_combinations.py)
     - [generate_abbreviations](algorithms/backtrack/generate_abbreviations.py)
     - [generate_parenthesis](algorithms/backtrack/generate_parenthesis.py)
@@ -142,7 +142,7 @@ If you want to uninstall algorithms, it is as simple as:
     - [fibonacci](algorithms/dp/fib.py)
 - [graph](algorithms/graph)
     - [check_bipartite](algorithms/graph/check_bipartite.py)
-    - [strongly_connected](algorithms/graph/checkDiGraphStronglyConnected.py)
+    - [strongly_connected](algorithms/graph/check_digraph_strongly_connected.py)
     - [clone_graph](algorithms/graph/clone_graph.py)
     - [cycle_detection](algorithms/graph/cycle_detection.py)
     - [find_all_cliques](algorithms/graph/find_all_cliques.py)
@@ -221,8 +221,8 @@ If you want to uninstall algorithms, it is as simple as:
     - [zigzagiterator](algorithms/queues/zigzagiterator.py)
 - [search](algorithms/search)
     - [binary_search](algorithms/search/binary_search.py)
-    - [first_occurance](algorithms/search/first_occurance.py)
-    - [last_occurance](algorithms/search/last_occurance.py)
+    - [first_occurrence](algorithms/search/first_occurrence.py)
+    - [last_occurrence](algorithms/search/last_occurrence.py)
     - [linear_search](algorithms/search/linear_search.py)
     - [search_insert](algorithms/search/search_insert.py)
     - [two_sum](algorithms/search/two_sum.py)
@@ -269,7 +269,7 @@ If you want to uninstall algorithms, it is as simple as:
     - [is_sorted](algorithms/stack/is_sorted.py)
 - [strings](algorithms/strings)
     - [fizzbuzz](algorithms/strings/fizzbuzz.py)
-    - [delete_reoccurring_characters](algorithms/strings/delete_reoccurring_characters.py)
+    - [delete_reoccurring](algorithms/strings/delete_reoccurring.py)
     - [strip_url_params](algorithms/strings/strip_url_params.py)
     - [validate_coordinates](algorithms/strings/validate_coordinates.py)
     - [domain_extractor](algorithms/strings/domain_extractor.py)
@@ -304,8 +304,8 @@ If you want to uninstall algorithms, it is as simple as:
     - [first_unique_char](algorithms/strings/first_unique_char.py)
     - [repeat_substring](algorithms/strings/repeat_substring.py)     
 - [tree](algorithms/tree)
-    - [bst](algorithms/tree/tree/bst)
-        - [array2bst](algorithms/tree/bst/array2bst.py)
+    - [bst](algorithms/tree/bst)
+        - [array_to_bst](algorithms/tree/bst/array_to_bst.py)
         - [bst_closest_value](algorithms/tree/bst/bst_closest_value.py)
         - [BSTIterator](algorithms/tree/bst/BSTIterator.py)
         - [delete_node](algorithms/tree/bst/delete_node.py)
@@ -334,7 +334,7 @@ If you want to uninstall algorithms, it is as simple as:
         - [add_and_search](algorithms/tree/trie/add_and_search.py)
         - [trie](algorithms/tree/trie/trie.py)
     - [binary_tree_paths](algorithms/tree/binary_tree_paths.py)
-    - [bintree2list](algorithms/tree/bintree2list.py)
+    - [bin_tree_to_list](algorithms/tree/bin_tree_to_list.py)
     - [deepest_left](algorithms/tree/deepest_left.py)
     - [invert_tree](algorithms/tree/invert_tree.py)
     - [is_balanced](algorithms/tree/is_balanced.py)
diff --git a/algorithms/search/linear_search.py b/algorithms/search/linear_search.py
index da4d9b9..cf57fcf 100644
--- a/algorithms/search/linear_search.py
+++ b/algorithms/search/linear_search.py
@@ -5,8 +5,7 @@
 #
 
 def linear_search(array, query):
-    length = len(array)
-    for i in range(length):
+    for i in range(len(array)):
         if array[i] == query:
             return i
 
diff --git a/algorithms/search/search_rotate.py b/algorithms/search/search_rotate.py
index ade0270..a92a15e 100644
--- a/algorithms/search/search_rotate.py
+++ b/algorithms/search/search_rotate.py
@@ -37,9 +37,6 @@ There are 2 possible solution: iterative and recursion.
 Recursion helps you understand better the above algorithm explanation
 """
 def search_rotate(array, val):
-    if not array:
-        return -1
-
     low, high = 0, len(array) - 1
     while low <= high:
         mid = (low + high) // 2
diff --git a/algorithms/sort/radix_sort.py b/algorithms/sort/radix_sort.py
index dd4113d..89a2dbc 100644
--- a/algorithms/sort/radix_sort.py
+++ b/algorithms/sort/radix_sort.py
@@ -1,24 +1,21 @@
 """
 radix sort
-complexity: O(nk) . n is the size of input list and k is the digit length of the number
+complexity: O(nk + n) . n is the size of input list and k is the digit length of the number
 """
 def radix_sort(arr, simulation=False):
-    is_done = False
     position = 1
+    max_number = max(arr)
 
     iteration = 0
     if simulation:
-        print("iteration",iteration,":",*arr)
+        print("iteration", iteration, ":", *arr)
 
-    while not is_done:
+    while position < max_number:
         queue_list = [list() for _ in range(10)]
-        is_done = True
 
         for num in arr:
             digit_number = num // position % 10
             queue_list[digit_number].append(num)
-            if is_done and digit_number > 0:
-                is_done = False
 
         index = 0
         for numbers in queue_list:
@@ -28,7 +25,8 @@ def radix_sort(arr, simulation=False):
 
         if simulation:
             iteration = iteration + 1
-            print("iteration",iteration,":",*arr)
+            print("iteration", iteration, ":", *arr)
 
         position *= 10
     return arr
+    
\ No newline at end of file
diff --git a/algorithms/stack/is_sorted.py b/algorithms/stack/is_sorted.py
index 8824dfa..0a69d83 100644
--- a/algorithms/stack/is_sorted.py
+++ b/algorithms/stack/is_sorted.py
@@ -13,15 +13,15 @@ def is_sorted(stack):
     storage_stack = []
     for i in range(len(stack)):
         if len(stack) == 0:
-            return True
+            break
         first_val = stack.pop()
         if len(stack) == 0:
-            return True
+            break
         second_val = stack.pop()
         if first_val < second_val:
             return False
         storage_stack.append(first_val)
-        storage_stack.append(second_val)
+        stack.append(second_val)
 
     # Backup stack
     for i in range(len(storage_stack)):
diff --git a/algorithms/tree/is_balanced.py b/algorithms/tree/is_balanced.py
index 69a420e..7d3297d 100644
--- a/algorithms/tree/is_balanced.py
+++ b/algorithms/tree/is_balanced.py
@@ -13,7 +13,7 @@ def get_depth(root):
         return 0
     left  = get_depth(root.left)
     right = get_depth(root.right)
-    if abs(left-right) > 1:
+    if abs(left-right) > 1 or left == -1 or right == -1:
         return -1
     return 1 + max(left, right)
 
diff --git a/algorithms/tree/is_symmetric.py b/algorithms/tree/is_symmetric.py
index 3aff3cf..0fd56b9 100644
--- a/algorithms/tree/is_symmetric.py
+++ b/algorithms/tree/is_symmetric.py
@@ -46,7 +46,7 @@ def is_symmetric_iterative(root):
             return False
         if left.val == right.val:
             stack.append([left.left, right.right])
-            stack.append([left.right, right.right])
+            stack.append([left.right, right.left])
         else:
             return False
     return True


Test patch:
diff --git a/tests/test_stack.py b/tests/test_stack.py
index d04b27f..78822bc 100644
--- a/tests/test_stack.py
+++ b/tests/test_stack.py
@@ -25,6 +25,7 @@ class TestSuite(unittest.TestCase):
         # Test case: bottom [6, 3, 5, 1, 2, 4] top
         self.assertFalse(is_sorted([6, 3, 5, 1, 2, 4]))
         self.assertTrue(is_sorted([1, 2, 3, 4, 5, 6]))
+        self.assertFalse(is_sorted([3, 4, 7, 8, 5, 6]))
 
     def test_remove_min(self):
         # Test case: bottom [2, 8, 3, -6, 7, 3] top