Question

In: Computer Science

Write a function that takes a list of strings and outputs them, one per line, in a rectangular frame.

python:

Read the list of strings as separate lines from a text file whose file name is provided at run-time (Dr. Hanna's input file E29.txtPreview the document).

Write a function that takes a list of strings and outputs them, one per line, in a rectangular frame. For

example the list

[ "Hello", "World", "in", "a", "frame" ]

gets output as

*********

* Hello *

* World *

* in *

* a *

* frame *

*********

Solutions

Expert Solution

# reads the file and stores each word seprately in the list

def getList(filename):

    # open the file in read mode

    fptr = open(filename , 'r')

   

    # store the text of the file in strng

    strng = fptr.read()

   

    arr = []

   

    # read the text line by line

    for line in strng.split('\n'):

   

        # read word by word removing all the special characters

        for word in line.split(' '):

            arr.append(word)

           

    return arr

   

def show_rectangular_frame(arr):

    # store the length o the longest string in arr

    max_len = 0

   

    # get the length of longest string

    for i in arr:

   

        if len(i) > max_len:

       

            max_len = len(i)

           

    temp = '*' * ( max_len + 5 )

   

    print(temp)

   

    for i in arr:

   

        print('*', i, ' *')

       

    print(temp)

       

filename = input('Enter the name of the file : ')

arr = getList(filename)

show_rectangular_frame(arr)

Sample Output


Related Solutions

Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a...
Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler. iterator: returns an...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output of longestMorseCodeWords should be an array of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically.For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] let words = ["gin", "zen", "gig", "msg"] longestMorseCodeWords(words) The Morse...
Design a circuit that takes two strings of binary digits and outputs 1 (True) if the...
Design a circuit that takes two strings of binary digits and outputs 1 (True) if the strings “partially” match, and 0 otherwise. To keep this manageable assume the two strings are three bits long, that is a1a2a3 and b1b2b3 where each ai or bi is a one or a zero. The strings are a perfect match if for all i, ai = bi;. The strings are a partial match if at most one i, ai is not equal to bi....
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT