Skip to content

Instantly share code, notes, and snippets.

@sarim
Created October 17, 2015 15:05
Show Gist options
  • Save sarim/6fe11897483734fb36d9 to your computer and use it in GitHub Desktop.
Save sarim/6fe11897483734fb36d9 to your computer and use it in GitHub Desktop.
<?php
$bills = array (
array (
'min' => 1,
'max' => 75,
'price' => 3.8
),
array (
'min' => 76,
'max' => 200,
'price' => 5.14
),
array (
'min' => 201,
'max' => 300,
'price' => 5.36
),
array (
'min' => 301,
'max' => 400,
'price' => 5.63
),
array (
'min' => 401,
'max' => 600,
'price' => 8.7
),
array (
'min' => 601,
'max' => 1000000,
'price' => 9.98
)
);
$billedUnit = 900; //User Input
$processed = $billedUnit;
$totalCost = 0;
foreach ($bills as &$bill) {
$amount = NULL;
if ($processed == 0) {
break;
}
$maxAmount = $bill['max'] - $bill['min'] + 1;
if ($maxAmount <= $processed) {
$amount = $maxAmount;
} else {
$amount = $processed;
}
$bill['amount'] = $amount;
$bill['cost'] = $amount * $bill['price'];
$totalCost += $bill['cost'];
$processed -= $amount;
echo "{$bill['min']}-{$bill['max']}\t\t{$bill['amount']}\t{$bill['cost']}\n";
}
echo "Total:\t$totalCost";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment