|
自己研究出来了
def repeat_letter(words, char_positions, repl_times):
if len(words) != len(char_positions):
print('Error:len(words) != len(char_positions)!')
return
if repl_times <= 0:
print('Error:repl_times <= 0!')
return
i = 0
while i < len(words):
if len(words[i]) > abs(char_positions[i]):
words[i] = words[i][0:char_positions[i]]+words[i][char_positions[i]]*repl_times+words[i][char_positions[i]:]
else:
words[i] = words[i]
i += 1
words = ['hello', 'exercise', 'with', 'words']
print("old words: %s" % words)
repl_times = 3
char_positions = [4, 2, 10, -2]
repeat_letter(words, char_positions, repl_times)
print("new words: %s" % words)
words = ['hello', 'exercise', 'with', 'words']
print("old words: %s" % words)
repl_times = 3
char_positions = [5, 8, 4, 5]
repeat_letter(words, char_positions, repl_times)
print("new words: %s" % words)
|
|