-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb9d5b1
commit 0af034f
Showing
5 changed files
with
193 additions
and
115 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* By adding type hints and enabling strict type checking, code can become | ||
* easier to read, self-documenting and reduce the number of potential bugs. | ||
* By default, type declarations are non-strict, which means they will attempt | ||
* to change the original type to match the type specified by the | ||
* type-declaration. | ||
* | ||
* In other words, if you pass a string to a function requiring a float, | ||
* it will attempt to convert the string value to a float. | ||
* | ||
* To enable strict mode, a single declare directive must be placed at the top | ||
* of the file. | ||
* This means that the strictness of typing is configured on a per-file basis. | ||
* This directive not only affects the type declarations of parameters, but also | ||
* a function's return type. | ||
* | ||
* For more info review the Concept on strict type checking in the PHP track | ||
* <link>. | ||
* | ||
* To disable strict typing, comment out the directive below. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
function total($items) | ||
function total(array $items): int | ||
{ | ||
return calculate($items, 0); | ||
} | ||
$groups = array_fill(1, 5, 0); // Groups of books | ||
$sets = array_fill(1, 5, 0); // Sets by size | ||
|
||
function calculate($items, $total) | ||
{ | ||
if (empty($items)) { | ||
return $total; | ||
// Group the basket | ||
foreach ($items as $bookId) { | ||
$groups[$bookId]++; | ||
} | ||
|
||
$group = array_unique($items); | ||
$totalMin = INF; | ||
|
||
for ($i = count($group); $i > 0; $i--) { | ||
$itemsToRemove = array_flip(array_slice($group, 0, $i)); | ||
$itemsRemaining = array_filter($items, function ($x) use (&$itemsToRemove) { | ||
if (array_key_exists($x, $itemsToRemove)) { | ||
unset($itemsToRemove[$x]); | ||
return false; | ||
// Arrange groups into counts by set size | ||
while (array_sum($groups) > 0) { | ||
$setSize = 0; | ||
foreach ($groups as $key => $count) { | ||
if ($count > 0) { | ||
$setSize++; | ||
$groups[$key]--; | ||
} | ||
return true; | ||
}); | ||
$totalCurrent = calculate($itemsRemaining, $total + totalForGroup($i)); | ||
$totalMin = min([$totalMin, $totalCurrent]); | ||
} | ||
if ($setSize > 0) { | ||
$sets[$setSize]++; | ||
} | ||
} | ||
|
||
return $totalMin; | ||
} | ||
// Replace each 3set+5set with two 4sets | ||
while ($sets[3] > 0 && $sets[5] > 0) { | ||
$sets[3]--; | ||
$sets[5]--; | ||
$sets[4] += 2; | ||
} | ||
|
||
function totalForGroup($count) | ||
{ | ||
$discount = [0, 0, 0.05, 0.1, 0.2, 0.25]; | ||
$price = 8; | ||
return $price * $count * (1 - $discount[$count]); | ||
// Calculate the total cost | ||
$cost = 800 * $sets[1] + | ||
(800 * 2 * 0.95) * $sets[2] + | ||
(800 * 3 * 0.9) * $sets[3] + | ||
(800 * 4 * 0.8) * $sets[4] + | ||
(800 * 5 * 0.75) * $sets[5]; | ||
|
||
return (int)$cost; | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.