英文:
Scrolling text. Rearrange the character. Characters in a string from index zero to last. I want to get the following result
问题
def scrolling_text(string: str) -> list:
new_list = []
string = string.upper()
for i in range(0, len(string)):
string = string[i:] + string[:i]
new_list.append(string)
return new_list
print(scrolling_text('robot')) # ["ROBOT", "OBOTR", "BOTRO", "OTROB", "TROBO"]
英文:
def scrolling_text(string: str) -> list:
new_list = []
string = string.upper()
for i in range(0, len(string)):
string = string[i:] + string[:i]
new_list.append(string)
return new_list
print(scrolling_text('robot')) # ["ROBOT", "OBOTR", "BOTRO", "OTROB", "TROBO"]
答案1
得分: 1
你想要做的是将字符串的第一个字母通过切片操作“移动”到字符串的末尾,然后将新得到的子字符串添加到列表中:
def scrolling_text(string: str) -> list:
new_list = []
string = string.upper()
for i in range(0, len(string)):
if i != 0:
string = string[1:] + string[:1]
new_list.append(string)
return new_list
print(scrolling_text('robot')) # ["ROBOT", "OBOTR", "BOTRO", "OTROB", "TROBO"]
英文:
What you want to do to get the list is shift
the first letter of the string and append
it to the back of the remaining sub-string through slicing
:
def scrolling_text(string: str) -> list:
new_list = []
string = string.upper()
for i in range(0, len(string)):
if i != 0:
string = string[1:] + string[:1]
new_list.append(string)
return new_list
print(scrolling_text('robot')) # ["ROBOT", "OBOTR", "BOTRO", "OTROB", "TROBO"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论