回答編集履歴
3
追記
answer
CHANGED
@@ -25,6 +25,7 @@
|
|
25
25
|
```
|
26
26
|
|
27
27
|
最終的にリストにするなら、ジェネレータとあまり性能差はないかと思います。
|
28
|
+
**(追記:append呼び出しのオーバーヘッドに留意する必要はあります。)**
|
28
29
|
再帰が深くなりすぎるとスタックから溢れますが...
|
29
30
|
```Python
|
30
31
|
>>> recursive_func_repeat(10000, [1])
|
2
追記
answer
CHANGED
@@ -35,4 +35,6 @@
|
|
35
35
|
File "<stdin>", line 5, in recursive_func_repeat
|
36
36
|
[Previous line repeated 995 more times]
|
37
37
|
RecursionError: maximum recursion depth exceeded
|
38
|
-
```
|
38
|
+
```
|
39
|
+
|
40
|
+
可読性に大きな差がないのであれば、再帰を積極的に採用する理由はないです。
|
1
追記
answer
CHANGED
@@ -8,4 +8,31 @@
|
|
8
8
|
...
|
9
9
|
>>> list(func_repeat(11))
|
10
10
|
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
|
11
|
+
```
|
12
|
+
|
13
|
+
---
|
14
|
+
そういえば、そもそもご提示のコードは再帰関数じゃないですね。
|
15
|
+
再帰を使って書くなら、こんな感じです。
|
16
|
+
```Python
|
17
|
+
>>> def recursive_func_repeat(I, ret):
|
18
|
+
... if not I:
|
19
|
+
... return ret
|
20
|
+
... ret.append(2 * ret[-1])
|
21
|
+
... return recursive_func_repeat(I-1, ret)
|
22
|
+
...
|
23
|
+
>>> recursive_func_repeat(10, [1])
|
24
|
+
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
|
25
|
+
```
|
26
|
+
|
27
|
+
最終的にリストにするなら、ジェネレータとあまり性能差はないかと思います。
|
28
|
+
再帰が深くなりすぎるとスタックから溢れますが...
|
29
|
+
```Python
|
30
|
+
>>> recursive_func_repeat(10000, [1])
|
31
|
+
Traceback (most recent call last):
|
32
|
+
File "<stdin>", line 1, in <module>
|
33
|
+
File "<stdin>", line 5, in recursive_func_repeat
|
34
|
+
File "<stdin>", line 5, in recursive_func_repeat
|
35
|
+
File "<stdin>", line 5, in recursive_func_repeat
|
36
|
+
[Previous line repeated 995 more times]
|
37
|
+
RecursionError: maximum recursion depth exceeded
|
11
38
|
```
|