-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4 different approaches to binary search including my first haskell pr…
…ogram :)
- Loading branch information
Aleksander Sumowski
committed
Jun 13, 2011
1 parent
7834119
commit 6ef2fee
Showing
7 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|