Question

In: Computer Science

Given a string sentence that consists of some words separated by a single space, and a...

Given a string sentence that consists of some words separated by a single space, and a string searchWord.

Write a function isPrefix to check if searchWord is a prefix of any word in sentence. A prefix of a string S is any leading contiguous substring of S. It returns the index of the word in sentence where searchWord is a prefix of this word (1-indexed).

If searchWord is a prefix of more than one word, return the index of the first word (minimum index). If there is no such word return -1.

Input: sentence = this problemo is an easy problemo, searchWord = pro

Output: 2

Explanation: pro is prefix of problemo which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.

Solutions

Expert Solution

Solution:

#function to check weather the word in sentence has prefix as searchWord
def isPrefix(string,searchWord):
#converting the words in a given string in the form of list separated by spaces
lis=list(string.split(" "))
#iterating through all the elements of list
for i in lis:
#startswith( ) checks weather the element starts with searchWord
if i.startswith(searchWord):
#if the word starts with the searchWord then its index is returned
return lis.index(i)+1
#in case there is no word in list that starts with searchWord then return -1
return -1
#taking string as input from user
string=input("Enter the string :")
#taking searchWord as input from user
searchWord=input("Enter the prefix word to be searched :")
#calling function isPrefix to get the required result
print(isPrefix(string,searchWord))

Steps to solve :

  • words in the string is converted in the form of list
  • each word in list is now compared using startswith() with search word
  • if the search is found then index of that word is returned
  • incase there is no word in list that starts with search word then -1 is returned by function

Note: if you find any error in the above code it may be due to improper indentation ,so please provide proper indentation with the help of above image

If you find my answer helpful please give thumbs up .thank you


Related Solutions

The input file Each line of the input file will contain a sentence with words separated...
The input file Each line of the input file will contain a sentence with words separated by one space. Read a line from the listed below  and use a StringTokenizer to extract the words from the line. The input file . Mary had a little lamb whose fl33ce was white as sn0w And everywhere that @Mary went the 1amb was sure to go. Read the above that contains a paragraph of words. Put all the words in an array, put the...
a.The sample space consists of the results of a single probability experiment. True or False b....
a.The sample space consists of the results of a single probability experiment. True or False b. Two events are dependent if the occurrence of one event changes to occurrence of the second event. True or False c. Using a standard deck of 52 cards, the probability of selecting a 4 of diamonds or a 4 of hearts is an example of a mutually exclusive event. True of False d. In a discrete probability distribution, the sum of the probabilities for...
Given a String variable named sentence that has been initialized, write an expression whose value is...
Given a String variable named sentence that has been initialized, write an expression whose value is the the very last character in the String referred to by sentence. write it in python
(i) Define in words the strength of an electric field at a given point in space....
(i) Define in words the strength of an electric field at a given point in space. (ii) An electron experiences an acceleration of magnitude 2.0 × 1013 ms−2 in the positive x-direction, due to a uniform electric field. Calculate the magnitude and direction of the electric field.
A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
Given a relational database that consists of the following relations: Performer (pid: integer, pname: string, years_of_experience:...
Given a relational database that consists of the following relations: Performer (pid: integer, pname: string, years_of_experience: integer, age: integer) Movie (mname: string, genre: string, minutes: integer, release_year: integer, did: integer) Acted (pid: integer, mname: string) Director (did: integer, dname: string, earnings: real) Do the following using your Azure SQL database: a) Use SQL statements to create the relations. b) Populate the relations using SQL statements with the given data posted on Canvas. c) Implement the SQL queries for the following:...
A space station is located in a gravity-free region of space. It consists of a large...
A space station is located in a gravity-free region of space. It consists of a large diameter, hollow thin-walled cylinder which is rotating freely about its axis. It is spinning at a speed such that the apparent gravity on the inner surface is the same as that on earth. The cylinder is of radius r and mass M. (a) What is the minimum total work which had to be done to get the cylinder spinning up to speed. (b) Radial...
The Instructor class consists of a firstname (String), lastname (String), office building (String) and room number...
The Instructor class consists of a firstname (String), lastname (String), office building (String) and room number (int). There is a no-arg constructor that initializes the properties to “Albert”, “Einstein”, “McNair”, 420. There is also a constructor with a parameter for each class property. Finally, there is a toString() method that returns each property separated by an asterisk * . Create a Netbeans project and name it CourseScheduler. Implement the Instructor class in Java. Declare and instantiate two Instructor objects in...
Fill in these missing words in the following statements. Some possible words are given as follows....
Fill in these missing words in the following statements. Some possible words are given as follows. reject, Type I error, p-value, parameter, null hypothesis, Type II error, test statistic (a) A statistical hypothesis is a hypothesis concerning a of a population or distribution. (b) If a test is devised to detect a change in some standard or prevailing value for the parameter in question, then the hypothesis of no change is generally labeled the hypothesis. (c) The error of rejecting...
The microwave spectrum of ? 127? consists of a series of lines separated by 12.8 ??−1....
The microwave spectrum of ? 127? consists of a series of lines separated by 12.8 ??−1. 1. Calculate the rotational constant ? ̃ in ??−1 2. Approximate the bond length of this molecule. 3. Recall in PS36 that you calculated the fundamental wavenumber for this molecule for vibrational transitions under the harmonic oscillator approximation. Using this as your starting point and assuming Δ? = +1, do the following. i. Calculate the values for the 1st 3 lines of both the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT