Question

In: Computer Science

PYTHON Question - An acrostic is a form of writing where a set letter of each...

PYTHON Question - An acrostic is a form of writing where a set letter of each line spells out a word or message. Often acrostics (especially poems) use the first letter to spell out their messages, but other “columns” in the text may be used.

For this lab, we’ll create an acrostic reader. Given the poem and dream data, write a function called print_acrostic() that takes a list that contains two elements:

  1. a string containing the poem/dream, and
  2. an integer that specifies which “column” the acrostic is to be read from.

and prints the acrostic to the screen. For this lab, you’re only given two text sources, but you should write your code in such a way that it can handle an arbitrary number of text/column pairs. An example main() might look like this:

def main():

acrostics = [[POEM, 0], [DREAM, 3]]
for each in acrostics:
print(print_acrostic(each))

main()

The poem is a message from Northeastern and uses the first character of each line (index 0); the dream is a message from New York and uses the 4th character of each line.

For example, given POEM is this

POEM = "Intensive technologies croon.\n\nLocal bandits lesson.\n" \
"Old notebooks emote.\nVideos intersect.\n" \
   ... # etc.

The acrostic at position zero would spell out a phrase: "I LOV..."
"Blank" lines created by two newline characters in a row (e.g. '\n\n') represent a space between words in the phrase (which is why the above example is "I LOV" rather than "ILOV"

POEM = "Intensive technologies croon.\n\nLocal bandits lesson.\n" \ "Old notebooks emote.\nVideos intersect.\n" \ "Early notebooks choreograph.\n\nAsynchronous modems search.\n" \ "Local desktops deliver.\nInteractive videos skill.\n" \ "Grain tablets clap.\nNew technologies cry.\n\n" \ "Commercial kits chant.\nSublingual tablets exalt.\n"

DREAM = "If tax payers tell their tale.\n" \ "Of heated victories against the tax man.\n" \ "A meeting with others who file late.\n\n" \ "There were many reasons to file early...\n" \ "Remembering what H&R Block said: 'Leave it to us!'\n" \ "Ford cars with ejection seats - is that a valid deduction?\n\n" \ "Peas and carrots, please - you barely ate dinner.\n" \ "Floors covered with peas - you couldn't eat them after all.\n" \ "Flux capacitors were science fiction - go back in time!\n\n" \ "Flow with this - the deadline is looming.\n" \ "Trails of paper litter your office.\n Follow the bouncing ball.\n" \ "Follow the rabbit.\n\n Full of ideas; you're distracted again.\n" \ "Scoops of ice cream await, finish the task!\n" \ "Alas, you'll need an extension.\n" \ "Rise up, go to bed - it's January and only a bad tax dream."

Solutions

Expert Solution

Here's the Python Code for the same:

# function to print the acrostic
def print_acrostic(acrosticText):
        text, index, result = acrosticText[0], acrosticText[1], ""
        i = 0

        for j, x in enumerate(text):
                if j > 0 and text[j - 1] == x and x == '\n':
                        result += ' '
                elif x == '\n':
                        i = 0
                elif i == index:
                        result += x
                        i += 1
                else:
                        i += 1

        # acrostic returned from here
        return result

# main function
def main(acrostics):

        # iterating through the acrostics list
        for each in acrostics:
                print(print_acrostic(each))

# driver code
if __name__ == '__main__':

        # Two sample texts
        POEM = "Intensive technologies croon.\n\nLocal bandits lesson.\nOld notebooks emote.\nVideos intersect.\nEarly notebooks choreograph.\n\nAsynchronous modems search.\nLocal desktops deliver.\nInteractive videos skill.\nGrain tablets clap.\nNew technologies cry.\n\nCommercial kits chant.\nSublingual tablets exalt.\n"
        DREAM = "If tax payers tell their tale.\nOf heated victories against the tax man.\nA meeting with others who file late.\n\nThere were many reasons to file early...\nRemembering what H&R Block said: 'Leave it to us!'\nFord cars with ejection seats - is that a valid deduction?\n\nPeas and carrots, please - you barely ate dinner.\nFloors covered with peas - you couldn't eat them after all.\nFlux capacitors were science fiction - go back in time!\n\nFlow with this - the deadline is looming.\nTrails of paper litter your office.\nFollow the bouncing ball.\nFollow the rabbit.\n\nFull of ideas; you're distracted again.\nScoops of ice cream await, finish the task!\nAlas, you'll need an extension.\nRise up, go to bed - it's January and only a bad tax dream."

        # you can pass any number of text/columns pairs in here (I've used the above two)
        acrostics = [[POEM, 0], [DREAM, 3]]

        # calling out main() function
        main(acrostics)

Code Screenshot for fixing any indentation issue (if you encounter then only):

Added comments for better understanding, do refer them. For any doubt, feel free to reach out again in the comments :)

Sample OUTPUT:

Hope it helps, consider hitting a like if it did! :)

Cheers!


Related Solutions

In python this structure can be represented by a set of tuples, where each tuple has...
In python this structure can be represented by a set of tuples, where each tuple has two elements. The following two lines would build the set given above and the print it. >>> L = [(’dog’,’white’), (’cat’,’black’),(’mouse’,’black’)] >>> f = set(L) >>> print(f) {(’cat’, ’black’), (’dog’, ’white’), (’mouse’, ’black’)} In the example, first we store the tuples into a list, and then we create a set with those tuples. There are obviously countless other ways to initialize f and get...
What method is used to set up a file for reading and/or writing? code in python
What method is used to set up a file for reading and/or writing? code in python
Match each term or phrase with its correct description by writing the appropriate letter in the...
Match each term or phrase with its correct description by writing the appropriate letter in the blank: ……….   Business objectives or mission ………. Management accounting ……….   Income Statement ……….   Long-term planning ……….   Accounting ……….   Accountability ……….   Reliability ……….   Maximising business wealth ……….   Financial accounting ……….   Control 1.A process and system for collecting, analysing and communicating economic information 2.Financial information provided to, and useful for, a broad range of users, including managers and others 3.General statements of the business’ aim, typically...
Menu Writing - Python - Please include #Comments in each line for explanation (a) A common...
Menu Writing - Python - Please include #Comments in each line for explanation (a) A common task while writing any software is to display a menu and ask the user for a choice. One such example is the menu on your cellphone. It has messaging, contacts, games, settings, media, and web (and possibly others) as options. Write a function called display menu that displays the menu to the user and allows the user to make a choice (using input). (b)...
Writing python code: Can you do for me this. The question looks bellow Overview : As...
Writing python code: Can you do for me this. The question looks bellow Overview : As a programmer, you have be asked by a good friend to create a program that will allow them to keep track of their personal expenses. They want to be able to enter their expenses for the month, store them in a file, and then have the ability to retrieve them later. However, your friend only wants certain people to have the ability to view...
A password is a string of ten characters, where each character is a lowercase letter, a...
A password is a string of ten characters, where each character is a lowercase letter, a digit, or one of the eight special characters !, @, #, $, %, &, (, and ). A password is called awesome, if it contains at least one digit or at least one special character. Determine the number of awesome passwords.
You are writing a letter to advise a claimant as to whether or not their claim...
You are writing a letter to advise a claimant as to whether or not their claim has been accepted. What type of information must be included in the determination letter? Provide at least eight examples.
Code in python Write a while loop code where it always starts form 2. Then it...
Code in python Write a while loop code where it always starts form 2. Then it randomly chooses a number from 1-4. If the number 4 is hit then it will write “TP” if the number 1 is hit then it will write”SL”. It will rerun the program every time the numbers 1 and 5 are hit. The code should also output every single number that is randomly chosen. 2 of the same numbers can't be chosen back to back...
In the blank to the left of each question, fill in the letter from the following...
In the blank to the left of each question, fill in the letter from the following list which best describes the presentation of the item on the financial statements of Helton Corporation for 2018. W. Change in accounting principle X. Change in accounting estimate Y. Correction of an error Z. None of these choices _____ 1. Raiders manufacture heavy equipment to customer specifications on a contract basis. On the basis that it is preferable, accounting for these long term contracts...
Hello. Please answer the following question in Scheme. Not Python, not any form of C, but...
Hello. Please answer the following question in Scheme. Not Python, not any form of C, but in the language Scheme. If you do not know Scheme, please do not answer the question. I've had to upload it multiple times now. Thank you. 1.2 Write a function called countdown that takes a positive integer and uses a do expression to display a sequence of numbers counting down to 1, each on its own line, then displaying the string "Blastoff!". > (countdown...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT