Create test.py
authorNils Forssén <forssennils@gmail.com>
Mon, 17 Jan 2022 14:42:26 +0000 (15:42 +0100)
committerNils Forssén <forssennils@gmail.com>
Mon, 17 Jan 2022 14:42:26 +0000 (15:42 +0100)
test.py [new file with mode: 0644]

diff --git a/test.py b/test.py
new file mode 100644 (file)
index 0000000..ef671a7
--- /dev/null
+++ b/test.py
@@ -0,0 +1,18 @@
+
+def binary_search(key: int, srt_list: list, carry=0):
+    length = len(srt_list)
+    idx = length // 2
+
+    if key > srt_list[idx]:
+        return binary_search(key, srt_list[idx:], carry=carry+idx)
+    elif key < srt_list[idx]:
+        return binary_search(key, srt_list[:idx], carry=carry)
+    else:
+        return carry + idx
+
+
+if __name__ == "__main__":
+    myList = [12, 45, 345, 345, 234, 13, 1, 5]
+    myList.sort()
+    print(myList)
+    print(binary_search(345, myList))