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 PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 2 Review Spring 2006: Encoding and Decoding


For (a)
def encoding(search1,search2,search3,replace1,replace2,replace3):
  letter = "Mr. Mark Guzdial requests the pleasure of your company at..."
  print letter.replace(search1,replace1).replace(search2,replace2).replace(search2,replace3)

This would ALMOST work, you are calling the wrong variable somewhere. HOWEVER, read the question again. It needs to accept the string to manipulate as input. -poof #10

except add some spaces under def!!!!
This is not what the question is asking for, its asking for a code that takes in any string, and replaces the set consonants displayed... Keep working on this :)... and for future reference, post the code with <code></code> tags around it to preserve formatting Liz Helms

does anyone know the answer to question (b)????
If you post something, we will gladly tell you if it is incorrect. Try working through it. -poof #10

should the answer for part 'a' somehow be kept and automatically put into the program for part 'b', or do you just want to see us reverse the order of replacement (i.e., string.replace('-','r').replace('s','=').replace('t',':'))?
They are independent functions. -Blake O'Hare

part c:
def replace(string):
string.lower()
print string.replace('a','-').replace('b','=').replace('-','b').replace('=','a')
It's cheap, but it works.
And what happens when you have hyphens and equal signs in the string? -Blake O'Hare

def encodeDecode(string):
  print string.replace("r","-").replace("s","=").replace("t",":")

How about this one?

Is this correct for part b?

def decode(string):
  print string.replace("-","r").replace("=","s").replace(":","t")

Yes and yes. -Blake O'Hare

Is this correct for part c?

def aToBAndBToA(string):
  string = string.replace("a","####")
  string = string.replace("b","a")
  string = string.replace("####","b")
  print string

And what happens when there's a #### in the string you're modifying? -Blake O'Hare

i think i am making this more complicated than it actually is but this is my code:
  
def replace(string)
  letter = string
  new = letter.replace('r','-').replace('s','=').replace('t',':')
  return new

when i try to run replace(Mr. Mark Guzdial requests the pleasure of your company at) in the command area i get that its not legal but its not the code thats not legal its the command i just tried to run – i am so confused


The colon at the end of line 1. -Blake O'Hare
i checked my code i actually do have a colon @ the end of line one i just left it off here – any other idea??

Am I anywhere close for C on this:
def encodeDecode(string):
  a='a'
  b='b'
  for i in a:
    loc = string.find(i)
    while loc <> -1:
     string=string.replace(i,'b')
  return string
  for i in b:
    loc=string.find(i)
    while loc <> -1:
      string=string.replace(i,'a')
  return string
  print string


The returns will both do exactly that: return control to the function that called encodeDecode. So the code after them (actually the code after the first return) will never get executed. For that reason, a return should generally be the very last command in a function or the last command in an if block. Colin Potts



Link to this Page