Dec 3, 2019 in Python 4,414 views

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

• 980 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

) Write a program that sorts a given list, once in ascending and then in descending order, without using the sort() built-in function

Hi, @There, Given an array of size n, ...READ MORE

Write a Python program that takes as input a month and year, determines how many days are in the month, and outputs the number of days. Thirty days are in September, April, June, and November

Write a Python program that takes as ...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