我收到的任务:
运行sequence_del函数的示例:
>>> sequence_del("ppyyyyythhhhhooonnnnn")
'python'
>>> sequence_del("SSSSsssshhhh")
'Ssh'
>>> sequence_del("Heeyyy yyouuuu!!!")
'Hey you!'
我写的代码:
def sequence_del(my_str): # the function deletes duplicated characters.
l = []
for ch in my_str:
if ch not in l:
l.append(ch)
print("".join(l))
由于某种原因,我想不出一种方法来覆盖任务中的第三个示例。 希望有帮助!
答案 0 :(得分:1)
尝试
def sequence_del(my_str): # the function deletes duplicated characters.
l = []
last=''
for ch in my_str:
if last!=ch:
l.append(ch)
last = ch
print("".join(l))