Question

In: Computer Science

Part 1: Write a Python function called reduceWhitespace that is given a string line and returns...

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

Solutions

Expert Solution

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


Related Solutions

Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
write an algorithm function called balance() in javascript that takes in a string and returns a...
write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word representing a word in English, and returns the word translated into Pig Latin. Pig Latin is a “language” in which English words are translated according to the following rules: For any word that begins with one or more consonants: move the consonants to the end of the word and append the string ‘ay’. For all other words, append the string ‘way’ to the end....
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Sovle with python 3.8 please. 1, Write a function called same_without_ends that has two string parameters....
Sovle with python 3.8 please. 1, Write a function called same_without_ends that has two string parameters. It should return True if those strings are equal WITHOUT considering the characters on the ends (the beginning character and the last character). It should return False otherwise. For example, "last" and "bask" would be considered equal without considering the characters on the ends. Don't worry about the case where the strings have fewer than three characters. Your function MUST be called same_without_ends. You...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks...
In Python Create a function called ????. The function receives a "string" that represents a year...
In Python Create a function called ????. The function receives a "string" that represents a year (the variable with this "String" will be called uve) and a list containing "strings" representing bank accounts (call this list ????). • Each account is represented by 8 characters. The format of each account number is "** - ** - **", where the asterisks are replaced by numeric characters. o For example, “59-04-23”. • The two central characters of the "string" of each account...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT