此代码旨在“分解”给定的字符串
def string_splosions(s):
"""erp9yoiruyfoduifgyoweruyfgouiweryg"""
new = ''
for i in s:
new += s[0:int(s.index(i))+1]
return new
由于某些原因,此代码可以为大多数单词返回正确的“explosion”,但是有重复字母的单词不能正确打印。 举例说明。你知道吗
正确的输出是:
Code --> CCoCodCode
abc --> aababc
pie --> ppipie
incorrect outputs when s is
Hello --> HHeHelHelHello (should be HHeHelHellHello)
(注意:在不正确的输出中,从第二次到最后一次的重复中应该还有1个l。)
您应该转录代码,而不是张贴图片:
问题是索引(i)返回该字符的第一个实例的索引,对于“Hello”中的两个l都是2。解决方法是直接使用索引,这也更简单:
甚至:
相关问题 更多 >
编程相关推荐