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

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

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

问题

  1. def scrolling_text(string: str) -> list:
  2. new_list = []
  3. string = string.upper()
  4. for i in range(0, len(string)):
  5. string = string[i:] + string[:i]
  6. new_list.append(string)
  7. return new_list
  8. print(scrolling_text('robot')) # ["ROBOT", "OBOTR", "BOTRO", "OTROB", "TROBO"]
英文:
  1. def scrolling_text(string: str) -> list:
  2. new_list = []
  3. string = string.upper()
  4. for i in range(0, len(string)):
  5. string = string[i:] + string[:i]
  6. new_list.append(string)
  7. return new_list
  8. print(scrolling_text('robot')) # ["ROBOT", "OBOTR", "BOTRO", "OTROB", "TROBO"]

答案1

得分: 1

你想要做的是将字符串的第一个字母通过切片操作“移动”到字符串的末尾,然后将新得到的子字符串添加到列表中:

  1. def scrolling_text(string: str) -> list:
  2. new_list = []
  3. string = string.upper()
  4. for i in range(0, len(string)):
  5. if i != 0:
  6. string = string[1:] + string[:1]
  7. new_list.append(string)
  8. return new_list
  9. 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:

  1. def scrolling_text(string: str) -> list:
  2. new_list = []
  3. string = string.upper()
  4. for i in range(0, len(string)):
  5. if i != 0:
  6. string = string[1:] + string[:1]
  7. new_list.append(string)
  8. return new_list
  9. 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:

确定