Created
April 17, 2015 18:33
-
-
Save wookiecooking/0fedd0eb244eb75d6929 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
/** | |
* Closest to Zero | |
* | |
* Write a function that can take an array of floating integers and return the integer that is closest to zero. | |
* Consider the following requirements: | |
* If the array is empty, then return 0 (zero) | |
* If two numbers are as close to zero, consider the positive integer as the closer to zero (e.g. if the | |
* array contains both -5 and 5, then the function returns 5). | |
* | |
* --- USAGE --- | |
* $num =0; | |
* $array = array( 7, -10, 13, 8, 4, -7.2, -12, -3.7, 3.5, -9.6, 6.5, -1.7, -6.2, 7 ); | |
* $lowest = new mathExamples(); | |
* $lowest->setArr($array)->setStart($num)->getLowDom() | |
* | |
*-------------------------------------------------------------------------------------------------------------- | |
* | |
* Separate a list of integers | |
* | |
* Write a function to take the following list and return one array of odd numbers and one array of even | |
* numbers | |
* | |
* --- USAGE --- | |
* $ints = array( 1, 21, 53, 84, 50, 66, 7, 38, 9 ); | |
* $seperator = new mathExamples(); | |
* $seperator->setArr($ints)->oddEvens(); | |
* | |
*-------------------------------------------------------------------------------------------------------------- | |
* | |
* Find the missing number | |
* | |
* Suppose you have an array of 99 numbers. The array contains the digits 1 to 100 with one digit missing. | |
* Describe two different algorithms to compute the missing number. One of these should optimize for low | |
* storage and one of these should optimize for fast processing. | |
* | |
* --- USAGE --- | |
* $start = 1; | |
* $arr = range(1,99); // Missing 100. | |
* | |
* Method one | |
* creates a array of 1 ..100, and checks the differences of values of the passed array, and returns array. | |
* This is the fastest method if you ask me, its a function built into the language. | |
* | |
* $missing = new mathExamples(); | |
* $missing->setArr($arr)->setLow($start)->findMissingOne(); | |
* | |
* Method two | |
* Similar to method one, instead we are writing the if statements. | |
* $missing = new mathExamples(); | |
* $missing->setArr($arr)->setLow($start)->findMissingOne2(); | |
* | |
*-------------------------------------------------------------------------------------------------------------- | |
* | |
* Next Palindrome in PHP | |
* | |
* Write a function that takes an integer and returns the smallest number that is greater than the given | |
* number which is a palendrome. | |
* For example, if the input was 111 the next palindromic number would be 121. | |
* | |
* --- USAGE --- | |
* $ints =111; | |
* $palindrome = new mathExamples(); | |
* $palindrome->setStart($ints)->printMultiTable(); | |
* | |
*-------------------------------------------------------------------------------------------------------------- | |
* | |
* Print a multiplication table in PHP | |
* | |
* Write a program to print out a multiplication table, from 1x1 to 12x12. | |
* | |
* --- USAGE --- | |
* $rows = 10; | |
* $cols = 10; | |
* $printTable = new mathExamples(); | |
* $printTable->setRows($rows)->setCols($cols)->nextPalindrome(); | |
* | |
*-------------------------------------------------------------------------------------------------------------- | |
* | |
* Group Numbers | |
* Split an array of numbers into a specified number of groups so that the sum of all elements in each | |
* group would be as equal as possible. | |
* | |
* --- USAGE --- | |
* $arr = array( 1, 2, 4, 7, 1, 6, 2, 8 ); | |
* $split = 3; | |
* $divider = new Divide(); | |
* $divider->setValues($arr)->setSplit($split)->resolve(); | |
*/ | |
class mathExamples { | |
private arr; | |
private start; | |
private low; | |
private rows; | |
private cols; | |
private max; | |
public function __construct($$arr=array(), $start=1) { | |
// Set values and split if defined | |
$this->setArr($arr); | |
$this->setStart($start); | |
} | |
public function setArr($arr) { | |
$this->arr($arr); | |
return $this | |
} | |
public function setLow($int) { | |
$this->low($int); | |
return $this | |
} | |
public function setMax($int) { | |
$this->max($int); | |
return $this | |
} | |
public function setRows($int) { | |
$this->rows($int); | |
return $this | |
} | |
public function setCols($int) { | |
$this->cols($int); | |
return $this | |
} | |
public function setStart($start) { | |
$this->start($arr); | |
return $this; | |
} | |
public function getLowDom() { | |
$closest = null; | |
foreach($this->$arr as $v) { | |
if($closest == null || abs($this->$start - $closest) > abs($v - $this->start)) { | |
$closest = $v; | |
} | |
} | |
return $closest; | |
} | |
public function oddEvens() { | |
$odd = array(); | |
$even = array(); | |
foreach ($this->$arr as $k => $v) { | |
if ($k % 2 == 0) { | |
$even[] = $v; | |
} | |
else { | |
$odd[] = $v; | |
} | |
} | |
return [$odd, $even] | |
} | |
public function findMissingOne() { | |
return array_diff(range($this->$low ,max($this->$max)), $this->$arr); | |
} | |
public function findMissingTwo() { | |
$missing = []; | |
$max_value = count($this->$arr); | |
if (!(in_array($this->$start, $this->$arr))) { | |
$this->$arr[] = $this->$start | |
} else { | |
if($this->$start <= $max_value -1) { | |
$this->$start++; | |
} | |
} | |
} | |
return $missing | |
} | |
public function nextPalindrome(){ | |
$this->start = abs($this->start); | |
$count = $this->start + 1; | |
while (!isset($x)) { | |
if ($this->start > 8 AND ($count == strrev($count))) { | |
$x = $count; | |
} else if ($this->start < 9) { | |
$x = $this->start + 1; | |
} | |
$count++; | |
} | |
return $x; | |
} | |
public function printMultiTable() { | |
echo "<table>"; | |
for($tr=1;$tr<=$this->$rows;$tr++){ | |
echo "<tr>"; | |
for($td=1;$td<=$this->$cols;$td++){ | |
echo "<td>".$tr*$td."</td>"; | |
} | |
echo "</tr>"; | |
} | |
echo "</table>"; | |
} | |
} | |
Class Divide { | |
private $values; | |
private $split; | |
private $target; | |
public function __construct($values=array(), $split=1) { | |
$this->setValues($values); | |
$this->setSplit($split); | |
} | |
public function setValues($values) { | |
$this->values = $values; | |
return $this; | |
} | |
public function setSplit($split) { | |
if(0 == $split) | |
$split = 1; | |
$this->split = abs($split); | |
return $this; | |
} | |
public function resolve() { | |
$this->target = round(array_sum($this->values) / $this->split); | |
asort($this->values); | |
$this->values = array_reverse($this->values); | |
$groups = array(); | |
for($i=0; $i<$this->split; $i++) { | |
$group = array(); | |
$target = $this->target; | |
while ($target > 0) { | |
$value = $this->_use_closer_to_target($target); | |
$group[] = $value; | |
$target = $target - $value; | |
if(count($this->values) == 0) | |
break; | |
} | |
$groups[$i] = $group; | |
} | |
foreach ($groups as $key => $value) { | |
echo implode(' + ', $value).' = '.array_sum($value); | |
} | |
} | |
private function _use_closer_to_target($target) | |
{ | |
$best_gap = false; | |
$best_gap_key = false; | |
foreach ($this->values as $key => $value) { | |
$gap = abs($target - $value); | |
if(false == $best_gap || $gap < $best_gap) { | |
$best_gap = $gap; | |
$best_gap_key = $key; | |
} | |
if(0 == $gap) | |
break; | |
} | |
$value = $this->values[$best_gap_key]; | |
unset($this->values[$best_gap_key]); | |
return $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment