回答編集履歴
2
誤字修正
test
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
全体のコードを読めてい
|
5
|
+
全体のコードを読めていないので、直るかはわからないのですが
|
6
6
|
|
7
|
-
|
7
|
+
traverse_in_depth 関数を呼び出す際に `yield from` を使ってみると、どうでしょうか?
|
8
8
|
|
9
9
|
|
10
10
|
|
@@ -12,9 +12,31 @@
|
|
12
12
|
|
13
13
|
# 修正前
|
14
14
|
|
15
|
+
def traverse_in_depth(node, depth):
|
16
|
+
|
17
|
+
if node is not None and node['depth'] == depth:
|
18
|
+
|
19
|
+
yield node['center']
|
20
|
+
|
21
|
+
elif node is not None and node['depth'] < depth:
|
22
|
+
|
23
|
+
yield traverse_in_depth(node['leftNode'], depth)
|
24
|
+
|
25
|
+
yield traverse_in_depth(node['rightNode'], depth)
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
15
31
|
def bin_node_iterator(left, right):
|
16
32
|
|
17
|
-
|
33
|
+
root_node = create(0, left, right)
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
max_depth = get_max_depth(root_node)
|
38
|
+
|
39
|
+
|
18
40
|
|
19
41
|
for depth in range(1, max_depth + 1):
|
20
42
|
|
@@ -28,9 +50,31 @@
|
|
28
50
|
|
29
51
|
# 修正後
|
30
52
|
|
53
|
+
def traverse_in_depth(node, depth):
|
54
|
+
|
55
|
+
if node is not None and node['depth'] == depth:
|
56
|
+
|
57
|
+
yield node['center']
|
58
|
+
|
59
|
+
elif node is not None and node['depth'] < depth:
|
60
|
+
|
61
|
+
yield from traverse_in_depth(node['leftNode'], depth) # <--- yield from を使います。
|
62
|
+
|
63
|
+
yield from traverse_in_depth(node['rightNode'], depth) # <--- yield from を使います。
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
31
69
|
def bin_node_iterator(left, right):
|
32
70
|
|
33
|
-
|
71
|
+
root_node = create(0, left, right)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
max_depth = get_max_depth(root_node)
|
76
|
+
|
77
|
+
|
34
78
|
|
35
79
|
for depth in range(1, max_depth + 1):
|
36
80
|
|
@@ -86,7 +130,7 @@
|
|
86
130
|
|
87
131
|
|
88
132
|
|
89
|
-
[
|
133
|
+
[i for i in f()]
|
90
134
|
|
91
135
|
# [<generator object g1 at 0x100e9db48>, <generator object g2 at 0x100e9dba0>]
|
92
136
|
|
1
誤字修正
test
CHANGED
@@ -58,6 +58,84 @@
|
|
58
58
|
|
59
59
|
|
60
60
|
|
61
|
+
```python
|
62
|
+
|
63
|
+
# コピペで動きます。
|
64
|
+
|
65
|
+
def f():
|
66
|
+
|
67
|
+
yield g1()
|
68
|
+
|
69
|
+
yield g2()
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
def g1():
|
74
|
+
|
75
|
+
yield 0
|
76
|
+
|
77
|
+
yield 1
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
def g2():
|
82
|
+
|
83
|
+
yield 2
|
84
|
+
|
85
|
+
yield 3
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
[_ for _ in f()]
|
90
|
+
|
91
|
+
# [<generator object g1 at 0x100e9db48>, <generator object g2 at 0x100e9dba0>]
|
92
|
+
|
93
|
+
```
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
61
99
|
今回は「ジェネレータイテレータ」の **中身** が欲しいので、
|
62
100
|
|
63
101
|
`yield from` を使います。
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
```python
|
106
|
+
|
107
|
+
# コピペで動きます。
|
108
|
+
|
109
|
+
def f():
|
110
|
+
|
111
|
+
yield from g1()
|
112
|
+
|
113
|
+
yield from g2()
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
def g1():
|
118
|
+
|
119
|
+
yield 0
|
120
|
+
|
121
|
+
yield 1
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
def g2():
|
126
|
+
|
127
|
+
yield 2
|
128
|
+
|
129
|
+
yield 3
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
[i recursive.pyfor i in f()]
|
134
|
+
|
135
|
+
# [0, 1, 2, 3]
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
* [yield, ジェネレータってなに?](https://python.ms/type/for/generator/)
|