 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Midterm Exam 2 Review Spring 2006: Encoding and Decoding
For (a)
def encoding(search1,search2,search3,replace1,replace2,replace3):
letter = "Mr. Mark Guzdial REMOVEDs 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. -Student2042 |
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 Student3381 |
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. -Student2042 |
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',':'))?
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? -Student1680 |
def encodeDecode(string):
print string.replace("r","-").replace("s","=").replace("t",":")
REMOVEDw about this one?
Is this correct for part b?
def decode(string):
print string.replace("-","r").replace("=","s").replace(":","t")
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? -Student1680 |
i think i am making this REMOVED 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 REMOVEDs 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
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