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
What factors should you consider when buying a hard drive?
liubo4ka [24]
1. Capacity
2. RPM
3. Is it a hybrid drive?
4. Is the company trustable?
4 0
3 years ago
In a FOR loop, if you declare a variable in the initializer, it exists only inside of the FOR loop.
Ludmilka [50]

Answer:

True

Explanation:

If you declare a variable in the initializer, it is local to the loop

4 0
2 years ago
Dom is a software developer who likes to tweak his computer's OS to make it work the way he wants it to. The OS he is most likel
Lorico [155]

im guessing linux because of "tweaks and things" but im not sure.

4 0
3 years ago
Which type of USB connector is always plugged into the computer to connect an external device?
ycow [4]
The answer is a micro usb
7 0
4 years ago
Read 2 more answers
Swapping as applied in memory management with an aid of a diagram​
irga5000 [103]

Answer:

what do i help on?

Explanation:

3 0
3 years ago
Other questions:
  • The most common size for a brochure is _____.
    9·1 answer
  • Which letter indicates the amplitude of the wave?<br> a. A<br> b. B<br> c. C<br> d. D
    12·1 answer
  • A computer program uses 3 bits to represent integers. When the program adds the decimal (base 10) numbers 6 and 2, the result is
    13·1 answer
  • Two advantages of Internet are ?​
    7·2 answers
  • A PC is not able to connect to a wired network. Pinging the loopback address is successful, but the gateway cannot be reached. O
    12·1 answer
  • Where the heck is the leader board?
    5·1 answer
  • Which of these is not a valid form<br>layout in Microsoft Access?​
    11·1 answer
  • What is the main difference between a peripheral device and other types of devices? Choose the best answer.
    12·1 answer
  • In attempts to improve their contribution to the environment a company decides to adapt green computing. Which of these techniqu
    13·1 answer
  • Can anyone talk to me?
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!