1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Lina20 [59]
3 years ago
6

1. Your task is to process a file containing the text of a book available as a file as follows:A function GetGoing(filename) tha

t 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, textlist): The parameter keywordlist contains a list of words. The parameter textlist contains text from a book as a list of words. For each word in keywordlist, the function will print the number of times it occurs in textlist. Nothing is returned.If you implemented the functions correctly then the following program:booktext = GetGoing('constitution.txt') FindMatches (['War', 'Peace', 'Power'], booktext) Will read the file "constitution.txt" and print something like the following:The number of characters is: 42761The number of words is: 7078The number of occurrences of War is: 4The number of occurrences of Peace is: 2The number of occurrences of Power is: 11
Computers and Technology
1 answer:
Irina18 [472]3 years ago
7 0

Answer:

See explaination

Explanation:

#function to count number of characters, words in a given file and returns a list of words

def GetGoing(filename):

file = open(filename)

numbrOfCharacters =0

numberOfWords = 0

WordList = []

#iterate over file line by line

for line in file:

#split the line into words

words = line.strip().split()

#add counn of words to numberOfWords

numberOfWords = numberOfWords + len(words)

#find number of characters in each word and add it to numbrOfCharacters

numbrOfCharacters = numbrOfCharacters + sum(len(word) for word in words)

#append each word from a line to WordList

for word in words:

WordList.append(word)

#display the result

print("The number of characters: ", numbrOfCharacters)

print("The number of Words: ", numberOfWords)

#return the list of words

return WordList

#find matches for keywords given in textlist

def FindMatches(keywordlist, textlist):

for keyword in keywordlist:

keyword = keyword.lower()

print ("The number of occurrences of {} is: {}".format(keyword,len([i for i, s in enumerate(textlist) if s == keyword])))

#main

booktext = GetGoing("constitution.txt")

FindMatches (['War', 'Peace', 'Power'], booktext)

You might be interested in
You have configured your organization's dhcp server to dynamically assign ip addresses to ghcp clients using a lease duration of
Nikitich [7]

When a user's workstation power is turned on and receives addressing information from this DHCP (Dynamic Host Configuration Protocol) server Monday morning at 8 am, the client will contact the DHCP server to renew the lease on Wednesday morning.

7 0
3 years ago
GIVING BRAINLIEST What does output allow a computer to do? Display information Receive information Do complex math problems Do m
kumpel [21]
The answer is display information
4 0
3 years ago
Read 2 more answers
Describe orderly how to save a Word document into a folder​
ivann1987 [24]
In order to do that you go onto the document you are going to save, on the top left it has the word “file”, click on that, once clicked it should say “Save as” and click on that, it will then ask you where you want it or which folder and you click the folder you want and finally click “save”
7 0
3 years ago
Which of the following is a common financial aid scam that students sometimes find on the Internet? Grant and scholarship databa
allsm [11]

Which of the following is a common financial aid scam that students sometimes find on the internet?

All of the above.

6 0
4 years ago
Read 2 more answers
What does a person study when learning computer science?
Nonamiya [84]
Honestly I don’t know for sure but I’m leaning towards B
8 0
3 years ago
Read 2 more answers
Other questions:
  • Which option of the Auto Correct tool enables you to add and delete words that do not follow abbreviation rules?
    11·2 answers
  • Which of the following STEM discoverers developed a new type of computer hardware?
    11·1 answer
  • Rows within a spreadsheet are identified by:
    8·1 answer
  • Plzzzzz help right answer gets Brainly
    10·1 answer
  • The total revenues for a company are $150,223 and the total expenses were 125,766. If you are calculating the net income, which
    10·1 answer
  • True or False: Wikipedia is a reliable source. ​
    5·2 answers
  • have you ever had to adjust your communicatio style to egage a customer or roommate? what was the situation and outcome?
    5·1 answer
  • Write a program to input the TotalCost and display the Assured gift as per the following criteria TotalCost(TC) Assured Gift Les
    6·1 answer
  • PLZ HELP I DIDNT MEAN TO CLICK THAT ANSWR I NEEDD HELP
    15·1 answer
  • 2.Some artists look for ways to extend copyright for longer than the laws normally allow. Why do they do this?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!