Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

recursive-iterative approach

  • Loading branch information...
commit 1e6a692debd3b3bba75f1a42a3587af51eb9adb8 1 parent ed96488
@pepellou authored
Showing with 41 additions and 0 deletions.
  1. +29 −0 pepellou/php/src/Chop.php
  2. +12 −0 pepellou/php/test/ChopTest.php
View
29 pepellou/php/src/Chop.php
@@ -49,3 +49,32 @@ public function chop(
}
}
+
+class RecursiveIterativeChopper {
+
+ public function _chop(
+ $number,
+ $vector,
+ $left,
+ $right
+ ) {
+ if ($left > $right)
+ return -1;
+ $mid = round(($right + $left) / 2);
+ if ($vector[$mid] == $number)
+ return $mid;
+ if ($vector[$mid] < $number)
+ return $this->_chop($number, $vector, $mid + 1, $right);
+ else
+ return $this->_chop($number, $vector, $left, $mid - 1);
+ }
+
+ public function chop(
+ $number,
+ $vector
+ ) {
+ return $this->_chop($number, $vector, 0, count($vector) - 1);
+ }
+
+
+}
View
12 pepellou/php/test/ChopTest.php
@@ -53,6 +53,18 @@ public function test_recursive(
$this->assertEquals($result, $chopper->chop($number, $vector));
}
+ /**
+ * @dataProvider data_test
+ */
+ public function test_recursive_iterative(
+ $result,
+ $number,
+ $vector
+ ) {
+ $chopper = new RecursiveIterativeChopper();
+ $this->assertEquals($result, $chopper->chop($number, $vector));
+ }
+
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.