-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseAndAdd.php
56 lines (54 loc) · 1.23 KB
/
reverseAndAdd.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* Created by PhpStorm.
* User: caldwecr
* Date: 2/10/14
* Time: 12:20 PM
*/
if($argc > 1) {
$input = file_get_contents($argv[1]);
// Split the lines
$input_values = explode(PHP_EOL, $input);
echo doReverseAndAdd($input_values);
}
/**
* @param $valuesArray
* @return string
*
* This method iterates through the values array finding the addition operation count and the reverse and add palindrome for each value
*/
function doReverseAndAdd($valuesArray)
{
$toReturn = '';
foreach($valuesArray as $key => $value) {
$current = $value;
$count = 0;
while(!isPalindrome($current)) {
$current = reverseAndAdd($current);
$count++;
}
$toReturn .= '' . $count . ' ' . $current . "\n";
}
return $toReturn;
}
/**
* @param string $value
* @return int
*
* This method tags a number as a string, and returns the sum of that number and the reverse of the number as an int
*/
function reverseAndAdd($value = '')
{
$normal = (int) $value;
$reverse = (int) strrev($value);
return $normal + $reverse;
}
/**
* @param $value
* @return bool
*/
function isPalindrome($value)
{
$sValue = '' . $value . '';
return $sValue === strrev($sValue);
}