Assignment3 Python2.Py

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

import re

wordsDict = {}
lettersDict = {}

def cleanupLine(line):
a = '''hello? there A-Z-R_T(,**), world, welcome to python.
this **should? the next line#followed- by@ an#other %million^ %%like $this.'''
stripped_line = re.sub("[^a-zA-Z0-9']+", ' ', line)
return stripped_line

def countWords(line):
"""For a stripped line, this function counts the words and updates
the globla variable wordsDict{}.
Note, we convert upper case words to lower case words"""
global wordsDict
if line is not None:

sentence = line.split()
for rawwords in sentence:
rawwords = rawwords.lower()

if rawwords not in wordsDict:


wordsDict[rawwords] = 0
wordsDict[rawwords] += 1
return wordsDict

def countLetters(line):
"""For a stripped line, this function counts the letters and updates
the globla variable lettersDict{}.
Note, we convert upper case letters to lower case
Note2, numbers and ' should be ignored"""
global lettersDict

if line is not None:

sentence = line.split()
for rawwords in sentence:
rawwords = rawwords.lower()
for singlechar in rawwords:
if singlechar not in lettersDict:
lettersDict[singlechar] = 0
lettersDict[singlechar] += 1
return lettersDict

def readFiles(filename):
handle = open(filename, 'r')
for line in handle:
stripped_line = cleanupLine(line)
countWords(stripped_line)
countLetters(stripped_line)

def results():
answerList = []
global lettersDict
global wordsDict
readFiles("C:\\Users\Desktop\suma kusumam python\input1.txt")
answerList.append(lettersDict['e'])

lettersDict = {}
readFiles("C:\\Users\Desktop\suma kusumam python\input2.txt")
answerList.append(lettersDict['t'])

lettersDict = {}
readFiles("C:\\Users\Desktop\suma kusumam python\input3.txt")
answerList.append(lettersDict['w'])

wordsDict = {}
readFiles("C:\\Users\Desktop\suma kusumam python\input1.txt")
answerList.append(wordsDict['to'])

wordsDict = {}
readFiles("C:\\Users\Desktop\suma kusumam python\input2.txt")
answerList.append(wordsDict['the'])

wordsDict = {}
readFiles("C:\\Users\Desktop\suma kusumam python\input3.txt")
answerList.append(wordsDict['computer'])

return answerList

results()

You might also like