From fc7f2dfb693b1db23a7abe0d1319ebdaeb90bc2e Mon Sep 17 00:00:00 2001 From: silkirai1812 <86019976+silkirai1812@users.noreply.github.com> Date: Tue, 15 Oct 2024 07:26:22 +0530 Subject: [PATCH 1/3] Create floor|CeilUsingBinarySearch.py Given an sorted array 'arr' of n integers and an integer x. Find the floor and ceiling of x in arr[0..n-1] --- searches/floor|CeilUsingBinarySearch.py | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 searches/floor|CeilUsingBinarySearch.py diff --git a/searches/floor|CeilUsingBinarySearch.py b/searches/floor|CeilUsingBinarySearch.py new file mode 100644 index 000000000000..8e4ff2e74b4f --- /dev/null +++ b/searches/floor|CeilUsingBinarySearch.py @@ -0,0 +1,47 @@ +def findFloor(arr, n, x): + low = 0 + high = n - 1 + ans = -1 + + while low <= high: + mid = (low + high) // 2 + # maybe an answer + if arr[mid] <= x: + ans = arr[mid] + # look for smaller index on the left + low = mid + 1 + else: + high = mid - 1 # look on the right + + return ans + + +def findCeil(arr, n, x): + low = 0 + high = n - 1 + ans = -1 + + while low <= high: + mid = (low + high) // 2 + # maybe an answer + if arr[mid] >= x: + ans = arr[mid] + # look for smaller index on the left + high = mid - 1 + else: + low = mid + 1 # look on the right + + return ans + + +def getFloorAndCeil(arr, n, x): + f = findFloor(arr, n, x) + c = findCeil(arr, n, x) + return (f, c) + +# sample test cas +# arr = [3, 4, 4, 7, 8, 10] +# n = 6 +# x = 5 +# ans = getFloorAndCeil(arr, n, x) +# print("The floor and ceil are:", ans[0], ans[1]) From 06ca19014d08720964cfc814f48f9ad1d6aaf13c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Oct 2024 02:04:26 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/floor|CeilUsingBinarySearch.py | 1 + 1 file changed, 1 insertion(+) diff --git a/searches/floor|CeilUsingBinarySearch.py b/searches/floor|CeilUsingBinarySearch.py index 8e4ff2e74b4f..9908919bcf31 100644 --- a/searches/floor|CeilUsingBinarySearch.py +++ b/searches/floor|CeilUsingBinarySearch.py @@ -39,6 +39,7 @@ def getFloorAndCeil(arr, n, x): c = findCeil(arr, n, x) return (f, c) + # sample test cas # arr = [3, 4, 4, 7, 8, 10] # n = 6 From 93ff8a08d8c74f4f38030947639e3a1403098db2 Mon Sep 17 00:00:00 2001 From: silkirai1812 <86019976+silkirai1812@users.noreply.github.com> Date: Tue, 15 Oct 2024 07:42:03 +0530 Subject: [PATCH 3/3] floor_or_ceil_using_binary_search.py --- ...earch.py => floor_or_ceil_using_binary_search.py} | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) rename searches/{floor|CeilUsingBinarySearch.py => floor_or_ceil_using_binary_search.py} (81%) diff --git a/searches/floor|CeilUsingBinarySearch.py b/searches/floor_or_ceil_using_binary_search.py similarity index 81% rename from searches/floor|CeilUsingBinarySearch.py rename to searches/floor_or_ceil_using_binary_search.py index 9908919bcf31..b0bdbea7e0e1 100644 --- a/searches/floor|CeilUsingBinarySearch.py +++ b/searches/floor_or_ceil_using_binary_search.py @@ -1,4 +1,4 @@ -def findFloor(arr, n, x): +def find_floor(arr, n, x): low = 0 high = n - 1 ans = -1 @@ -16,7 +16,7 @@ def findFloor(arr, n, x): return ans -def findCeil(arr, n, x): +def find_ceil(arr, n, x): low = 0 high = n - 1 ans = -1 @@ -34,9 +34,9 @@ def findCeil(arr, n, x): return ans -def getFloorAndCeil(arr, n, x): - f = findFloor(arr, n, x) - c = findCeil(arr, n, x) +def get_floor_and_ceil(arr, n, x): + f = find_floor(arr, n, x) + c = find_ceil(arr, n, x) return (f, c) @@ -44,5 +44,5 @@ def getFloorAndCeil(arr, n, x): # arr = [3, 4, 4, 7, 8, 10] # n = 6 # x = 5 -# ans = getFloorAndCeil(arr, n, x) +# ans = get_floor_and_ceil(arr, n, x) # print("The floor and ceil are:", ans[0], ans[1])