-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountmoney.php
86 lines (77 loc) · 2.33 KB
/
countmoney.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
$padLen = 30;
$numCombos = 0;
function sortData($data) {
function compare($a, $b) {
$valueA = $a['value'];
$valueB = $b['value'];
if ($valueA == $valueB) {
return 0;
}
return ($valueA > $valueB) ? -1 : 1;
}
usort($data, "compare");
return $data;
}
function findChange($coinIndex, $data) {
$dataSize = count($data);
for ($j = 0; $j <= $data[$coinIndex]["maxCount"]; $j++) {
$data[$coinIndex]["count"] = $j;
$nextCoinIndex = $coinIndex + 1;
if ($coinIndex == ($dataSize - 2)) {
$previousTotal = 0;
for ($i = 0; $i < $dataSize - 1; $i++) {
$previousTotal += ($data[$i]["count"] * $data[$i]["value"]);
}
$coinRemainder = $data[$nextCoinIndex]["maxCount"] - $previousTotal;
$data[$nextCoinIndex]["count"] = $coinRemainder;
if ($coinRemainder < 0) {
continue;
} else {
echo "\n";
foreach ($data as $d) {
echo str_pad($d["count"], strlen($d["displayName"]), " ", STR_PAD_LEFT);
}
$GLOBALS["numCombos"]++;
}
} else {
findChange($nextCoinIndex, $data);
}
}
}
function printHeader($data) {
echo "\n";
foreach ($data as $d) {
echo $d['displayName'];
}
}
function getFormattedDataFromUserInput($input) {
$inputArray = explode("," ,$input);
$inputArraySize = sizeof($inputArray);
for ($i = 0, $j = 0; $i < $inputArraySize; $i += 2, $j++) {
$maxCount = $inputArray[$i + 1];
$maxCounts = array_filter($inputArray, 'is_numeric');
$value = max($maxCounts) / $maxCount;
$paddingLength = strlen($inputArray[$i]) + 1;
$displayName = str_pad($inputArray[$i], $paddingLength, " ", STR_PAD_LEFT);
$data[$j] = [
"displayName" => $displayName,
"maxCount" => $maxCount,
"value" => $value,
"count" => 0
];
}
return sortData($data);
}
function run($argv) {
if (count($argv) > 1) {
$data = getFormattedDataFromUserInput($argv[1]);
printHeader($data);
$startingCoinIndex = 0;
findChange($startingCoinIndex, $data);
echo "\n", " Count: ", $GLOBALS["numCombos"], "\n";
} else {
echo "No input provided. Please run again with proper input as 'php countmoney.php <input>'. \n";
}
}
?>