Question

Python: Write a program that performs a logarithmic search on an ordered list of 1000 elements....

Python: Write a program that performs a logarithmic search on an ordered list of 1000 elements. The search on the list should be on a randomly generated number between 1-1000.

Homework Answers

Answer #1

Explanation:

  • binary search is also called as Logarithmic search
  • Here we first create a list with numbers from 1 to 1000 in increasing order.
  • So our list contains numbers from 1 to 1000 in increasing order
  • Then we will input a random number between 1 to 1000  using randint(1,1000) function
  • Then we will use binary search to search for our element and its position
  • Here our element and its position will be same since our list has numbers from 1 to 1000 in increasing order and random search element is between 1 and 1000

Code:

#import random package
import random

#create an empty list
l=[]

#run a for loop from 1 to 1000 and append it to the list l
for i in range(1,1001):
l.append(i)
  
#generate a random number between 1 to 1000
# here n is the element to search
n = random.randint(1,1000)

#assign 0 to first
first = 0

#assign last element index to last. Here since the length of list is 1000.
#last element index is 999
last = 999

#assign (first+last)//2 to middle
middle = (first+last)//2

#use while with condition first< = last
while (first <= last):
#if middle index element is less than n then update first with middle+1
if (l[middle] < n):
first = middle + 1

#else if middle index element is equal to n print element position and break loop
elif(l[middle] == n):
print("{0} found at position: {1}\n".format(n, middle+1))
break
# else update last with middle-1
else:
last = middle - 1;
middle = (first + last)//2
# if first index is greater than last index i means the search element is not present in the list
if (first > last):
print("Element {0} is not present in the list".format(n))

  

Sample O/P1:

522 found at position: 522

Sample O/P1:

321 found at position: 321

Sample O/P1:

639 found at position: 639

Code screenshot:

Sample O/P1 screenshot:

Sample O/P2 screenshot:

Sample O/P3 screenshot:

(If you still have any doubt please comment I will definitely help)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python: Write a program that performs a sequential search on an ordered list of 1000 elements....
Python: Write a program that performs a sequential search on an ordered list of 1000 elements. The search on the list should be on a randomly generated number between 1-1000.
Write a Python program prints out the unique elements of a list lst in a sorted...
Write a Python program prints out the unique elements of a list lst in a sorted manner (i.e it removes duplicates from the list). For example, if list is [10,20,30,20,10,50,60,40,80,50,40], the output should be [10, 20, 30, 40, 50, 60, 80].?
Write a Python program to ask how many elements do users want to create in a...
Write a Python program to ask how many elements do users want to create in a list, then let the user create 2 lists with the number of elements previously entered. Then create and display all combinations of letters, selecting each letter from a different key in a dictionary. Example: How many elements? 2 List 1 = ['a', 'b'] List 2 = ['c', 'd'] Output: ac ad bc bd
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...
Write A python program to return all matches for a given search keyword to return the...
Write A python program to return all matches for a given search keyword to return the matched indexes. Hint use string.find(search, begin, end) recursively or in interation
Write a Python program that does the following: Creates a list with the values 2,4,6,8,10 Gets...
Write a Python program that does the following: Creates a list with the values 2,4,6,8,10 Gets a number as input from the user Prints 'in the list' if the input number is a value in the list, and 'not in the list' otherwise
[Python] what are the program solutions to the promts? Write a program to determine which word...
[Python] what are the program solutions to the promts? Write a program to determine which word is the shortest of the following: apple, banana, peach, plum, grapefruit. Write a program to determine the average number given in a list. The first line of your program should give a name to a list to be averaged: e.g numbers = [3,17,1,44,239]
In Python write a program that prompts the user for six elements and their atomic numbers...
In Python write a program that prompts the user for six elements and their atomic numbers and stores them in a list. Each element and weight pair should also be a list. After the values have been entered, print the list as it entered. Then print the first two characters from the element names (the first character capitalized and the second in lower case) along with the atomic number, starting with the element with the lowest atomic number. You must...
The maximum number of searches required by the binary search algorithm to search an ordered list...
The maximum number of searches required by the binary search algorithm to search an ordered list of n items, where n is a power of 2, what is the time complexity?
Submission Question: Recursion with Trees – Depth First Search & Breadth First Search Write a Python...
Submission Question: Recursion with Trees – Depth First Search & Breadth First Search Write a Python program using the Python IDE based on recursion with trees that is both depth first and breadth first searches. The Python program to be developed will demonstrate the use of both depth-first (DFS) and breadth-first (BFS) searches. A tree node structure will be developed that will be used both for DFS and BFS searches. The program will apply recursion both for the DFS and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT