MCQ IN COMPUTER SCIENCE & ENGINEERING

COMPUTER SCIENCE AND ENGINEERING

ALGORITHMS

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Which algorithm will be most useful to guess the number that someone has in their head spanning from 1-100?
A
linear search
B
binary search
C
Either A or B
D
None of the above
Explanation: 

Detailed explanation-1: -A binary search is a dichotomic divide and conquer search algorithm. The maximum number of turns it takes to guess a number from 1 to 100 is log2(100-1 +1)= log2(100) = 7.

Detailed explanation-2: -Divide and Conquer Algorithm for the Guessing Game They came from adding the lowest range “1” and the highest range “100” minus “1", which equals 100 (the total possible numbers you can guess), and then dividing by 2 to get 50.

Detailed explanation-3: -Hence, to get any number between 1 and 100, all you need to know are its 7 digits in binary. So you ask the question if the first digit is 0. Irrespective of the answer being yes or no, you know the digit once the question has been answered.

Detailed explanation-4: -Binary search follows the divide and conquer approach in which the list is divided into two halves, and the item is compared with the middle element of the list. If the match is found then, the location of the middle element is returned.

Detailed explanation-5: -So below is how you can write a program to create a number guessing game using Python: import random n = random. randrange(1, 10) guess = int(input("Enter any number: “)) while n!= guess: if guess < n: print("Too low") guess = int(input("Enter number again: “)) elif guess > n: print("Too high!

There is 1 question to complete.