Created
October 3, 2018 18:13
-
-
Save flymio/bfd7f9d1ffd1c08d4270af4d1197b7a9 to your computer and use it in GitHub Desktop.
solution for https://www.hackerrank.com/challenges/grading/
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
Новые функции при работе с массивами: | |
end - последний элемент (а я то трахался с тем чтобы -1 получить) | |
current - текущий элемент (когда мы берем элементы через next()) | |
next - следующий элемент массива, не знаю зачем оно может пригодиться если что. |
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 | |
/* | |
* Complete the gradingStudents function below. | |
*/ | |
function gradingStudents($grades) { | |
$newgrades = array(); | |
foreach($grades as $grade){ | |
$newgrade = intval($grade / 5); | |
if ($newgrade * 5 < $grade){ | |
$newgrade++; $newgrade*=5; | |
} | |
$oldgrade = $grade; | |
$diff = $newgrade - $grade; | |
if ($diff < 3 && $newgrade >= 38){ | |
$grade = $newgrade; | |
} | |
array_push($newgrades, $grade); | |
} | |
return $newgrades; | |
} | |
$fptr = fopen(getenv("OUTPUT_PATH"), "w"); | |
$__fp = fopen("php://stdin", "r"); | |
fscanf($__fp, "%d\n", $n); | |
$grades = array(); | |
for ($grades_itr = 0; $grades_itr < $n; $grades_itr++) { | |
fscanf($__fp, "%d\n", $grades_item); | |
$grades[] = $grades_item; | |
} | |
$result = gradingStudents($grades); | |
fwrite($fptr, implode("\n", $result) . "\n"); | |
fclose($__fp); | |
fclose($fptr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment