Created
September 25, 2023 09:46
-
-
Save 4RSIM3R/5d3e52c2e7ba8dca2b2d6860ca3da48f to your computer and use it in GitHub Desktop.
[PHP] counting altitude
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 | |
// counting the altitude | |
// 1. 'up' berarti naik 1 (+1) | |
// 2. 'down' berarti turun 1 (-1) | |
// 3. 'flat' berarti datar (+0) | |
// anda memiliki sebuah array 'arr' yang berisi : ['up', 'up', 'down', 'flat'] | |
// buatlah program untuk menentukan ketinggian akhir berdasarkan aturan di atas | |
// arr = ['up', 'up', 'down', 'flat'], maka hasil nya adalah 1, karena naik 2 kali, turun 1 kali, dan datar 1 kali -> (2 * 1) - 1 + 0 = 1 | |
$arr = ['up', 'up', 'down', 'flat']; | |
$result1 = 0; // 1 | |
for ($i = 0; $i < count($arr) - 1; $i++) { | |
# code... | |
} | |
echo $result1; // 1 | |
$arr = ['down', 'down', 'down', 'up']; | |
$result2 = 0; // -2 | |
echo $result2; // -2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment