Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize the algorithms #288

Merged
merged 20 commits into from May 31, 2018
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -37,11 +37,11 @@ If you want to use the API algorithms in your code, it is as simple as:
You can test by creating a python file: (Ex: use `merge_sort` in `sort`)

```python3
from sort import merge_sort
from algorithms.sort import merge_sort

if __name__ == "__main__":
my_list = [1, 8, 3, 5, 6]
my_list = merge_sort.merge_sort(my_list)
my_list = merge_sort(my_list)
print(my_list)
```

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions algorithms/bit/__init__.py
@@ -0,0 +1,15 @@
from .add_bitwise_operator import *
from .count_ones import *
from .find_missing_number import *
from .power_of_two import *
from .reverse_bits import *
from .single_number import *
from .single_number2 import *
from .single_number3 import *
from .subsets import *
from .bit_operation import *
from .swap_pair import *
from .find_difference import *
from .has_alternative_bit import *
from .insert_bit import *
from .remove_bit import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 0 additions & 16 deletions bit/single_number2.py → algorithms/bit/single_number2.py
Expand Up @@ -17,22 +17,6 @@
the remaining should be the exact bit of the single number.
In this way, you get the 32 bits of the single number.
"""
def single_number(nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
for i in range(0, 32):
count = 0
for num in nums:
if (num >> i) & 1:
count += 1
res |= ((count % 3) << i)
if res >= 2**31:
res -= 2**32
return res


# Another awesome answer
def single_number2(nums):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions algorithms/graph/__init__.py
@@ -0,0 +1 @@
from .tarjan import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions graph/tarjan.py → algorithms/graph/tarjan.py
Expand Up @@ -3,7 +3,7 @@
in a graph.
https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
"""
from graph.graph import DirectedGraph
from algorithms.graph.graph import DirectedGraph

class Tarjan(object):
def __init__(self, dict_graph):
Expand Down Expand Up @@ -54,4 +54,4 @@ def strongconnect(self, v, sccs):
break
scc.sort()
sccs.append(scc)


File renamed without changes.
3 changes: 3 additions & 0 deletions algorithms/heap/__init__.py
@@ -0,0 +1,3 @@
from .binary_heap import *
from .skyline import *
from .sliding_window_max import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions algorithms/linkedlist/__init__.py
@@ -0,0 +1,8 @@
from .reverse import *
from .is_sorted import *
from .remove_range import *
from .swap_in_pairs import *
from .rotate_list import *
from .is_cyclic import *
from .merge_two_list import *
from .is_palindrome import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions algorithms/maths/__init__.py
@@ -0,0 +1,11 @@
from .base_conversion import *
from .extended_gcd import *
from .gcd import *
from .generate_strobogrammtic import *
from .is_strobogrammatic import *
from .next_perfect_square import *
from .prime_check import *
from .primes_sieve_of_eratosthenes import *
from .pythagoras import *
from .rabin_miller import *
from .rsa import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions algorithms/queues/__init__.py
@@ -0,0 +1,4 @@
from .queue import *
from .max_sliding_window import *
from .reconstruct_queue import *
from .priority_queue import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions algorithms/search/__init__.py
@@ -0,0 +1,8 @@
from .binary_search import *
from .first_occurance import *
from .last_occurance import *
from .search_insert import *
from .two_sum import *
from .search_range import *
from .find_min_rotate import *
from .search_rotate import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions algorithms/sort/__init__.py
@@ -0,0 +1,8 @@
from .bubble_sort import *
from .comb_sort import *
from .counting_sort import *
from .heap_sort import *
from .insertion_sort import *
from .merge_sort import *
from .quick_sort import *
from .selection_sort import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions algorithms/stack/__init__.py
@@ -0,0 +1,10 @@
from .stack import *
from .is_consecutive import *
from .is_sorted import *
from .remove_min import *
from .stutter import *
from .switch_pairs import *
from .valid_parenthesis import *
from .simplify_path import *
from .stack import *
from .ordered_stack import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions algorithms/strings/__init__.py
@@ -0,0 +1,23 @@
from .add_binary import *
from .breaking_bad import *
from .decode_string import *
from .delete_reoccurring import *
from .domain_extractor import *
from .encode_decode import *
from .group_anagrams import *
from .int_to_roman import *
from .is_palindrome import *
from .is_rotated import *
from .license_number import *
from .make_sentence import *
from .merge_string_checker import *
from .multiply_strings import *
from .one_edit_distance import *
from .rabin_karp import *
from .reverse_string import *
from .reverse_vowel import *
from .reverse_words import *
from .roman_to_int import *
from .strip_url_params import *
from .validate_coordinates import *
from .word_squares import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file removed graph/__init__.py
Empty file.
Empty file removed heap/__init__.py
Empty file.
Empty file removed linkedlist/__init__.py
Empty file.
Empty file removed maths/__init__.py
Empty file.
1 change: 0 additions & 1 deletion queues/__init__.py

This file was deleted.

Empty file removed search/__init__.py
Empty file.
Empty file removed sort/__init__.py
Empty file.
1 change: 0 additions & 1 deletion stack/__init__.py

This file was deleted.

32 changes: 17 additions & 15 deletions tests/test_bit.py
@@ -1,18 +1,20 @@
from bit.add_bitwise_operator import add_bitwise_operator
from bit.count_ones import count_ones_iter, count_ones_recur
from bit.find_missing_number import find_missing_number, find_missing_number2
from bit.power_of_two import is_power_of_two
from bit.reverse_bits import reverse_bits
from bit.single_number import single_number
from bit.single_number2 import single_number2
from bit.single_number3 import single_number3
from bit.subsets import subsets
from bit.bit_operation import get_bit, set_bit, clear_bit, update_bit
from bit.swap_pair import swap_pair
from bit.find_difference import find_difference
from bit.has_alternative_bit import has_alternative_bit, has_alternative_bit_fast
from bit.insert_bit import insert_one_bit, insert_mult_bits
from bit.remove_bit import remove_bit
from algorithms.bit import (
add_bitwise_operator,
count_ones_iter, count_ones_recur,
find_missing_number, find_missing_number2,
is_power_of_two,
reverse_bits,
single_number,
single_number2,
single_number3,
subsets,
get_bit, set_bit, clear_bit, update_bit,
swap_pair,
find_difference,
has_alternative_bit, has_alternative_bit_fast,
insert_one_bit, insert_mult_bits,
remove_bit
)

import unittest
import random
Expand Down
4 changes: 2 additions & 2 deletions tests/test_graph.py
@@ -1,4 +1,4 @@
from graph.tarjan import Tarjan
from algorithms.graph import Tarjan

import unittest

Expand Down Expand Up @@ -41,4 +41,4 @@ def test_tarjan_example_2(self):
}

g = Tarjan(example)
self.assertEqual(g.sccs, [['A', 'B', 'E'], ['C', 'D'], ['F', 'G'], ['H']])
self.assertEqual(g.sccs, [['A', 'B', 'E'], ['C', 'D'], ['F', 'G'], ['H']])
8 changes: 5 additions & 3 deletions tests/test_heap.py
@@ -1,6 +1,8 @@
from heap.binary_heap import BinaryHeap
from heap.skyline import get_skyline
from heap.sliding_window_max import max_sliding_window
from algorithms.heap import (
BinaryHeap,
get_skyline,
max_sliding_window
)

import unittest

Expand Down
18 changes: 10 additions & 8 deletions tests/test_linkedlist.py
@@ -1,11 +1,13 @@
from linkedlist.reverse import reverse_list, reverse_list_recursive
from linkedlist.is_sorted import is_sorted
from linkedlist.remove_range import remove_range
from linkedlist.swap_in_pairs import swap_pairs
from linkedlist.rotate_list import rotate_right
from linkedlist.is_cyclic import is_cyclic
from linkedlist.merge_two_list import merge_two_list, merge_two_list_recur
from linkedlist.is_palindrome import is_palindrome, is_palindrome_stack, is_palindrome_dict
from algorithms.linkedlist import (
reverse_list, reverse_list_recursive,
is_sorted,
remove_range,
swap_pairs,
rotate_right,
is_cyclic,
merge_two_list, merge_two_list_recur,
is_palindrome, is_palindrome_stack, is_palindrome_dict
)

import unittest

Expand Down
24 changes: 13 additions & 11 deletions tests/test_maths.py
@@ -1,14 +1,16 @@
from maths.base_conversion import int2base, base2int
from maths.extended_gcd import extended_gcd
from maths.gcd import gcd, lcm
from maths.generate_strobogrammtic import gen_strobogrammatic, strobogrammatic_in_range
from maths.is_strobogrammatic import is_strobogrammatic, is_strobogrammatic2
from maths.next_perfect_square import find_next_square, find_next_square2
from maths.prime_check import prime_check, prime_check2
from maths.primes_sieve_of_eratosthenes import primes
from maths.pythagoras import pythagoras
from maths.rabin_miller import is_prime
from maths.rsa import encrypt, decrypt, generate_key
from algorithms.maths import (
int2base, base2int,
extended_gcd,
gcd, lcm,
gen_strobogrammatic, strobogrammatic_in_range,
is_strobogrammatic, is_strobogrammatic2,
find_next_square, find_next_square2,
prime_check, prime_check2,
primes,
pythagoras,
is_prime,
encrypt, decrypt, generate_key
)

import unittest

Expand Down
11 changes: 7 additions & 4 deletions tests/test_queues.py
@@ -1,7 +1,10 @@
from queues.queue import ArrayQueue, LinkedListQueue
from queues.max_sliding_window import max_sliding_window
from queues.reconstruct_queue import reconstruct_queue
from queues.priority_queue import PriorityQueue, PriorityQueueNode
from algorithms.queues import (
ArrayQueue, LinkedListQueue,
max_sliding_window,
reconstruct_queue,
PriorityQueue, PriorityQueueNode
)

import unittest

class TestQueue(unittest.TestCase):
Expand Down
18 changes: 10 additions & 8 deletions tests/test_search.py
@@ -1,11 +1,13 @@
from search.binary_search import binary_search, binary_search_recur
from search.first_occurance import first_occurance
from search.last_occurance import last_occurance
from search.search_insert import search_insert
from search.two_sum import two_sum, two_sum1, two_sum2
from search.search_range import search_range
from search.find_min_rotate import find_min_rotate, find_min_rotate_recur
from search.search_rotate import search_rotate, search_rotate_recur
from algorithms.search import (
binary_search, binary_search_recur,
first_occurance,
last_occurance,
search_insert,
two_sum, two_sum1, two_sum2,
search_range,
find_min_rotate, find_min_rotate_recur,
search_rotate, search_rotate_recur
)

import unittest

Expand Down
18 changes: 10 additions & 8 deletions tests/test_sort.py
@@ -1,11 +1,13 @@
from sort.bubble_sort import bubble_sort
from sort.comb_sort import comb_sort
from sort.counting_sort import counting_sort
from sort.heap_sort import max_heap_sort, min_heap_sort
from sort.insertion_sort import insertion_sort
from sort.merge_sort import merge_sort
from sort.quick_sort import quick_sort
from sort.selection_sort import selection_sort
from algorithms.sort import (
bubble_sort,
comb_sort,
counting_sort,
max_heap_sort, min_heap_sort,
insertion_sort,
merge_sort,
quick_sort,
selection_sort
)

import unittest

Expand Down
20 changes: 11 additions & 9 deletions tests/test_stack.py
@@ -1,12 +1,14 @@
from stack.is_consecutive import first_is_consecutive, second_is_consecutive
from stack.is_sorted import is_sorted
from stack.remove_min import remove_min
from stack.stutter import first_stutter, second_stutter
from stack.switch_pairs import first_switch_pairs, second_switch_pairs
from stack.valid_parenthesis import is_valid
from stack.simplify_path import simplify_path
from stack.stack import ArrayStack, LinkedListStack
from stack.ordered_stack import OrderedStack
from algorithms.stack import (
first_is_consecutive, second_is_consecutive,
is_sorted,
remove_min,
first_stutter, second_stutter,
first_switch_pairs, second_switch_pairs,
is_valid,
simplify_path,
ArrayStack, LinkedListStack,
OrderedStack
)

import unittest
class TestSuite(unittest.TestCase):
Expand Down