解题思路:根据题目要求一步一步来。
注意事项:python中index(value)只能得到第一个重复元素的位置,需要翻转一下。
参考代码:
s = list(input().strip())
command = input().strip().split()
for i in range(0, len(command), 2):
if command[i] == 'D':
char_to_delete = command[i+1]
if char_to_delete in s:
s.remove(char_to_delete)
else:
print('被改的字符不存在')
elif command[i] == 'I':
char_to_insert_before = command[i+1]
char_to_insert = command[i+2]
if char_to_insert_before in s:
index = len(s) - 1 - s[::-1].index(char_to_insert_before)
s.insert(index, char_to_insert)
else:
print('被改的字符不存在')
elif command[i] == 'R':
char_to_replace = command[i+1]
new_char = command[i+2]
s = [new_char if char == char_to_replace else char for char in s]
print(''.join(s))
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题7.2 (Java代码)浏览:681 |
矩形面积交 (Java代码)浏览:1213 |
【绝对值排序】 (C++代码)浏览:670 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:624 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:879 |
Hello, world! (C++代码)浏览:1744 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:531 |
Hello, world! (C语言代码)浏览:714 |
C二级辅导-等差数列 (C语言代码)浏览:694 |
GC的苦恼 (C语言代码)浏览:618 |