Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add def replace and search #24

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions lists/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
4 changes: 0 additions & 4 deletions lists/test_lists.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down