diff --git a/Challenge 1 - Sorting/challenge1.php b/Challenge 1 - Sorting/challenge1.php index a4abe2d..f180cf9 100644 --- a/Challenge 1 - Sorting/challenge1.php +++ b/Challenge 1 - Sorting/challenge1.php @@ -1,2 +1,42 @@ $i; $j-- ) { + if ($arr[$j-1] > $arr[$j]) { + $arr = $this->swap($j-1, $j, $arr); + } + } + } + return $arr; + } + + public function swap($i, $j, array $arr) + { + $tmp = $arr[$i]; + $arr[$i] = $arr[$j]; + $arr[$j] = $tmp; + + return $arr; + } +} + +$rand_arr = array(); + +for ($i = 0; $i < 100; $i++) { + array_push($rand_arr, rand (1, 99)); +} + +echo nl2br("Original Array:\n"); +print_r($rand_arr); + +$sorter = new ArraySort; +echo nl2br("\n\nSorted Array:\n"); +print_r($sorter->sort($rand_arr)); + +?> diff --git a/Challenge 2 - Uppercase/challenge2.php b/Challenge 2 - Uppercase/challenge2.php index a4abe2d..883953a 100644 --- a/Challenge 2 - Uppercase/challenge2.php +++ b/Challenge 2 - Uppercase/challenge2.php @@ -1,2 +1,4 @@ diff --git a/Challenge 4 - 3n+1/Cycle.php b/Challenge 4 - 3n+1/Cycle.php new file mode 100644 index 0000000..0ff4b51 --- /dev/null +++ b/Challenge 4 - 3n+1/Cycle.php @@ -0,0 +1,66 @@ +findCycleLength($i); + $current_max_length = $max_cycle[1]; + if ($current_max_length < $length) { + $max_cycle = array(array($i), $length); + } elseif ($current_max_length == $length) { + array_push($max_cycle[0], $i); + } + $i++; + } + return $max_cycle; + } + + public function findCycleLength($n) + { + if ($n == 1) { + $this->cycle_length_map[$n] = 1; + } + if (!isset($this->cycle_length_map[$n])) { + $length = 1 + $this->findCycleLength($this->nextNumberAfter($n)); + $this->cycle_length_map[$n] = $length; + } + return $this->cycle_length_map[$n]; + } + + public function generateCycle($n) + { + $cycle_array = array(); + array_push($cycle_array, $n); + + $count = 0; + + $i = $n; + while($i != 1) { + $next = $this->nextNumberAfter($i); + array_push($cycle_array, $next); + $i = $next; + + $count++; + } + return $cycle_array; + } + + public function nextNumberAfter($n) + { + if ($n%2 == 0) { + return $n/2; + } else { + return (3 * $n) + 1; + } + } + +} + + +?> diff --git a/Challenge 4 - 3n+1/CycleTest.php b/Challenge 4 - 3n+1/CycleTest.php new file mode 100644 index 0000000..2892a26 --- /dev/null +++ b/Challenge 4 - 3n+1/CycleTest.php @@ -0,0 +1,39 @@ +assertEquals(4, $cycle->nextNumberAfter(8)); + $this->assertEquals(16, $cycle->nextNumberAfter(5)); + $this->assertEquals(1, $cycle->nextNumberAfter(2)); + $this->assertEquals(4, $cycle->nextNumberAfter(1)); + } + + public function testCycleGeneration() + { + $cycle = new Cycle; + $this->assertEquals(array(22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1), $cycle->generateCycle(22)); + } + + public function testCycleLength() + { + $cycle = new Cycle; + $this->assertEquals(1, $cycle->findCycleLength(1)); + $this->assertEquals(2, $cycle->findCycleLength(2)); + $this->assertEquals(16, $cycle->findCycleLength(22)); + } + + public function testMaxCycleLength() + { + $cycle = new Cycle; + $this->assertEquals(array(array(1), 1), $cycle->findMaxCycleLengthBetween(1, 1)); + $this->assertEquals(array(array(9), 20), $cycle->findMaxCycleLengthBetween(16, 1)); + $this->assertEquals(array(array(18, 19), 21), $cycle->findMaxCycleLengthBetween(1, 22)); + $this->assertEquals(array(array(999999), 259), $cycle->findMaxCycleLengthBetween(999999, 1000000)); + } +} +?> + diff --git a/Challenge 4 - 3n+1/challenge4.php b/Challenge 4 - 3n+1/challenge4.php index e69de29..5b8f590 100644 --- a/Challenge 4 - 3n+1/challenge4.php +++ b/Challenge 4 - 3n+1/challenge4.php @@ -0,0 +1,27 @@ + 1000000); +} + +$i = intval($_GET['i']); +$j = intval($_GET['j']); + +if (check_within_bounds($i) && check_within_bounds($i)) { + echo "Inputs must be between 1 and 1000000"; +} else { + $cycle = new Cycle; + $max_length_array = $cycle->findMaxCycleLengthBetween($i, $j); + echo nl2br("Inputs: $i, $j"); + + echo nl2br("\nNumber(s) with highest cycle length: "); + echo implode(", ", $max_length_array[0]); + + echo nl2br("\nHighest cycle length: "); + echo $max_length_array[1]; + + +} + +?>