Question

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.

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)**

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 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 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 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 matched indexes. Hint use string.find(search,
begin, end) recursively or in interation

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?

In Python:
Sublist of list A is defined as a list whose elements are all
from list A. For example, suppose list A = [0, 1, 2, 3, 4, 5, 6],
its has many sublists and one of them is [0, 1, 3] because elements
0, 1 and 3 are all contained in list A.
Define a function named returnComplement that accepts two
integer lists as the parameter (one of the list is the sublist of
the other). Suppose names...

Write a program to swap mth and nth elements of a linked
list.
Code needed in java.

Write a Python function that takes a list of integers as a
parameter and returns the sum of the elements in the list. Thank
you.

Write a Python function that takes a list of integers as a
parameter and returns the sum of the elements in the list. Thank
you.

ADVERTISEMENT

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 1 minute ago

asked 40 minutes ago

asked 57 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 2 hours ago

asked 3 hours ago

asked 4 hours ago