In: Computer Science
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:
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."
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!