Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function lowestNumber(array $prices): array
- {
- $lowestNumber = ['number' => 0, 'index' => 0];
- for ($i = 0; $i < count($prices); $i++) {
- $lowest = $lowestNumber['number'];
- if ($lowest === 0 || $lowest > $prices[$i]) {
- $lowestNumber = ['number' => $prices[$i], 'index' => $i];
- }
- }
- return $lowestNumber;
- }
- function clockwise(array $prices): int
- {
- $lowest = lowestNumber($prices);
- if ($lowest['index'] + 1 >= count($prices)) {
- throw new \Exception('Не переживай, Биткоин поднимется!');
- }
- $largest = max(array_splice($prices, $lowest['index']));
- if (!$largest) {
- throw new \Exception('Woops?!');
- }
- return $largest - $lowest['number'];
- }
- function tests()
- {
- assert(clockwise([9, 8, 6, 7, 11, 10]) === 5);
- try {
- clockwise([9, 8, 6, 7, 11, 5]);
- } catch (\Exception $e) {
- assert($e->getMessage() === 'Не переживай, Биткоин поднимется!');
- }
- assert(clockwise([3, 9, 8, 6, 7, 12, 10]) === 9);
- }
- tests();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement