我试图制作一个递归序列(必须这样做),我打印一个*后跟最后一个字符加上下一行的下一个字符,等等。所以如果"你好& #34;通过后,它会打印出来:
*
o
ol
oll
olle
olleH
我的问题是,如何让最后一个角色留下来,如何让星星先打印出来。我有它所以一切都反过来打印,但只有一个角色和明星打印最后。这就是我所拥有的:
def reverse(s):
if len(s) == 0:
return ('*')
else:
print(s[-1])
return reverse(s[0:-1])
谢谢!
答案 0 :(得分:2)
试试这个:
def reverse(s):
if len(s) == 0:
print(*)
else:
reverse(s[1:])
print(s[::-1])
当字符串没有长度时,它将打印*
,否则它将以1个字符递归调用并打印字符串反转。
作为reverse('two')
的追溯:
Call - 'two' Call - 'wo' Call - 'o' Call - '' Print - '*' Return Print - 'o' Return Print - 'ow' Return Print - 'owt' Return
请致电reverse('Hello')
答案 1 :(得分:0)
def reverse(s, length, iteration):
if iteration == 0:
return '*' + reverse(s, length, iteration+1)
elif iteration == length:
return s[::-1]
else:
return '%s\n' % s[length - iteration: length][::-1] + reverse(s, length, iteration+1)
input = 'Hello'
print(input, len(input), 0)
def reverse(s):
print('*')
length = len(s)
for i in range(1, length + 1):
yield s[length - i: length][::-1]
print('\n'.join(list(reverse('Hello'))))
答案 2 :(得分:0)
这将打印:
ggplot(data=edge_c_summary,aes(x = times,y=means))+
geom_errorbar(data=edge_c_summary,aes(ymin=means-sd,ymax=means+sd))+
geom_line(aes(y=means))+
geom_line(data = ridge_c_summary,aes(x=times,y=means),colour="red")+
geom_errorbar(data=ridge_c_summary,aes(ymin=means-sd,ymax=means+sd),colour="red")+
geom_line(data = valley_c_summary,aes(x=times,y=means),colour="blue")+
geom_errorbar(data=valley_c_summary,aes(ymin=means-sd,ymax=means+sd),colour="blue")