Created
April 26, 2022 22:07
-
-
Save naagaraa/7bf0e2f081672e609b4a65b5d104a503 to your computer and use it in GitHub Desktop.
test 2
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
<?php | |
function checkbus($family = 5, $member = []) | |
{ | |
if ($family == count($member)) { | |
sort($member); | |
$max_familily = 4; | |
// echo "this is valid data"; | |
$random_bus = []; | |
$max_bus = []; | |
foreach ($member as $index => $currenly_member) { | |
if ($currenly_member == $max_familily) { | |
// echo "skip to next family <br>"; | |
array_push( | |
$max_bus, | |
$currenly_member | |
); | |
continue; | |
} elseif ($currenly_member <= $max_familily) { | |
// echo "sum currenly to next member <br>"; | |
array_push($random_bus, $currenly_member); | |
} | |
} | |
$a = array_sum($random_bus) / 4; | |
$b = count($max_bus); | |
echo "Minimum bus required is : " . ceil($a + $b); | |
} else { | |
echo "Input must be equal with count of family"; | |
} | |
} | |
function main($keluarga, $mbr) | |
{ | |
// remove space | |
$string = preg_replace('/\s+/', '', $mbr); | |
$string = str_split($string); | |
$string = array_map("intval", $string); | |
checkbus($keluarga, $string); | |
} | |
$keluarga = 5; | |
$anggota = "1 2 4 3 3"; | |
main($keluarga, $anggota); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment