无法使用面向对象的prog生成fibonacci树。在python中。 [英] Unable to generate fibonacci tree with object oriented prog. In python.
本文介绍了无法使用面向对象的prog生成fibonacci树。在python中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I am a starter & want to integrate dfs code with Fibonacci series generating code. The Fibonacci code too runs as dfs, with calls made from left to right.
The integration is incomplete still.
I have two issues :
(i) Unable to update 'path' correctly in fib(), as the output is not correctly depicting that.
(ii) Stated in fib() function below, as comment.
-----
Have one more issue that is concerned with program's working:
(iii) On modifying line #16 to: stack = root = stack[1:]; get the same output as before.
import sys
count = 0
root_counter = 0
path=1
inf = -1
node_counter = 0
root =0
def get_depth_first_nodes(root):
nodes = []
stack = [root]
while stack:
cur_node = stack[0]
stack = stack[1:]
nodes.append(cur_node)
for child in cur_node.get_rev_children():
stack.insert(0, child)
return nodes
def node_counter_inc():
global node_counter
node_counter = node_counter + 1
class Node(object):
def __init__(self, id_,path):
self.id = node_counter_inc()
self.children = []
self.val = inf #On instantiation, val = -1, filled bottom up;
#except for leaf nodes
self.path = path
def add_child(self, node):
self.children.append(node)
def get_children(self):
return self.children
def get_rev_children(self):
children = self.children[:]
children.reverse()
return children
def fib(n, level, val, path):
global count, root_counter, root
print('count :', count, 'n:', n, 'dfs-path:', path)
count += 1
if n == 0 or n == 1:
path = path+1
return n
if root_counter == 0:
root = Node(n, path)
root_counter = 1
else:
#cur_node.add_child(Node(n, path)) -- discarded for next(new) line
root.add_child(Node(n, path))
tmp = fib(n-1, level + 1,inf, path) + fib(n-2, level + 1,inf,path+1)
#Issue 2: Need update node's val field with tmp.
#So, need suitable functions in Node() class for that.
print('tmp:', tmp, 'level', level)
return tmp
def test_depth_first_nodes():
fib(n,0,-1,1)
node_list = get_depth_first_nodes(root)
for node in node_list:
print(str(node))
if __name__ == "__main__":
n = int(input("Enter value of 'n': "))
test_depth_first_nodes()
< b>我尝试了什么:
除了上述尝试之外,还浪费地尝试在fib()中使用cur_node(而不是root)。但这并没有成功,因为它被视为整数。我对上述程序进行了以下更改:1。将其初始化为'0'作为g.v.; 2.在两个函数中将其声明为g.v.:fib()和get_depth_first_nodes(); 3.注释第53行而不是第52行。
What I have tried:
Apart from the above attempt, also wastefully tried to use cur_node (instead of root) in fib(). But that did not work out as it was taken as integer. I made following changes to the above program : 1. initializing it to '0' as a g.v.; 2. declaring it as g.v.in both functions: fib() and get_depth_first_nodes(); 3. commenting line #53 instead of line #52.
推荐答案
转到 Python教程 - Python 3.7.2文档 [ ^ ],其中包含有关如何创建斐波纳契程序的出色解释。
Go to The Python Tutorial — Python 3.7.2 documentation[^], which contains excellent explanations of how to create a Fibonacci program.
这篇关于无法使用面向对象的prog生成fibonacci树。在python中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文