From: Nils Forssén Date: Mon, 17 Jan 2022 14:42:26 +0000 (+0100) Subject: Create test.py X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=941d1d4b5cb7e5ed57f85997c41cc4c1a6f615d9;p=TDDE44.git Create test.py --- 941d1d4b5cb7e5ed57f85997c41cc4c1a6f615d9 diff --git a/test.py b/test.py new file mode 100644 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))