diff --git a/02_binary/binary_haskel/binary.hs b/02_binary/binary_haskel/binary.hs new file mode 100644 index 0000000..97158be --- /dev/null +++ b/02_binary/binary_haskel/binary.hs @@ -0,0 +1,14 @@ +import qualified Data.Vector.Unboxed as V + + +binarySearch :: Int -> V.Vector Int-> Int +binarySearch number arr = binSearch number arr 0 (V.length arr - 1) + +binSearch :: Int -> V.Vector Int -> Int -> Int -> Int +binSearch number arr low_index high_index + |low_index == high_index = -1 + |number > testedNumber = binSearch number arr (index + 1) high_index + |number < testedNumber = binSearch number arr low_index (index - 1) + |(number == testedNumber) = index + where index = (low_index + high_index) `div` 2 + testedNumber = arr V.! index diff --git a/02_binary/binary_iter/chop.py b/02_binary/binary_iter/chop.py new file mode 100644 index 0000000..2a6fede --- /dev/null +++ b/02_binary/binary_iter/chop.py @@ -0,0 +1,18 @@ +from math import floor +import ipdb + +def chop(number, array): + if not array or len(array)==0: + return -1 + min_index = 0 + max_index = len(array) + index = int(floor((min_index + max_index)/2)) + while array[index] != number and min_index < max_index: + if number>array[index]: + min_index = index + 1 + else: + max_index = index - 1 + index = int(floor((min_index + max_index)/2)) + if array[index] == number: + return index + return -1 diff --git a/02_binary/binary_iter/tests.py b/02_binary/binary_iter/tests.py new file mode 100644 index 0000000..3a6d9f1 --- /dev/null +++ b/02_binary/binary_iter/tests.py @@ -0,0 +1,27 @@ +import unittest +import ipdb +from chop import chop + +class tests(unittest.TestCase): + def test_returns_minus_one_when_array_empty(self): + self.assertEquals(chop(2,[]),-1) + def test_returns_zero_when_only_number(self): + self.assertEquals(chop(2,[2]),0) + def test_returns_minus_one_when_not_single_element(self): + self.assertEquals(chop(2, [3]), -1) + def test_returns_minus_one_when_not_present(self): + self.assertEquals(chop(2, [1,3,4,5,5,5,5,5,56]), -1) + def test_returns_position_when_prestn(self): + self.assertEquals(chop(2, [1,2]), 1) + def test_returns_positions_one(self): + self.assertEquals(chop(1, [1, 3, 5]), 0) + def test_returns_positions_two(self): + self.assertEquals(chop(3, [1, 3, 5]), 1) + def test_returns_positions_three(self): + self.assertEquals(chop(5, [1, 3, 5]), 2) + self.assertEquals(chop(1, [1, 2, 3, 4, 5]), 0) + self.assertEquals(chop(2, [1, 2, 3, 4, 5]), 1) + self.assertEquals(chop(3, [1, 2, 3, 4, 5]), 2) + self.assertEquals(chop(4, [1, 2, 3, 4, 5]), 3) + self.assertEquals(chop(5, [1, 2, 3, 4, 5]), 4) + diff --git a/02_binary/binary_recursive/chop.py b/02_binary/binary_recursive/chop.py new file mode 100644 index 0000000..e34734f --- /dev/null +++ b/02_binary/binary_recursive/chop.py @@ -0,0 +1,15 @@ +from math import floor +def chop(number, array): + if not array or len(array) == 0 or number < array[0] or number > array[len(array) - 1]: + return -1 + return __chop(number, array, 0, len(array)) + +def __chop(number, array, lower_index, upper_index): + index = int(floor((lower_index + upper_index)/2)) + if array[index] == number: + return index + if lower_index == upper_index: + return -1 + if array[index] > number: + return __chop(number, array, lower_index, index - 1) + return __chop(number, array, index + 1, upper_index) diff --git a/02_binary/binary_recursive/tests.py b/02_binary/binary_recursive/tests.py new file mode 100644 index 0000000..f4e271a --- /dev/null +++ b/02_binary/binary_recursive/tests.py @@ -0,0 +1,29 @@ +import unittest +import ipdb +from chop import chop + +class tests(unittest.TestCase): + def test_returns_minus_one_when_array_empty(self): + self.assertEquals(chop(2,[]),-1) + def test_returns_zero_when_only_number(self): + self.assertEquals(chop(2,[2]),0) + def test_returns_minus_one_when_not_single_element(self): + self.assertEquals(chop(2, [3]), -1) + def test_returns_minus_one_when_not_present(self): + self.assertEquals(chop(2, [1,3,4,5,5,5,5,5,56]), -1) + def test_returns_position_when_prestn(self): + self.assertEquals(chop(2, [1,2]), 1) + def test_returns_positions_one(self): + self.assertEquals(chop(1, [1, 3, 5]), 0) + def test_returns_positions_two(self): + self.assertEquals(chop(3, [1, 3, 5]), 1) + def test_returns_positions_three(self): + self.assertEquals(chop(5, [1, 3, 5]), 2) + self.assertEquals(chop(1, [1, 2, 3, 4, 5]), 0) + self.assertEquals(chop(2, [1, 2, 3, 4, 5]), 1) + self.assertEquals(chop(3, [1, 2, 3, 4, 5]), 2) + self.assertEquals(chop(4, [1, 2, 3, 4, 5]), 3) + self.assertEquals(chop(5, [1, 2, 3, 4, 5]), 4) + self.assertEquals(chop(3, [1, 2, 3, 4, 5, 6]), 2) + self.assertEquals(chop(5, [1, 2, 3, 4, 5, 6]), 4) + diff --git a/02_binary/binary_slicing/chop.py b/02_binary/binary_slicing/chop.py new file mode 100644 index 0000000..195ccb0 --- /dev/null +++ b/02_binary/binary_slicing/chop.py @@ -0,0 +1,16 @@ +from math import floor + +def chop(num, array): + return __chop(num, array, 0) + +def __chop(num, array, base_index): + if not array or len(array) == 0: + return -1 + index = len(array) // 2 + testedNum = array[index] + if testedNum == num: + return base_index + index + if num > testedNum: + return __chop(num, array[index + 1 : len(array)], base_index + index + 1) + if num < testedNum: + return __chop(num, array[0: index], base_index) diff --git a/02_binary/binary_slicing/tests.py b/02_binary/binary_slicing/tests.py new file mode 100644 index 0000000..3a6d9f1 --- /dev/null +++ b/02_binary/binary_slicing/tests.py @@ -0,0 +1,27 @@ +import unittest +import ipdb +from chop import chop + +class tests(unittest.TestCase): + def test_returns_minus_one_when_array_empty(self): + self.assertEquals(chop(2,[]),-1) + def test_returns_zero_when_only_number(self): + self.assertEquals(chop(2,[2]),0) + def test_returns_minus_one_when_not_single_element(self): + self.assertEquals(chop(2, [3]), -1) + def test_returns_minus_one_when_not_present(self): + self.assertEquals(chop(2, [1,3,4,5,5,5,5,5,56]), -1) + def test_returns_position_when_prestn(self): + self.assertEquals(chop(2, [1,2]), 1) + def test_returns_positions_one(self): + self.assertEquals(chop(1, [1, 3, 5]), 0) + def test_returns_positions_two(self): + self.assertEquals(chop(3, [1, 3, 5]), 1) + def test_returns_positions_three(self): + self.assertEquals(chop(5, [1, 3, 5]), 2) + self.assertEquals(chop(1, [1, 2, 3, 4, 5]), 0) + self.assertEquals(chop(2, [1, 2, 3, 4, 5]), 1) + self.assertEquals(chop(3, [1, 2, 3, 4, 5]), 2) + self.assertEquals(chop(4, [1, 2, 3, 4, 5]), 3) + self.assertEquals(chop(5, [1, 2, 3, 4, 5]), 4) +