In: Computer Science
Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed.
For example, ‘This line has extra space characters'
'This line has extra space characters’
• Function name: reduceWhitespace
• Number of parameters: one string line
• Return value: one string line The main file should handle the file operations to read from a .txt file you create and call the function from the module you create.
In main you should also print out your text from the file before the function call and print the result after the function call, showing that the function works. Additionally, write the lines with reduce whitespaces into a text file.
Your function should also include the specification (docstring).
Note: the code will be tested by giving it a .txt file, so you should prompt the user for the text file.
Lab 9 Part 2:
In the same module, write a Python function named countAllLetters that is given a string line and returns a list containing every letter and character in the line and the number of times that each letter/character appears (with upper/lower case letters counted together).
For example,
‘This is a short line’ -> [(‘t’,2), (‘h’,2), (‘i’,3), (‘s’,3), (‘ ‘,4), (‘a’,1),
(‘o’,1), (‘r’,1), (‘l’,1), (‘n’,1), (‘e’,1)]
• Function name: countAllLetters
• Number of parameters: one string line
• Return value: a list
The main file should handle the file operations to read from a new .txt file you create and call the function from the module you create. In main, you should print out the text you read from the file, then call the function and then print out the resulting list, showing that the function works. You do not need to write the output into a text file.
Your function should also include the specification (docstring).
Note: the code will be tested by giving it a .txt file, so you should prompt the user for the text file.
Please zip both files
Program
MyString.py module
"""
• Function name: reduceWhitespace
• Number of parameters: one string line
• Return value: one string line with extra white space between words removed
"""
def reduceWhitespace(line):
# first get all the words using string's split function
# split function with no parameter will split string according to whitespaces
# without counting empty string
tokens=str(line).split()
reduced_line=str()
# append all the words with a single space in between words
for item in tokens:
reduced_line=reduced_line+item+" "
# return the new line
return reduced_line
"""
• Function name: countAllLetters
• Number of parameters: one string line
• Return value: a list of all the letters in the input string along with
their count
"""
def countAllLetters(line):
# convert all character to lower
line=str(line).lower()
chars = []
# initialize empty list
letterCount=list()
# for every word in the line
for token in line.split():
# for every character in the word
for c in token:
# if the present character is not in list
if chars.count(c)==0:
# add the character to list
chars.append(c)
# for every character present in string
for c in chars:
# append to letterCount list the character and its count in the string
# count function in string returns the number of occurences of the substring in the string
letterCount.append((c,line.count(c)))
# return the letter count list
return letterCount
main.py
# import the module created
import MyString
file_name=str(input("Enter the file Name:"))
# open 2 files one input file and one output file
fileIn=open(file_name)
fileOut=open("out.txt","w")
# for every line in input file
for line in fileIn:
# call to reduceWhitspace function
print("reduceWhitespace()")
print(line)
reduced_Line=MyString.reduceWhitespace(line)
print("Reduced line is:")
print(reduced_Line)
# write to file the minimized line
fileOut.write(reduced_Line+"\n")
# call to countAllLetters function
print("countAllLetters()")
print(line)
letters_count=MyString.countAllLetters(line)
# print the letters count
print(letters_count)
output
in.txt
This line has extra space characters
This line has extra space characters
This is a short line
out.txt
This line has extra space characters
This line has extra space characters
This is a short line
refer to above screenshots for indentation and output