Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Searching data for repetitions is a common task in computer science. In this pro

ID: 3728057 • Letter: S

Question

Searching data for repetitions is a common task in computer science. In this problem, you will write a Python program to do a simple repetition task: given a long sentence string consisting of words separated by a single space, print out each word, in order of first appearance, along with the locations that word appears in the sentence. This is best explained by an example. Consider the string “Computer science is computer science". The first word is "computer" and it appears in the position 0 and in the position 3. "science" appears in positions 1 and 4. "the" appears only in position 2. Sample run of the program: Enter a sentence: Computer science is computer science computer 0 3 science 14 is 2 Do you want to enter another sentence (Y/N)? N Constraints and comments: You can assume the sentence consists of words without any punctuation, with consecutive words separated by a space Do not distinguish between upper and lower case Include a continuation look; keep prompting the user until they choose to quit out of the program You are welcome to use any of the Python string and list functions. Hint: use split to split a sentence into a list of individual words · o »

Explanation / Answer

''' Algorithm: (1) Create a list of words by splitting the sentence by space (2) create a list of all unique words (3) Now search all indexes of unique words in list of all the words and print The end="" in print is used for not changing line after each print, means It does not allow print to change line Sample Input/Output: Enter a sentence: Computer science is computer science computer 0 3 science 1 4 is 2 Do you want to enter another sentence (Y/N)? Alice is alice Enter a sentence: alice 0 2 is 1 Do you want to enter another sentence (Y/N)? n ''' #Your program start from below line to the end of answer #Function to display output def print_word_location(sentence): #Creating list of words using split function words=sentence.split(" ") #Getting all unique words from list words unique_words=[] for word in words: #if word already not in list unique_word thne append if(word.lower() not in unique_words): unique_words.append(word.lower()) #Now we can display indexes of each words #Iterate list of unique words list for word in unique_words: #display word print(word,end=" ") #Search for all indexes of that word in list of words for x in range(0,len(words)): if(words[x].lower()==word): print(x,end=" ") #Change the line print("") #Reasing until user enters N/n while(True): #Taking input from user sentence=input("Enter a sentence: ") #Calling function to display summary print_word_location(sentence) #Asking user for his choide choice=input("Do you want to enter another sentence (Y/N)? ") #if choice is n/N stop asking user if(choice.lower()=='n'): break;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote