Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

This page removed for FERPA compliance
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 2 Review Fall 2004: Encoding and Decoding

Post answers, comments, and questions here!
(Back to Fall2004 Midterm 2 Review)



def replace(test):
a=test.replace('e','#')
b=a.replace('i','!')
c=b.replace('a','@')
d=c.replace('o','%')
e=d.replace('u','^')
f=e.replace('r','-')
g=f.replace('s','=')
h=g.replace('t',':')
print h

test of course would be your input and everything under "def" would be indented 2 spaces

Read FAQ for how to format code so that we can read it. The last line is wrong. You can also do this all in one line. Mark Guzdial


well regardless of if it's the easiest way to do it or not, it worked when I ran it and did what it was supposed to

b)
def replace2(test):
newtest=test.replace('#','e').replace('!','i').replace('@','a').replace('%','o').replace('^','u').replace('-','r').replace('=','s').replace(':','t')
  print newtest


No, it didn't do what it's supposed to do. Last line is wrong on both of these. Mark Guzdial


why do they both work on my computer if the last line is wrong? am i missing something?

Return and print are not the same thing Student1594

do you REMOVEDe return instead of print becaREMOVEDe it not only lets you view it but also saves it that way? i understand that, but the question doesn't really specify

The question specifies "Write a second function that takes an encoded string as input, and returns the original unencoded string." Your function returns None, and the namespace containing your variable newtest is destroyed when the function ceases execution. The difference between returning and printing at the end of a function is somewhat masked when REMOVEDing the console becaREMOVEDe return values are automatically printed out. Student2243

i'm sitting in the review session right now and the TAs did the first question exactly like the first one on this page, including the last line where it says "print" so why is mine wrong?!

Then your TA made the same mistake you did. If the questions says "return the result" then there better be a return statement in your code, or it's wrong, and you will get points off. If the questions says "print the result" then you would want a print statement. JREMOVEDt always follow the instructions, and write what is asked. Student1594

Question 1 says nothing about printing or returning, so i think either would be acceptable. Question 2 does say return, so return ould be the only correct way.



Link to this Page