Question

Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is...

Using Python, if you could help me with the code

# Create a modified version of the search linear function defined
# above to return all
# occurrences of the search word in the text

# An occurrence is the index of a word in the text that matches your given
# search string.
# e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12]

def search_linear_occurrences(xs, target):
""" Find and return a list of the occurrences of the target in sequence xs """
l = [] # List of occurrences of target in xs

# Code to complete


return l

---------------------------------------------------------------------------------------------------

The code that I have so far

import urllib.request

# Location of book
url = "http://openbookproject.net/thinkcs/python/english3e/_downloads/alice_in_wonderland.txt"
local_copy = "alice_in_wonderland.txt" # Local file to store book in

urllib.request.urlretrieve(url, local_copy) # This function copies the
# thing the url points at into the local file copy

with open(local_copy) as fh: # Read from the local file into a string
alice_text = fh.read()
  
print(alice_text[:100]) # Let's check we've got the words
  
# Make all the alphabet characters lower case
alice_text = alice_text.lower()

alice_words = alice_text.split() # Chop the text up into individual words
# using the split() method, which breaks the string up at whitespace

print(alice_words[:100])

print("There are: " + str(len(alice_words)) + " words")

def search_linear(xs, target):
""" Find and return the index of target in sequence xs """
for (i, v) in enumerate(xs): # enumerate is a cool function which returns for each
# element v in a list a pair (i, v), where i is the index of v in the list
  
if v == target:
return i
return -1

search_linear(alice_words, target="hatter") # Search for the word "hatter" in the text"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

def search_linear_occurrences(xs, target):
    l = []
    for i in range(len(xs)):
        if xs[i] == target:
            l.append(i)
    return l
Add a comment
Know the answer?
Add Answer to:
Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is...
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
  • Using Python, if you could help me with the code # Create a modified version of...

    Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...

  • Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: val...

    Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: valiant paris The valiant Paris seeks for his love. -Enter the text that you want to search for, or DONE when finished: Romeo and Juliet Enter Romeo and Juliet aloft, at the WIndow -- Enter the text that you want to search for, DONE when finished: DONE   # copy the following two lines into any # program...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be...

    Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be song lyrics to your favorite song. With your code, you’ll read from the text file and capture the data into a data structure. Using a data structure, write the code to count the appearance of each unique word in the lyrics. Print out a word frequency list. Example of the word frequency list: 100: frog 94: dog 43: cog 20: bog Advice: You can...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • In this assignment, you will explore more on text analysis and an elementary version of sentiment...

    In this assignment, you will explore more on text analysis and an elementary version of sentiment analysis. Sentiment analysis is the process of using a computer program to identify and categorise opinions in a piece of text in order to determine the writer’s attitude towards a particular topic (e.g., news, product, service etc.). The sentiment can be expressed as positive, negative or neutral. Create a Python file called a5.py that will perform text analysis on some text files. You can...

  • Hi, I need some help finishing the last part of this Python 1 code. The last...

    Hi, I need some help finishing the last part of this Python 1 code. The last few functions are incomplete. Thank you. The instructions were: The program has three functions in it. I’ve written all of break_into_list_of_words()--DO NOT CHANGE THIS ONE. All it does is break the very long poem into a list of individual words. Some of what it's doing will not make much sense to you until we get to the strings chapter, and that's fine--that's part of...

  • Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified...

    Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified letter in a string. This function has to be recursive; you may not use loops!   CodeRunner has been set to search for keywords in your answer that might indicate the use of loops, so please avoid them. For example: Test Result text = 'welcome' letter = 'e' result = freq_of(letter, text) print(f'{text} : {result} {letter}') welcome : 2 e and A list can be...

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