Python Question: Question 12 Write a function named fileStats. The function file
ID: 3574660 • Letter: P
Question
Python Question:
Question 12
Write a function named fileStats. The function fileStats takes two string parameters: inFile, the name of an input file and outFile, the name of an output file.
The function fileStats should read and analyze the contents of the input file and write the statistics it compiles to the output file. The statistics you should compute about the input file are:
the number of characters;
the number of words;
the number of lines;
the number of digits;
the number of punctuation marks
Each statistic should be written to a separate line of the output file. (Hint: the string class contains constants named punctuation and digits.)
For example, if the input file contains the following lines:
Lord I'm one, Lord I'm two, Lord I'm three, Lord I'm four,
Lord I'm 500 miles from my home.
500 miles, 500 miles, 500 miles, 500 miles
Lord I'm five hundred miles from my home.
Then an output file with the following lines would be correct:
characters 181
words 35
lines 4
digits 15
punctuation 15
Question 13
Write a function named symmetry that takes a string, text, as a parameter and returns a dictionary d. Every letter that is both the first and last letter of some word in text should be a key in d. For example, if text contains the word 'shucks', 's' should be a key in d. The value of 's' in d is the number of words that both begin and end with 's'. The parameter text contains only upper and lower case characters and white space.
The following is an example of correct output:
t = '''The sun did not shine
it was too wet to play
so we sat in the house
all that cold cold wet day
I sat there with Sally
we sat there we two
and I said how I wish
we had something to do'''
print(symmetry(t))
{'d': 1, 'i': 3, 't': 1}
Explanation / Answer
12 : I have used punctuations and digits from string. Code :
from string import punctuation
from string import digits
def fileStats(inFile,outFile):
chars = words = lines = punct = digit = 0
#opening the file
with open(inFile, 'r') as in_file:
#reading the file line by line
for line in in_file:
lines += 1
words += len(line.split())
chars += len(line)
#iterating over line to see if each character is in punctuation or digits.
for c in line :
if c in punctuation :
punct+=1
if c in digits :
digit+=1
f = open(outFile, 'w')
f.write('characters :'+str(chars))
f.write(' ')
f.write('words :'+str(words))
f.write(' ')
f.write('lines :'+str(lines))
f.write(' ')
f.write('digits :'+str(digit))
f.write(' ')
f.write('punctuations :'+str(punct))
f.write(' ')
f.close()
fileStats('inputFile.txt','outputFile.txt')
Sample Input(inputFile.txt) :
Hey..!!
Whats up?
How you doing!?
123 555
Sample Output (outputFile.txt) :
characters :41
words :8
lines :4
digits :6
punctuations :7
13 : adding code with comments.
def symmetry(text) :
#creating dictionary
symmetry_dict = {}
#iterating over each word
for word in text.split() :
#saving firstletter of word
firstLetter = word[0]
#checking if this word ends with this letter
if (word.endswith(firstLetter)) :
#check if firstLetter is present in dict
#if it does add its counter
#else insert in dict
if firstLetter in symmetry_dict :
symmetry_dict[firstLetter] += 1
else :
symmetry_dict[firstLetter] = 1
return symmetry_dict
t ="Write a function named symmetry that takes a string, text, as a parameter and returns a dictionary d. Every letter that is both the first and last letter of some word in text should be a key in d"
print(symmetry(t))
Sampe output :
{'a': 5, 't': 3, 'd': 1}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.