Use Python programing. First please download all 4 files listed below into the s
ID: 3802815 • 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
# import these modules to help me
import string
import random, fileinput
# 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 )
poemFileName2 ="linesBackwards_maryPoem.txt"
#(2)
print ("(2) Starting Randomize Lines Backwards ... ")
randomizeLines( poemFileName2 )
#(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"""
print(" Please check your folder")
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()
count = 1
# now read each line of the poem and store the lines in a list
for nextLine in inputFile:
#nextLine = nextLine.strip()
allLines.append(str(count)+'.'+nextLine) //To adding numbers before the lines.
count+=1
# 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(line)
# end of for loop
print(" " + newFileName +" created. ")
outputFile.close()
return None
# Function: randomizeLines
# Inputs: [You write this]
# Outputs: [You write this]
def randomizeLines( poemFileName2 ):
text = []
"""You need to write the docstring"""
newFileName = "randomizedlines_" + poemFileName2
outputFile = open(newFileName, 'w')
"""for line in fileinput.input(poemFileName2):
if random.randrange(fileinput.lineno())== " ":
outputFile.write(random.choice(line)) """
with open(poemFileName2) as f:
lines = f.readlines()
#random_int = random.randint(0,len(lines)-1)
outputFile.write(random.choice(lines))
# end of for loop
print (" Please check your folder and check folder ")
print(" " + newFileName +" created. ")
outputFile.close()
#inputFile.close()
# Function: readingWordsBackwards
# Inputs: [You write this]
# Outputs: [You write tis]
def readingWordsBackwards( poemFileName ):
outputFile = open("readingWordsBackwards.txt", "w")
lines=[]
with open(poemFileName) as f:
for line in f:
words = line.split()
line_rev = " ".join(reversed(words))
outputFile.write(line_rev)
outputFile.write(" ")
print (" Please check your folder and check folder readingWordsBackwards function ")
#inputFile.close()
# 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()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.