Use Python programing. First please download all 4 files listed below into the s
ID: 3803956 • Letter: U
Question
Use Python programing.
First please download all 4 files listed below into the same folder. Start by reading the poemTranslator.pdf file to learn about the assignment. You will eventually be filling in code in the RPB.py file that reads from one or the other of the poem text files (your choice)!
(Note: if your browser opens files when you click on the links instead of letting you download them, right-click on the file link and select "Save As" or "Save Link As" to download and save the files.)
poem translator pdf -http://www.cs.uni.edu/~diesburg/courses/cs1510_sp17/homework/PA07/poemTranslator.pdf
RPB.py - http://www.cs.uni.edu/~diesburg/courses/cs1510_sp17/homework/PA07/RPB.py (after opening this link copy the whole thing and paste it on python to start working on it)
marry poem - http://www.cs.uni.edu/~diesburg/courses/cs1510_sp17/homework/PA07/maryPoem.txt
Lost generation poem -http://www.cs.uni.edu/~diesburg/courses/cs1510_sp17/homework/PA07/LostGeneration.txt
Explanation / Answer
"""
File: RPB.py
Author: [Your name]
Description:
Date last modified:
03/22/2017 (smd) -- provided modified template
03/xx/2017 (xxx) -- you add comments here
----------------------------------------------------------------
--------------------------------------------------------------
INPUT: A poem from a local text (.txt) file. Each input
file must follow this format:
------------------------------
| Poem Title (on line 1)
| Author Name (one line 2)
| <blank line>
| Line #1 of the poem
| Line #2 of the poem, etc.
OUTPUT: Prints messages to the console and produces new files
that have deformed the poem in various ways.
Backwards lines:
The new filename will have "linesBackwards_" prepended to
the original filename. For example, an original input
filename of "maryPoem.txt" will produce a new file called:
"linesBackward_maryPoem.txt" (in the same directory).
(you say more ...)
---------------------------------------------------------------
"""
# import these modules to help me
import string
import random
# Function: main
# Inputs: Nothing
# Outputs: Returns None, but creates 3 output files that
# change the poemFileName
def main():
"""Runs all the functions that change the poem"""
poemFileName = "maryPoem.txt"
print ("") # blank line
#(1)
print ("(1) Starting Reading Lines Backwards ... ")
readingLinesBackwards( poemFileName )
#(2)
print ("(2) Starting Randomize Lines Backwards ... ")
randomizeLines( poemFileName )
#(3)
print ("(3) Starting Reading Words Backwards ... ")
readingWordsBackwards( poemFileName )
print (" Done. ")
# Function: readingLinesBackwards
# Inputs: poemFileName (must be a text file in the same
# directory as this program
# Outputs: function returns None, but an output file is
# written that is backwards with "backwards_"
# prepended to the file name.
def readingLinesBackwards( poemFileName ):
"""Creates a new file with poemFileName written backwards"""
inputFile = open(poemFileName, 'r')
newFileName = "linesBackwards_" + poemFileName
outputFile = open(newFileName, 'w')
# make an empty list to hold all the lines of a poem
allLines = []
#Get the title and author. The .strip() method takes out the
#newline at the end of the line.
poemTitle = inputFile.readline().strip()
poemAuthor = inputFile.readline().strip()
# skip blank line after the author name
inputFile.readline()
# now read each line of the poem and store the lines in a list
for nextLine in inputFile:
#nextLine = nextLine.strip()
allLines.append(nextLine)
# close the input (original) file
inputFile.close()
# now reverse the lines in the list
allLines.reverse()
# get the total number of lines in this poem
totalLines = len(allLines)
# print title and author
outputFile.write("Lines Backwards ")
outputFile.write('Title: ' + poemTitle + ' ')
outputFile.write('Author: ' + poemAuthor + ' ')
# write the lines in the list to the file, one at a time.
for line in allLines:
outputFile.write(str(totalLines) + " " + line)
totalLines = totalLines - 1
# end of for loop
print(" " + newFileName +" created. ")
outputFile.close()
return None
# Function: randomizeLines
# Inputs: poemFileName (must be a text file in the same
# directory as this program
# Outputs: function returns None, but an output file is
# written that is backwards with "randomize_"
# prepended to the file name.
def randomizeLines( poemFileName ):
"""You need to write the docstring"""
inputFile = open(poemFileName, 'r')
newFileName = "randomize_" + poemFileName
outputFile = open(newFileName, 'w')
# make an empty list to hold all the lines of a poem
allLines = []
#Get the title and author. The .strip() method takes out the
#newline at the end of the line.
poemTitle = inputFile.readline().strip()
poemAuthor = inputFile.readline().strip()
# skip blank line after the author name
inputFile.readline()
# now read each line of the poem and store the lines in a list
for nextLine in inputFile:
#nextLine = nextLine.strip()
allLines.append(nextLine)
# close the input (original) file
inputFile.close()
# now reverse the lines in the list
random.shuffle(allLines)
# print title and author
outputFile.write("Lines Backwards ")
outputFile.write('Title: ' + poemTitle + ' ')
outputFile.write('Author: ' + poemAuthor + ' ')
# write the lines in the list to the file, one at a time.
for line in allLines:
outputFile.write(line)
# end of for loop
print(" " + newFileName +" created. ")
outputFile.close()
return None
# Function: readingWordsBackwards
# Inputs: poemFileName (must be a text file in the same
# directory as this program
# Outputs: function returns None, but an output file is
# written that is backwards with "backwardsWords_"
# prepended to the file name.
def readingWordsBackwards( poemFileName ):
"""You need to write the docstring"""
inputFile = open(poemFileName, 'r')
newFileName = "backwardsWords_" + poemFileName
outputFile = open(newFileName, 'w')
# make an empty list to hold all the lines of a poem
allLines = []
#Get the title and author. The .strip() method takes out the
#newline at the end of the line.
poemTitle = inputFile.readline().strip()
poemAuthor = inputFile.readline().strip()
# skip blank line after the author name
inputFile.readline()
# now read each line of the poem and store the lines in a list
for nextLine in inputFile:
#nextLine = nextLine.strip()
allLines.append(remove_punctuation(nextLine))
# close the input (original) file
inputFile.close()
# now reverse the lines in the list
allLines.reverse()
# get the total number of lines in this poem
totalLines = len(allLines)
# print title and author
outputFile.write("Lines Backwards ")
outputFile.write('Title: ' + poemTitle + ' ')
outputFile.write('Author: ' + poemAuthor + ' ')
# write the lines in the list to the file, one at a time.
for line in allLines:
line = line.split()
line.reverse()
outputFile.write(str(totalLines) + " " + ' '.join(line).lower() + ' ')
totalLines = totalLines - 1
# end of for loop
print(" " + newFileName +" created. ")
outputFile.close()
return None
# Function: remove_punctuation
# Inputs: A string (e.g., line of a poem)
# Outputs: A string with all the punctuation symbols removed
def remove_punctuation( s ):
s_without_punct = ""
for letter in s:
if letter not in string.punctuation:
s_without_punct += letter
return s_without_punct
main()
Please pay close attention to the code indentation.
Sample Outputs:
linesBackwards_maryPoem.txt:
Lines Backwards
Title: Mary had a little lamb
Author: Sarah Josepha Hale
23 the teacher did reply.
22 "Why, Mary loves the lamb, you know."
21
20 the eager children cry.
19 "Why does the lamb love Mary so?"
18
17 till Mary did appear.
16 And waited patiently about,
15
14 but still it lingered near,
13 And so the teacher turned it out,
12
11 to see a lamb at school.
10 It made the children laugh and play,
9
8 which was against the rule.
7 He followed her to school one day
6
5 the lamb was sure to go.
4 And everywhere that Mary went,
3
2 whose fleece was white as snow.
1 Mary had a little lamb,
randomize_maryPoem.txt
Lines Backwards
Title: Mary had a little lamb
Author: Sarah Josepha Hale
to see a lamb at school.
the teacher did reply.
And waited patiently about,
which was against the rule.
whose fleece was white as snow.
It made the children laugh and play,
And everywhere that Mary went,
till Mary did appear.
"Why, Mary loves the lamb, you know."
And so the teacher turned it out,
He followed her to school one day
the lamb was sure to go.
Mary had a little lamb,
"Why does the lamb love Mary so?"
but still it lingered near,
the eager children cry.
backwardsWords_maryPoem.txt
Lines Backwards
Title: Mary had a little lamb
Author: Sarah Josepha Hale
23 reply did teacher the
22 know you lamb the loves mary why
21
20 cry children eager the
19 so mary love lamb the does why
18
17 appear did mary till
16 about patiently waited and
15
14 near lingered it still but
13 out it turned teacher the so and
12
11 school at lamb a see to
10 play and laugh children the made it
9
8 rule the against was which
7 day one school to her followed he
6
5 go to sure was lamb the
4 went mary that everywhere and
3
2 snow as white was fleece whose
1 lamb little a had mary
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.