-
Notifications
You must be signed in to change notification settings - Fork 0
/
mth_to_last.php
44 lines (41 loc) · 1018 Bytes
/
mth_to_last.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
<?php
/**
* Created by PhpStorm.
* User: caldwecr
* Date: 2/6/14
* Time: 3:02 PM
* Copyright Cympel Inc
*/
if($argc > 1) {
$input = file_get_contents($argv[1]);
// Split the lines
$input_values = explode(PHP_EOL, $input);
foreach($input_values as $key => $value) {
$v = getMthToLast($value);
if($v !== false) {
echo $v . "\n";
}
}
}
function getMthToLast($input)
{
$numbers = array();
$index = -1;
$toReturn = false;
parseInput($input, $numbers, $index);
if(array_key_exists($index, $numbers)) {
$toReturn = $numbers[$index];
}
return $toReturn;
}
function parseInput($input, &$characters, &$index)
{
$inputArray = explode(' ', $input);
$myCount = count($inputArray) - 1;
$index = (int) $inputArray[$myCount];
//var_dump($inputArray, $index, $myCount);
// Convert numbers to reverse 1 based array
for($i = 0; $i < $myCount; $i++) {
$characters[$myCount - $i] = $inputArray[$i];
}
}