Scrolling text. Rearrange the character. Characters in a string from index zero to last. I want to get the following result

huangapple go评论126阅读模式
英文:

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"]

huangapple
  • 本文由 发表于 2023年3月10日 01:28:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定