Created
June 7, 2016 06:08
-
-
Save ryanhs/76377a42ef30588ad1e470b76808e7fb to your computer and use it in GitHub Desktop.
ipk
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 A2N($alphabet) { | |
// if alphabet like AB, BA.. | |
if (strlen($alphabet) > 1) { | |
$result = 0; | |
foreach (str_split($alphabet) as $chr) { | |
$result += A2N($chr); | |
} | |
return $result / count(str_split($alphabet)); | |
} | |
$alphabet = strtoupper($alphabet); | |
$result = -(ord($alphabet) - 69); | |
return $result > 0 && $result <= 4 ? $result : 0; | |
} | |
function calculateGPA($grades) { | |
$tmp = [0, 0]; | |
foreach ($grades as $grade) { | |
$tmp[0] += A2N($grade[1]) * $grade[0]; | |
$tmp[1] += $grade[0]; | |
} | |
return number_format($tmp[0] / $tmp[1], 2); | |
} | |
$grades = [ | |
//~ ['credits', 'alphabet'] | |
//~ semester 1 | |
//~ [3, 'A'], // SI | |
//~ [3, 'AB'], // DI | |
//~ [3, 'AB'], // GPU | |
//~ [3, 'C'], // matlan | |
//~ | |
//~ semester 2 | |
//~ [3, 'A'], | |
//~ [3, 'A'], | |
//~ [3, 'A'], | |
//~ [3, 'A'], | |
//~ | |
//~ semester 3 | |
//~ [3, 'A'], | |
//~ [6, 'A'], | |
//~ [3, 'A'], | |
[3, 'B'], | |
[3, 'B'], | |
[3, 'C'], | |
[3, 'A'], | |
[3, 'AB'], | |
[3, 'A'], | |
[3, 'B'], | |
[3, 'A'], | |
[3, 'A'], | |
[3, 'A'], | |
[3, 'C'], | |
]; | |
header('Content-Type: text/plain'); | |
$sks = 0; | |
foreach ($grades as $grade) { | |
$sks += $grade[0]; | |
echo "sks: {$grade[0]}\tgrade: {$grade[1]}" . PHP_EOL; | |
} | |
echo PHP_EOL; | |
echo 'GPA => ' . calculateGPA($grades) . ' / ' . $sks; | |
exit; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment