Respuesta :
Answer:
See explaination
Explanation:
def lineStats(inFile,outFile,threshold):
# opening the input file
f=open(inFile)
# opening the file for writing output
f2=open(outFile,'w')
# reading every line in the file
for line in f:
# removing the space
line=line.strip()
# splitting the line by space
words=line.split()
# writing the number of words to the outfile file
f2.write(str(len(words))+' ')
# counter variable
count=0
# empty list
t=[]
# for every word in the words list
for word in words:
# changing the word to lower case
word=word.lower()
# checking for the length of the word greater than threshold
if len(word) > threshold:
# if the word is not in the list 't'
if word not in t:
# appending the word into the list 't'
t.append(word)
# writing the number of threshold words to the output file
f2.write(str(len(t))+'\n')
# closing the files
f.close()
f2.close()
# testing
if __name__=='__main__':
lineStats('fish.txt','fishOut.txt',3)
CONTENTS OF fish.txt:
Yes some are red and some are blue
Some are old and some are new
CONTENTS OF fishOut.txt:
8 2
7 1
CLASS CODE:
PYTHON CODE(state.py):
class State:
'''
State class is used to represent a state in a country.
'''
# constructor
def __init__(self,name):
self.name=name
self.universities=[]
# method to add university to the state
def addUniversity(self,university):
self.universities.append(university)
# method to check university is in the state or not
def is_home_of(self,university):
if university in self.universities:
return True
else:
return False
PYTHON CODE (state_test.py):
from state import State
newjersey=State('New Jersey')
newjersey.addUniversity('NJIT')
newjersey.addUniversity('Princeton')
print('New Jersey is the home of MIT :',newjersey.is_home_of('MIT'))