如何扭转彼此嵌套的循环?
存在一个字符串s =“ abcd”,
如何通过字符串迭代其索引产生以下输出:
3
32
321
3210
我编写了以下代码:
for i in reversed(range(len(s))):
for j in range(i, -1, -1):
print(j, end='')
print()
它给了我输出:
3210
210
10
0
如何实现所需的输出?
There exists a string s = "abcd"
How do I iterate through the string such that it produces the following output for its index:
3
32
321
3210
I have written the following code:
for i in reversed(range(len(s))):
for j in range(i, -1, -1):
print(j, end='')
print()
It gives me the output:
3210
210
10
0
How do I achieve the desired output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑解决逆问题:
该解决方案似乎很容易,而产生结果的唯一更改是从
len(s)-1 -1
中减去。Think about solving the inverse problem:
That solution seems pretty easy, and the only change to produce your result is subtracting from
len(s)-1
.