diff --git a/lists/lists.py b/lists/lists.py index 04d850f..7dcda2d 100644 --- a/lists/lists.py +++ b/lists/lists.py @@ -8,7 +8,30 @@ def replace(input_list: list[int]) -> list[int]: :param input_list: Исходный список :return: Список с замененными элементами """ - pass + + if input_list: + max_numb = input_list[0] + for value in input_list[1:]: + if value > max_numb: + max_numb = value + + for position, value in enumerate(input_list): + if value > 0: + input_list[position] = max_numb + + return input_list + + @staticmethod + def binary_search(sorted_list: list[int], left: int, right: int, query: int) -> int: + if left > right: + return -1 + mid = (left + right) // 2 + if query == sorted_list[mid]: + return mid + elif query < sorted_list[mid]: + return int(ListExercise.binary_search(sorted_list, left, mid - 1, query)) + else: + return int(ListExercise.binary_search(sorted_list, mid + 1, right, query)) @staticmethod def search(input_list: list[int], query: int) -> int: @@ -20,4 +43,10 @@ def search(input_list: list[int], query: int) -> int: :param query: Искомый элемент :return: Номер элемента """ - pass + if input_list: + input_list.sort() + left, right = 0, len(input_list) - 1 + index = ListExercise.binary_search(input_list, left, right, query) + return index + else: + return -1 diff --git a/lists/test_lists.py b/lists/test_lists.py index 7da0c76..a8ac46c 100644 --- a/lists/test_lists.py +++ b/lists/test_lists.py @@ -1,10 +1,7 @@ -import pytest - from .lists import ListExercise class TestListExercise: - @pytest.mark.skip(reason="ListExercise.replace is not implemented") def test_replace(self) -> None: input_list = [3, 2, -8, 4, 100, -6, 7, 8, -99] replaced_list = ListExercise.replace(input_list) @@ -18,7 +15,6 @@ def test_replace(self) -> None: replaced_list = ListExercise.replace(input_list) assert replaced_list == [] - @pytest.mark.skip(reason="ListExercise.search is not implemented") def test_search(self) -> None: assert ListExercise.search([1], 900) == -1 assert ListExercise.search([1], 1) == 0