質問編集履歴

4

ソースコード

2016/10/22 14:52

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  python 3.4.3
28
28
 
29
- ```python:tree_formula.py
29
+ ```python
30
30
 
31
31
  # coding: utf-8
32
32
 

3

ファイル名追加

2016/10/22 14:52

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  python 3.4.3
28
28
 
29
- ```
29
+ ```python:tree_formula.py
30
30
 
31
31
  # coding: utf-8
32
32
 

2

ソースコードの入力を改めました

2016/10/14 11:49

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -26,74 +26,74 @@
26
26
 
27
27
  python 3.4.3
28
28
 
29
+ ```
30
+
29
31
  # coding: utf-8
30
32
 
31
33
  # Here your code !
32
34
 
33
35
 
34
36
 
35
- class tnode :
37
+ class tnode :
36
38
 
37
- ---def init(self):
39
+ def __init__(self):
38
40
 
39
- ---self.left = None
41
+ self.left = None
40
42
 
41
- ---self.ope = ""
43
+ self.ope = ""
42
44
 
43
- ---self.right = None
45
+ self.right = None
44
46
 
45
47
 
46
48
 
47
- def gentree(p, w):
49
+ def gentree(p, w):
48
50
 
49
- ---p = tnode()
51
+ p = tnode()
50
52
 
51
- ---w_lst = list(w)
53
+ w_lst = list(w)
52
54
 
53
- ---p.ope = w_lst.pop()
55
+ p.ope = w_lst.pop()
54
56
 
55
- ---if p.ope == "-" or p.ope == "+" or p.ope == "*" or p.ope == "/" :
57
+ if p.ope == "-" or p.ope == "+" or p.ope == "*" or p.ope == "/" :
56
58
 
57
- ------w = "".join(w_lst)
59
+ w = "".join(w_lst)
58
60
 
59
- ------p.right = gentree(p.right, w)
61
+ p.right = gentree(p.right, w)
60
62
 
61
- ------p.left = gentree(p.left, w)
63
+ p.left = gentree(p.left, w)
62
64
 
63
- ---return p
65
+ return p
64
66
 
67
+
65
68
 
69
+ def prefix(p):
66
70
 
67
- def prefix(p):
71
+ if p :
68
72
 
69
- ---if p :
73
+ yield p.ope
70
74
 
71
- ------yield p.ope
75
+ yield from prefix(p.left)
72
76
 
73
- ------yield from prefix(p.left)
74
-
75
- ------yield from prefix(p.right)
77
+ yield from prefix(p.right)
76
78
 
77
79
 
78
80
 
79
81
  # main
80
82
 
81
- f = input()
83
+ f = input()
82
84
 
83
85
  print(f)
84
86
 
85
87
 
86
88
 
87
- root = None
89
+ root = None
88
90
 
89
91
  root = gentree(root, f)
90
92
 
91
93
 
92
94
 
93
- for x in prefix(root):
95
+ for x in prefix(root):
94
96
 
95
- print(x, end="")
97
+ print(x, end="")
96
-
97
-
98
98
 
99
99
  ```

1

ソースコードのインデント追加

2016/10/14 11:15

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- (python3)逆ポーランド記法の式から、式の木(2分木)を作るプログラム(再帰)
1
+ 逆ポーランド記法の式から、式の木(2分木)を作るプログラム(再帰)
test CHANGED
@@ -32,67 +32,67 @@
32
32
 
33
33
 
34
34
 
35
- class tnode :
35
+ class tnode :
36
36
 
37
- def __init__(self):
37
+ ---def init(self):
38
38
 
39
- self.left = None
39
+ ---self.left = None
40
40
 
41
- self.ope = ""
41
+ ---self.ope = ""
42
42
 
43
- self.right = None
43
+ ---self.right = None
44
44
 
45
45
 
46
46
 
47
- def gentree(p, w):
47
+ def gentree(p, w):
48
48
 
49
- p = tnode()
49
+ ---p = tnode()
50
50
 
51
- w_lst = list(w)
51
+ ---w_lst = list(w)
52
52
 
53
- p.ope = w_lst.pop()
53
+ ---p.ope = w_lst.pop()
54
54
 
55
- if p.ope == "-" or p.ope == "+" or p.ope == "*" or p.ope == "/" :
55
+ ---if p.ope == "-" or p.ope == "+" or p.ope == "*" or p.ope == "/" :
56
56
 
57
- w = "".join(w_lst)
57
+ ------w = "".join(w_lst)
58
58
 
59
- p.right = gentree(p.right, w)
59
+ ------p.right = gentree(p.right, w)
60
60
 
61
- p.left = gentree(p.left, w)
61
+ ------p.left = gentree(p.left, w)
62
62
 
63
- return p
63
+ ---return p
64
64
 
65
-
66
65
 
67
- def prefix(p):
68
66
 
69
- if p :
67
+ def prefix(p):
70
68
 
71
- yield p.ope
69
+ ---if p :
72
70
 
73
- yield from prefix(p.left)
71
+ ------yield p.ope
74
72
 
73
+ ------yield from prefix(p.left)
74
+
75
- yield from prefix(p.right)
75
+ ------yield from prefix(p.right)
76
76
 
77
77
 
78
78
 
79
79
  # main
80
80
 
81
- f = input()
81
+ f = input()
82
82
 
83
83
  print(f)
84
84
 
85
85
 
86
86
 
87
- root = None
87
+ root = None
88
88
 
89
89
  root = gentree(root, f)
90
90
 
91
91
 
92
92
 
93
- for x in prefix(root):
93
+ for x in prefix(root):
94
94
 
95
- print(x, end="")
95
+ print(x, end="")
96
96
 
97
97
 
98
98