Removes the item from the sorted array and returns the new array. >>> NumberContainer().binary_search_delete([1,2,3], 2) [1, 3] >>> NumberContainer().binary_search_delete([0, 0, 0], 0) [0, 0] >>> NumberContainer().binary_search_delete([-1, -1
(self, array: list | str | range, item: int)
| 18 | self.indexmap: dict[int, int] = {} |
| 19 | |
| 20 | def binary_search_delete(self, array: list | str | range, item: int) -> list[int]: |
| 21 | """ |
| 22 | Removes the item from the sorted array and returns |
| 23 | the new array. |
| 24 | |
| 25 | >>> NumberContainer().binary_search_delete([1,2,3], 2) |
| 26 | [1, 3] |
| 27 | >>> NumberContainer().binary_search_delete([0, 0, 0], 0) |
| 28 | [0, 0] |
| 29 | >>> NumberContainer().binary_search_delete([-1, -1, -1], -1) |
| 30 | [-1, -1] |
| 31 | >>> NumberContainer().binary_search_delete([-1, 0], 0) |
| 32 | [-1] |
| 33 | >>> NumberContainer().binary_search_delete([-1, 0], -1) |
| 34 | [0] |
| 35 | >>> NumberContainer().binary_search_delete(range(7), 3) |
| 36 | [0, 1, 2, 4, 5, 6] |
| 37 | >>> NumberContainer().binary_search_delete([1.1, 2.2, 3.3], 2.2) |
| 38 | [1.1, 3.3] |
| 39 | >>> NumberContainer().binary_search_delete("abcde", "c") |
| 40 | ['a', 'b', 'd', 'e'] |
| 41 | >>> NumberContainer().binary_search_delete([0, -1, 2, 4], 0) |
| 42 | Traceback (most recent call last): |
| 43 | ... |
| 44 | ValueError: Either the item is not in the array or the array was unsorted |
| 45 | >>> NumberContainer().binary_search_delete([2, 0, 4, -1, 11], -1) |
| 46 | Traceback (most recent call last): |
| 47 | ... |
| 48 | ValueError: Either the item is not in the array or the array was unsorted |
| 49 | >>> NumberContainer().binary_search_delete(125, 1) |
| 50 | Traceback (most recent call last): |
| 51 | ... |
| 52 | TypeError: binary_search_delete() only accepts either a list, range or str |
| 53 | """ |
| 54 | if isinstance(array, (range, str)): |
| 55 | array = list(array) |
| 56 | elif not isinstance(array, list): |
| 57 | raise TypeError( |
| 58 | "binary_search_delete() only accepts either a list, range or str" |
| 59 | ) |
| 60 | |
| 61 | low = 0 |
| 62 | high = len(array) - 1 |
| 63 | |
| 64 | while low <= high: |
| 65 | mid = (low + high) // 2 |
| 66 | if array[mid] == item: |
| 67 | array.pop(mid) |
| 68 | return array |
| 69 | elif array[mid] < item: |
| 70 | low = mid + 1 |
| 71 | else: |
| 72 | high = mid - 1 |
| 73 | raise ValueError( |
| 74 | "Either the item is not in the array or the array was unsorted" |
| 75 | ) |
| 76 | |
| 77 | def binary_search_insert(self, array: list | str | range, index: int) -> list[int]: |