+1 vote
Dec 3, 2019 in Python 864 views

## 2 answers to this question.

This link has the program. It uses a module called bisect in python

https://docs.python.org/2/library/bisect.html

Regards,

RRR

PS: if this answer helped you, please up-vote it. If you have a suggestion, please share the same

• 880 points

Hello @Ramasubrananiam! You can use the binary search algorithm in such situations. When you say the data is stored in a sorted list, the best approach wrt time complexity would be binary search. You can use the following code:

```def binarySearch (arr, l, r, x):
# Check base case
if r >= l:
mid = l + (r - l)/2
# If element is present at the middle itself
if arr[mid] == x:
return mid
# If element is smaller than mid, then it can only be present in left subarray
elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)
# Else the element can only be present in right subarray
else:
return binarySearch(arr, mid+1, r, x)
else:
# Element is not present in the array
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print "Element is present at index %d" % result
else:
print "Element is not present in array"```
answered Dec 3, 2019 by Priyanka

## Write a for loop that prints all elements of a list and their position in the list. a = [4,7,3,2,5,9]

Try using this question by list comprehension: a=[4,7,3,2,5,9] print([x for ...READ MORE

+1 vote

## I am trying to write a program in python for the occurrences of a character given the start and stop values of that string only using while

Here's the code: check = input("Enter the character: ...READ MORE

## Write code to create a list of word lengths for the words in original_str using the accumulation pattern and assign the answer to a variable num_words_list.

Hi,  num_words_list = len(original_str.split()) original_str.split() - split words in ...READ MORE

## How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

## Storing a list of arrays into a CSV file and retrieving it back in a different program

This is the code that I am ...READ MORE

## Write a program to get a string from a given string where all the occurrences of its first char have been changed to '\$' , except the first char itself?

You can use the code below: lis = ...READ MORE

## Is there a way to list out in-built variables and functions of Python?

The in-built variables and functions are defined ...READ MORE