I am trying to write two procedures to replace matched strings in a string in python.
And I have to write two procedures.
def matched_case(old new):
.........
note: inputs are two strings, it returns a replacement converter.
def replacement(x,another_string):
..........
note:inputs are a converter from previous procedure, and a string. It returns the result of applying the converter to the input string.
for example:
a = matched_case('mm','m')
print replacement(a, 'mmmm')
it should return m
another example:
R = matched_case('hih','i')
print replacement(R, 'hhhhhhihhhhh')
it should return hi
I am not sure how can I use loop to do the whole thing. Thanks so much for anyone can give a hint.
解决方案def subrec(pattern, repl, string):
while pattern in string:
string = string.replace(pattern, repl)
return string
foo('mm', 'm', 'mmmm') return m
foo('hih', 'i', 'hhhhhhihhhhh') return hi