Midterm Exam 2 Review Fall 2005: Encoding and Decoding
what is meant by part b? do you want us to write a function that makes the change and then changes it back or what; can you just be a little more clear on what's asked for part b? thanks
| I think what is being asked is to write a function that will take a string that has - , = , and : and turn them back into the correct letters. So if i gave the funtion the string "-e=:" i would want the function to print "rest" -Albert d'Heurle |
can you give some hints to part c. the problem is how do you get the program to replace at the same time instead of doing one, and then the other?
| Hint: .replace isn't a good option in this case. -Blake O'Hare |
def replaceLetters(string):
source=string
source=source.replace("r","-").replace("s","=").replace("t",":")
print source
this is a short function, but is it right?
this is for part b, and it is just the inverse of a
def replaceLetters(string):
source=string
source=source.replace("-","r").replace("=","s").replace(":","t")
print source
| This and the one above it seem correct. The winner is you. -Blake O'Hare |
for c, do we use .split?
| Now that you mention it, I think there is a way to do it with .split. That would be interesting. Go for it! -Blake O'Hare |
can we use find for part c... the above didn't help me much because i still can't figure out anything that will change both of them at the same time rather than changing one, and then switching it back
def replaceLetters(string):
string.replace("-","r")
string.replace("=","s")
string.replace(":","t")
print string
why won't this work for part a?
| The .replace method RETURNS the resultant string. It doesn't automatically store the changes. -Blake O'Hare |
will this work?
def replaceLetter(letter):
letter = "Mr. Mark Guzdial requests the pleasure of you company at..."
print letter.replace('r','-').replace('s','=').replace('t',':')
Link to this Page