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

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 2 Review Spring 2004: Encoding and Decoding

Comments, answers, and questions go here:

(Link back to Sp2004 Midterm 2 Review)


Okay guys, this is pretty close. Maybe someone who is smart can finish it out. But if you figue it out please post the answer for me.

def change():
  string = "How are you this fine day?"
  newstring = string.replace("e","#")
  print newstring

HAHAHA that is awesome...

This works for the e's but I can't quite figure out how to do more than one letter. It is not newstring = string.replace("e","#"),("a","!"). I tried - it no work!

Sorry it is my first time. The above is almost a cruel joke. Here we go again.
def change():
string = "How are you this fine day?"
newstring = string.replace("e","#")
print newstring

There are two spaces for every line after def change():

Don't put spaces between the method calls–you need dots. It's dot notation. Look at the example again. Mark Guzdial

Looks like I figured it out on my own. Here you go:

def create()
string = "How are you this fine day?"
newstring = string.replace("e","#").replace("a","!")
print newstring

Of course you have to type all the different "replaces" out. To answer the second part all you do is reverse the items in the (). For instance ("e","#") becomes ("#","e").

One more post on this stupid problem. It actually wants you the function to have an input so instead of the code above just use this:

def create(string)
newstring = string.replace("e","#").replace("a","!")...
print newstring

This way you can use any string
>>> create("How are you today?")
How !r# you tod!y?



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


this isnt working. it is giving me a –

An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer.

on the second line....the newLetter= part

Amelia Cipolla

nevermind...i figured it out. thanks
Amelia Cipolla


def replace(string):

letter= string
print letter.replace("e","#").replace("i","!").replace("a","@").replace("o","%").replace("u","^").replace("r","-").replace("s","=").replace("t",":")

def decode(istring):
letter=istring
print letter.replace("#","e").replace("!","i").replace("@","a").replace("%","o").replace("^","u").replace("-","r").replace("=","s").replace(":","t")

Margaret McIntosh

is print string at the end "a must"?

Actually, I think it's a mistake – look at (b) again. Mark Guzdial

What I meant is that HAVING print is a mistake. Mark Guzdial


So, is there a mistake in the review question? As in, should we not have print letter.replace.... etc.... Should it just be letter.replace...etc... And it would just replace it and it would show up?poof #2


No, the question is fine. There's a flaw in Margaret's solutions. Mark Guzdial



def create():
string = "How are you this fine day?"
newstring = string.replace("e","#").replace("i","!").replace
("a", "@").replace("o", "%").replace("u","^").replace
("r","-).replace("s","=").replace("t",":")
print newstring

Amy Howard

What's wrong with margaret's answer? It seems to work fine

It works fine, but print is not the same as "return the string..." Mark Guzdial


Can we do it like Amy did it? Could we just go ahead and put "print..." or would have to be "return string."

So should the code end with print newstring AND return string? Kristin Noell

No, just return. That's what the problem asks for, explicitly. Mark Guzdial




So if you put "return newstring" on the bottom of Amy Howard's solution in place of print newstring, would that recieve full credit?

So I am confused, how would you fix Margaret's solution?

def create():
  string = "How are you this fine day?"
  string.replace("e","#").replace("i","!").replace
              ("a", "@").replace("o", "%").replace("u","^").replace
              ("r","-).replace("s","=").replace("t",":")
  print newstring

And that's how we fix Margaret's solution. Karin Bowman

Tahts nearly the same things as Margaret's solution. Prof Guzdial notes above that it should be return newstring, not print newstring. This is how i have it.
def create(string):
  newstring = string.replace("e","#").replace("i","!").replace
              ("a", "@").replace("o", "%").replace("u","^").replace
              ("r","-").replace("s","=").replace("t",":")
  return newstring

Unencoded....

def decode(coded):
  newstring = coded.replace("#","e").replace("!","i").replace
              ("@", "a").replace("%", "o").replace("^","u").replace
              ("-","r").replace("=","s").replace(":","t")
  return newstring

Thomas Sobeck

Thomas' looks pretty good, but I think they need to work in one function; his seems to do it in two functions.

How this?:

def create(string):
  newstring = string.replace("e","#").replace("i","!").replace
              ("a", "@").replace("o", "%").replace("u","^").replace
              ("r","-").replace("s","=").replace("t",":")
  return newstring

  decoded = newstring.replace("#","e").replace("!","i").replace
              ("@", "a").replace("%", "o").replace("^","u").replace
              ("-","r").replace("=","s").replace(":","t")
  return decoded


Jonathan Laing

Part B says "Write a second function..." so they should be separate. FYI.
Thomas Sobeck



Link to this Page