Question

mywords ="You need to ensure that clients can view your site on a smartphone and that...

mywords ="You need to ensure that clients can view your site on a smartphone and that it works as well as it would if the client were accessing it via a computer"

Search word "ensure" and "if" using Recursive function in a binary search algorithm

In Python please, thank you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def binarySearch (word_list, l, r, word): 
  
    # Check if starting index is not greater than end index
    if r >= l: 
        mid = l + (r - l) // 2 # calculate mid index
        if word_list[mid] == word:     # If element is present at the middle itself 
            return mid 
          
        elif word_list[mid] > word: # If element is smaller than mid, then it can only be present in left subarray 
            return binarySearch(word_list, l, mid-1, word) 
  
        else: # Else the element can only be present in right subarray 
            return binarySearch(word_list, mid + 1, r, word) 
  
   # Element is not present in the array 
    else:  
        return -1

# given string
mywords ="You need to ensure that clients can view your site on a smartphone and that it works as well as it would if the client were accessing it via a computer"

# create word list and sort it also
word_list = mywords.split()
word_list.sort()
l = 0
r = len(word_list)-1


# test cases
result = binarySearch(word_list, l, r, "ensure")
if (result == -1): 
    print("Element not present") 
else: 
    print("Element is present")

result = binarySearch(word_list, l, r, "if")
if (result == -1): 
    print("Element not present") 
else: 
    print("Element is present")

FOR HELP PLEASE COMMENT.
THANK YOU.

Element is present Element is present

Add a comment
Know the answer?
Add Answer to:
mywords ="You need to ensure that clients can view your site on a smartphone and that...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need question 9-10 answered. Thank you Question 1 iShow the resulting binary search tree if...

    I need question 9-10 answered. Thank you Question 1 iShow the resulting binary search tree if we are to insert following elements into the tree in given order, [34, 12, 23, 27,31,9,11,45, 20, 37. i) Show the resulting balanced binary search tree if we are to insert following sorted elements into the tree, [9,12,21, 23, 29, 31, 34, 45, 48, 52, 55] iii What is the pre-order traversal of the balanced binary search tree? v) What is the post-order traversal...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

  • PSY278 Wi9 Assignment 2.7.19 (Protected View)-Word TOOLS VIEW Be careful-files from the Internet can contain viruses....

    PSY278 Wi9 Assignment 2.7.19 (Protected View)-Word TOOLS VIEW Be careful-files from the Internet can contain viruses. Unless you need to edit, it's safer to stay in Protected View. Enable Editing TECTED VIEW Worksheet Gender Identity Disorder . Submit the Week 9 Assignment via Blackboard by clicking on the Week 9 Assignment" link Include the proper file naming convention PSY278 wk9 assn smith mmddyyyy Assignment Overview Gender identity disorder has become a controversial topic over the last few decades. For this...

  • PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE...

    PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE TEST CODE FOR IT. 5) Design a recursive function to find the immediate successor of a target integer in an array o:f sorted integers. Here the immediate successor of a target is defined as the smallest number that is no smaller than the target in this sorted array int binarySuccessor(const int anArrayl, const int first, const int last, int target); In the above function...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • cloud computing

    Regional Gardens Ltd is a company that runs a number of related gardening enterprises. It has a large display garden that it opens for public inspection a number of times a year. These enterprises include the Regional Gardens Nursery which sells plants and garden supplies to the public, and Regional Garden Planners which provides garden advice, design and consultancy services.Regional Gardens has engaged you as a consultant to advise them on the use of Cloud Computing in their daily operations...

  • program in python Randomness can be used to improve the performance of deterministic algorithms which need...

    program in python Randomness can be used to improve the performance of deterministic algorithms which need to make many choices. Rather than repeatedly making fixed, hard-coded choices, a pseudorandom number generator can be used to make dynamic, unbiased choices. If the benefits of "good" choices outweigh the costs of "bad" choices, a random selection of good and bad choices can improve the performance of an algorithm Let us explore this with the QUICKSELECT algorithm. Discovered by the influential computer science...

  • There are N sites that need protection (number them 1 to N). Someone is going to pick one of them to attack, and you must pick one to protect. Suppose that the attacker is going to attack site i with probability qi. You plan on selecting a site to protect

    There are N sites that need protection (number them 1 to N). Someone is going to pick one of them to attack, and you must pick one to protect. Suppose that the attacker is going to attack site i with probability qi. You plan on selecting a site to protect, with probability pi of selecting site i. If you select the same site to protect that the attacker chooses to attack, you successfully defend that site. The choice of {qi}...

  • Hello, I need help with the questions below. And, can you show your work so I...

    Hello, I need help with the questions below. And, can you show your work so I can understand how you got your answer. Without the work, I won't be able to comprehend how you split the subnets. Please and thank you. Name Day/Night Homework 5 Subnetting For the Class A network 5.0.0.0 which has been subnetted into 2 subnets, fill in the following chart (dotted decimal). Show your work. (6 points) Network Address Broadcast Address Host Address Range First Subnet...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT