Question

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 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...
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 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...

  • 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...

  • 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,...

  • 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...

  • 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...

  • Your task is to process a file containing the text of a book available as a...

    Your task is to process a file containing the text of a book available as a file as follows: A function GetGoing(filename) that will take a file name as a parameter. The function will read the contents of the file into a string. Then it prints the number of characters and the number of words in the file. The function also returns the content of the file as a Python list of words in the text file. A function FindMatches(keywordlist,...

  • In Python How do I make the following function below iterate for 20 years. (1996 to...

    In Python How do I make the following function below iterate for 20 years. (1996 to 2016) def getComplaints(year, make, model): url0 = #url url = url0.format(year,make,model) s = requests.get(url).text # this is a CSV string df = pandas.read_csv(StringIO(s)) # use pandas to parse the CSV complaints = df.to_dict('records') # convert to list of dicts return complaints I want to change this function so it is able to search from the year 1996 all the way to 2016 and then...

  • Please write the following code as simple as possible in python: You will need to define...

    Please write the following code as simple as possible in python: You will need to define a function with four arguments. Here is what I used: > def find_matches(file1.txt, output1.txt, strings, value): file1.txt will contain a list of various strings. The program must copy from the first argument, and it should be written in the second argument (the second file, "output1.txt"). The third and fourth arguments will determine which specific strings will be copied over to the second file. For...

  • Could you please do this in Python to run in Spark. For your second assignment I...

    Could you please do this in Python to run in Spark. For your second assignment I would like you to write code that does a word and character count on some input: URL Input File The word count should not only count all occurrences of an individual word, the total number of representations of that word, and a frequency analysis of that word versus the total document. Occurence are considered for spelling, without care of capitalization for example -- "word...

  • C++ program: can you help create a autocorrect code using the cpp code provided and the...

    C++ program: can you help create a autocorrect code using the cpp code provided and the words below using pairs, vectors and unordered map: Objectives To practice using C++ std::pair, std::vector, and std::unordered_map To tie together what we've learned into the context of a real-world application used by millions of people every day Instructions For Full Credit You're given a short list of words in known_words_short.txt that contains a handful of very different words. Assume this short list of words...

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