def pet(n,shape):
if n==1:
return shape
else:
return stack_frac(1/n,scale(1/n,shape),pet(n-1,shape))
缩放功能将形状大小更改为1/n
。但是,只需要第一个形状即可缩放到1/n
。其余n-1
模式为2/n, 3/n....n/n
。我的递归将所有大小更改为1/n, 1/(n-1), 1/(n-2)...
有没有办法将递归更改为2/n, 3/n....n/n
?
答案 0 :(得分:2)
你可以尝试:
def pet(n, shape, k=1):
if k == n:
return shape
else:
return stack_frac(k/n, scale(k/n, shape), pet(n, shape, k+1))