Advertisement
Korotkodul

C. Поиск максимумов

Feb 18th, 2025 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def get_extremum_indices(
  4.     ordinates: np.ndarray,
  5. ) -> tuple[np.ndarray, np.ndarray]:
  6.     dif_sign = np.sign(np.array(ordinates[1:] - ordinates[:-1]))
  7.     dif_dif = dif_sign[1:] - dif_sign[:-1]
  8.     mask_max = dif_dif == -2
  9.     mask_min = dif_dif == 2
  10.     indices = np.arange(2, ordinates.shape[0]) - 1 #так как -2 и 2 указывают на предыдущий элемент
  11.     max_indices = indices[mask_max]
  12.     min_indices = indices[mask_min]
  13.     return min_indices, max_indices
  14.  
  15. a = np.array([7, 8, 6, 2, 4, 7, 0, 2, 8, 7, 7, 0, 9, 9, 7])
  16. res = get_extremum_indices(a)
  17. print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement