Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. <?php
  2.  
  3. function lowestNumber(array $prices): array
  4. {
  5. $lowestNumber = ['number' => 0, 'index' => 0];
  6.  
  7. for ($i = 0; $i < count($prices); $i++) {
  8. $lowest = $lowestNumber['number'];
  9.  
  10. if ($lowest === 0 || $lowest > $prices[$i]) {
  11. $lowestNumber = ['number' => $prices[$i], 'index' => $i];
  12. }
  13. }
  14.  
  15. return $lowestNumber;
  16. }
  17.  
  18. function clockwise(array $prices): int
  19. {
  20. $lowest = lowestNumber($prices);
  21. if ($lowest['index'] + 1 >= count($prices)) {
  22. throw new \Exception('Не переживай, Биткоин поднимется!');
  23. }
  24.  
  25. $largest = max(array_splice($prices, $lowest['index']));
  26. if (!$largest) {
  27. throw new \Exception('Woops?!');
  28. }
  29.  
  30. return $largest - $lowest['number'];
  31. }
  32.  
  33. function tests()
  34. {
  35. assert(clockwise([9, 8, 6, 7, 11, 10]) === 5);
  36.  
  37. try {
  38. clockwise([9, 8, 6, 7, 11, 5]);
  39. } catch (\Exception $e) {
  40. assert($e->getMessage() === 'Не переживай, Биткоин поднимется!');
  41. }
  42.  
  43. assert(clockwise([3, 9, 8, 6, 7, 12, 10]) === 9);
  44. }
  45.  
  46. tests();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement