From b47ec7ed4a8579ab71cc26883722b149002ab332 Mon Sep 17 00:00:00 2001 From: Jpsern Date: Mon, 15 Jun 2026 23:54:24 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E4=BA=8C=E5=88=86=E6=8E=A2?= =?UTF-8?q?=E7=B4=A2=E3=81=AE=E7=B0=A1=E5=8D=98=E3=81=AA=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_binary_search.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test/test_binary_search.py diff --git a/test/test_binary_search.py b/test/test_binary_search.py new file mode 100644 index 0000000..bbf3c02 --- /dev/null +++ b/test/test_binary_search.py @@ -0,0 +1,32 @@ +import unittest +from src.tutorial import Tutorial + + +class TestBinarySearch(unittest.TestCase): + def test_binary_search_found_middle(self): + self.assertEqual( + Tutorial.binary_search([1, 3, 5, 7, 9], 5), + 2, + '中央の要素が見つかる' + ) + + def test_binary_search_found_first(self): + self.assertEqual( + Tutorial.binary_search([1, 3, 5, 7, 9], 1), + 0, + '先頭の要素が見つかる' + ) + + def test_binary_search_found_last(self): + self.assertEqual( + Tutorial.binary_search([1, 3, 5, 7, 9], 9), + 4, + '末尾の要素が見つかる' + ) + + def test_binary_search_not_found(self): + self.assertEqual( + Tutorial.binary_search([1, 3, 5, 7, 9], 4), + -1, + '存在しない要素は-1を返す' + ) From 220d5f4489900288398f1f5f67d09950d014820a Mon Sep 17 00:00:00 2001 From: Jpsern Date: Mon, 15 Jun 2026 23:59:48 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=E4=BA=8C=E5=88=86=E6=8E=A2?= =?UTF-8?q?=E7=B4=A2=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tutorial.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/tutorial.py b/src/tutorial.py index 54dc3d1..3241386 100644 --- a/src/tutorial.py +++ b/src/tutorial.py @@ -36,3 +36,19 @@ def convert_to_decimal_number(cls, num: int): for i in range(len(n)): result += int(n[i]) * (2 ** (len(n) - i - 1)) return result + + @classmethod + def binary_search(cls, values: list[int], target: int): + left = 0 + right = len(values) - 1 + + while left <= right: + middle = (left + right) // 2 + if values[middle] == target: + return middle + if values[middle] < target: + left = middle + 1 + else: + right = middle - 1 + + return -1 From ad54aae7024f1c9d0fb8c585a00a72454b5b0dc4 Mon Sep 17 00:00:00 2001 From: Jpsern Date: Tue, 16 Jun 2026 00:01:57 +0900 Subject: [PATCH 3/4] =?UTF-8?q?test:=20=E4=BA=8C=E5=88=86=E6=8E=A2?= =?UTF-8?q?=E7=B4=A2=E3=81=AE=E3=82=B1=E3=83=BC=E3=82=B9=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_binary_search.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test_binary_search.py b/test/test_binary_search.py index bbf3c02..641fb55 100644 --- a/test/test_binary_search.py +++ b/test/test_binary_search.py @@ -30,3 +30,17 @@ def test_binary_search_not_found(self): -1, '存在しない要素は-1を返す' ) + + def test_binary_search_duplicate_values(self): + self.assertEqual( + Tutorial.binary_search([1, 2, 2, 2, 3], 2), + 2, + '重複要素があっても見つかる' + ) + + def test_binary_search_empty_array(self): + self.assertEqual( + Tutorial.binary_search([], 1), + -1, + '空配列では-1を返す' + ) From 0f0611bda65a3c87540bc43018ae90f436bd6831 Mon Sep 17 00:00:00 2001 From: Jpsern Date: Tue, 16 Jun 2026 00:03:20 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=E4=BA=8C=E5=88=86=E6=8E=A2=E7=B4=A2?= =?UTF-8?q?=E3=82=92=E5=B7=A6=E7=AB=AF=E4=B8=80=E8=87=B4=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tutorial.py | 7 +++++-- test/test_binary_search.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/tutorial.py b/src/tutorial.py index 3241386..f847ab2 100644 --- a/src/tutorial.py +++ b/src/tutorial.py @@ -41,14 +41,17 @@ def convert_to_decimal_number(cls, num: int): def binary_search(cls, values: list[int], target: int): left = 0 right = len(values) - 1 + answer = -1 while left <= right: middle = (left + right) // 2 if values[middle] == target: - return middle + answer = middle + right = middle - 1 + continue if values[middle] < target: left = middle + 1 else: right = middle - 1 - return -1 + return answer diff --git a/test/test_binary_search.py b/test/test_binary_search.py index 641fb55..73c9a01 100644 --- a/test/test_binary_search.py +++ b/test/test_binary_search.py @@ -34,8 +34,8 @@ def test_binary_search_not_found(self): def test_binary_search_duplicate_values(self): self.assertEqual( Tutorial.binary_search([1, 2, 2, 2, 3], 2), - 2, - '重複要素があっても見つかる' + 1, + '重複要素は左端のindexを返す' ) def test_binary_search_empty_array(self):