Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Challenge 1 - Sorting/challenge1.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
<?php


class ArraySort
{
public function sort(array $arr)
{
$n = sizeof($arr);
for ($i = 0; $i < $n; $i++) {
for ($j = $n-1; $j > $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));

?>
2 changes: 2 additions & 0 deletions Challenge 2 - Uppercase/challenge2.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<?php
echo preg_replace_callback('/\b(\w)/', function ($match) { return strtoupper($match[0]);} , 'in-touch insight systems inc. programming challenge number 2');

?>
66 changes: 66 additions & 0 deletions Challenge 4 - 3n+1/Cycle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

class Cycle
{
private $cycle_length_map = array();

public function findMaxCycleLengthBetween($a, $b) {
$max_cycle = array(array(0), 0);
$i = min($a, $b);
$j = max($a, $b);
while($i <= $j) {
$length = $this->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;
}
}

}


?>
39 changes: 39 additions & 0 deletions Challenge 4 - 3n+1/CycleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
include_once('cycle.php');

class CycleTest extends PHPUnit_Framework_TestCase
{
public function testNextNumber()
{
$cycle = new Cycle;
$this->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));
}
}
?>

27 changes: 27 additions & 0 deletions Challenge 4 - 3n+1/challenge4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
include 'cycle.php';

function check_within_bounds($input) {
return ($input <= 0) || ($input > 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];


}

?>