Created
May 15, 2020 09:55
-
-
Save SamsadSajid/c7ddc527c9fa3619e3009f957e262ec5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lambda_handler(event, context): | |
target_value = event['target_value'] | |
print("Lambda is Invoked with the Integer:: ", target_value) | |
nums = [-1,0,3,5,9,12] | |
found, index = search(nums, target_value) | |
if found: | |
statusCode = 200 | |
msg = 'Target value {0} found in position {1}'.format(target_value, index) | |
else: | |
statusCode = 400 | |
msg = 'Target value {0} was not found in the List!'.format(target_value) | |
return { | |
'statusCode': statusCode, | |
'body': msg | |
} | |
def search(nums, target_value): | |
head, tail = 0, len(nums)-1 | |
while head < tail: | |
mid = (head + tail) // 2 | |
if nums[mid] == target_value: | |
return (True, mid) | |
elif nums[mid] > target_value: | |
tail = mid - 1 | |
else: | |
head = mid + 1 | |
return (False, -1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment