Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi it's python I want to write output to the text file. But I am not sure how can I add it please help me

Hi it's python I want to write output to the text file. But I am not sure how can I add it please help me to add write a text file from my code.

Below is my code.

File = open("/Users/Skyler/Downloads/words_file.txt", 'r') #

To_Read = File.read()

words = To_Read.split()

max_word = None # for storing maximum word

min_word = None # for storing minimum word

max_num = -1 # finding maximum number

min_num = 99999999999999999999 # finding and store minimum number

#creating a dictionary to store word length and counts

word_lengths_distribution={i:0 for i in range(21)}

for word in words:

if (len(word) > max_num): # finding max word

max_num = len(word)

max_word = word

if (len(word) < min_num): # finding min word

min_num = len(word)

min_word = word

#checking if there is a key in dictionary with above length (len(word))

if len(word) in word_lengths_distribution:

#exists, increasing the count by 1

word_lengths_distribution[len(word)]+=1

else:

#does not exist, adding as a new entry with value 1

word_lengths_distribution[len(word)]=1

average = sum(len(word) for word in words) / len(words)

print("Total number of words in the file:", str(len(words)))

print("Total number of characters in all the words in the file:",

str(len(To_Read) - To_Read.count(" "))) # prevent counting newline character

print("Average length of words in the file (rounded to two decimal digits):", round(average, 2))

print("Maximum word length :", max_num)

print("Minimum word length :", min_num)

print("Words lengths distribution:")

#looping through a sorted list of word length-count mapping

for i in sorted(word_lengths_distribution):

#displaying length and count of words with that length

print('Length',i,'=',word_lengths_distribution[i],'words')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

1. Television more Over watching faceing of many problems ?

Answered: 1 week ago

Question

Is there a link between chronic stress and memory function?

Answered: 1 week ago

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago