Skip to main content

Binary Search and Linear Search

Question 1 

Alice has some cards with numbers written on them. She arranges the cards in decreasing order, and lays them out face down in sequence on a table. she challenges Bob to pick out the card containging a given number by turning over as few cards as possible. Write a function to help Bob locate the card. 

cards= [13, 11, 10, 7, 4, 3,8, 9, 24]

query= 24

output = 4

In [61]:





Linear search



Q 2

how many time the sorted list is rotated:

Linear search





{'input': {'nums': [4, 5, 8, 9, 10]}, 'output': 0} output: 0
{'input': {'nums': [4, 5, 6, 7, 8, 1, 2, 3]}, 'output': 5} output: 5
{'input': {'nums': [7, 3, 5]}, 'output': 1} output: 1
{'input': {'nums': [3, 5, 7, 8, 9, 10]}, 'output': 5} output: 0
Wall time: 999 µs
{'input': {'nums': [4, 5, 8, 9, 10]}, 'output': 0} output: 0
{'input': {'nums': [4, 5, 6, 7, 8, 1, 2, 3]}, 'output': 5} output: 5
{'input': {'nums': [7, 3, 5]}, 'output': 1} output: 1
{'input': {'nums': [3, 5, 7, 8, 9, 10]}, 'output': 5} output: 0
Wall time: 0 ns

Popular posts from this blog

Bagging and Boosting

  What is an Ensemble Method? The ensemble is a method used in the machine learning algorithm. In this method, multiple models or ‘weak learners’ are trained to rectify the same problem and integrated to gain desired results. Weak models combined rightly give accurate models. Bagging Bagging is an acronym for ‘Bootstrap Aggregation’ and is used to decrease the variance in the prediction model. Bagging is a parallel method that fits different, considered learners independently from each other, making it possible to train them simultaneously. Bagging generates additional data for training from the dataset. This is achieved by random sampling with replacement from the original dataset. Sampling with replacement may repeat some observations in each new training data set. Every element in Bagging is equally probable for appearing in a new dataset.  These multi datasets are used to train multiple models in parallel. The average of all the predictions from different ensemble models i...