I'm trying to create my own list type with a new class called TZList,
I tried to create it with a recursive __init__ func but it won't work,
here's the code:
class TZList:
def __init__(self, *args):
numargs = len(args)
self.value = None
self.next = None
if numargs == 0:
self.value = None
self.next = None
elif numargs == 1:
self.value = args[0]
self.next = None
else:
self.value = args[0]
numargs -= 1
args = args[1:]
self.next = TZList(args)
when i try to get the data like this:
t = TZList(1,2,3)
print(t.value)
print(t.next.value)
print(t.next.next.value)
i get a weird print:
Traceback (most recent call last):
1
(2, 3)
File "C:\****\a3.py", line 79, in <module>
print(t.next.next.value)
AttributeError: 'NoneType' object has no attribute 'value'
and i have no idea why, hope you could help me.
The reason is the way you are repassing args into TZList. You are passing it as a tuple. Instead of self.next = TZList(args) do self.next = TZList(*args)
Related
I can't figure out the type of error that I'm getting.
Traceback (most recent call last):
File "C:/Users/Eli/.PyCharmCE2019.2/config/scratches/BinarySearchTree.py", line 43, in
bt.printTreePreOrder(bt.Root())
TypeError: 'NoneType' object is not callable
class BinaryTreeNode:
def __init__(self,key, data = None):
self.key = key
self.value = data
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.Root = None
self.size = 0
def __len__(self):
return self.size
def root (self):
return self.Root
def insert(self,key,data):
new_node = BinaryTreeNode(key,data)
x = self.Root
y = None
while x != None:
y = x
if key < y.key:
x = x.left
else:
x = x.right
if y == None:
self. key < y.key
y.left = new_node
else:
y.right = new_node
self.size +=1
def printTreePreOrder(self,node):
print(node.key)
if node.left:
self.printTreePreOrder(node.left)
if node.right:
self.printTreePreOrder(node.right)
if __name__ == "__main__":
bt = BinarySearchTree()
bt.insert(12,'bill')
bt.insert(6,'Tom')
bt.insert(14,'jill')
bt.insert(3,'guy')
bt.printTreePreOrder(bt.Root())
You're attempting to call Root as if it were a function, but it's only a member variable of class BinaryTreeNode, therefore is not callable.
Furthermore, it is returning as NoneType because you initialize it to None and it is never assigned to any other value.
The way you have it implemented, you should do bt.printTreePreOrder(bt.Root)
The code is written is meant to calculate the number of nodes in a tree. However, I am getting a simple type error that keeps referring back to my TreeNode class. I have checked for spellings and typos, but I still can't find the error. Please help
Here is the error
Traceback (most recent call last):
File "C:/Users/Eli/.PyCharmCE2019.2/config/scratches/Tree.py", line 30, in <module>
t1.addChild(t1.Root(),6)
TypeError: 'TreeNode' object is not callable
class TreeNode():
def __init__(self, data= None, parent = None):
self.data = data
self.children = []
self.parent = parent
class Tree():
def __init__(self):
self.Root = None
self.size = 0
def __len__(self):
return self.size
def root (self):
return self.Root
def children (self, node):
return node.children()
def addChild (self,parent,data):
t = TreeNode (data,parent)
if parent == None:
self.Root = t
else:
parent.children.append(t)
self.size +=1
def printTreePreOrder(self,node):
print(node.data)
for n in node.children:
self.printTreePreOrder(n)
if __name__ == "__main__":
t1 = Tree()
t1.addChild(None,5)
t1.addChild(t1.Root(),6)
cl = t1.children(t1.Root())
for n in cl:
t1.addChild(n,3)
t1.addChild(n,9)
t1.printTreePr
t1.Root() should probably be t1.Root because your root is a TreeNode object instead of a function to find the root.
You will likely hit a similar wall when accessing node.children()
I'm trying to write a next method to be able to iterate over a linked list object with a for loop and no matter how I change my code I keep getting "'NoneType' object is not callable". It's an assignment and therefore I can't change anything when it comes to the other constructors and methods. Only thing I can play around with is iter and next. This is my code:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self, fdata):
firstNode = Node(fdata)
self.first = firstNode
self.last = firstNode
self.n = 1
def append(self, ndata):
newNode = Node(ndata)
self.last.next = newNode
self.last = newNode
self.next = None
self.n += 1
def __iter__(self):
return self
def next(self):
if self.__current.next == None:
raise StopIteration
else:
self.__current = self.__current.next
return self.__current.next.ndata
a = LinkedList(0); a.append(1); a.append(2)
for n in a:
print n
Looks like you advance reading too far. Try:
self.__current = self.__current.next
return self.__current.data
Because self.__current.next at then moment will point to next-next element. And in case of list dnid - to nowhere.
I'm kinda new to python and I can't get past this error:
Traceback (most recent call last):
File "***", line 63, in <module>
bst = Node()
TypeError: __init__() missing 1 required positional argument: 'val'
Basically, the program is a BST which would allow you to insert, search and look for the minimum item by only going left.
Here's the code (sorry, it's hungarian)
class Node:
def __init__(self, val):
self.ertek = val
self.balgyerek = None
self.jobbgyerek = None
self.gyoker = None
def beszur(self, pri):
if self.gyoker:
return self.gyoker.beszur(pri)
else:
self.gyoker = Node(pri)
return True
if self.ertek == pri:
return False
elif self.ertek > pri:
if self.balgyerek:
return self.balgyerek.beszur(pri)
else:
self.balgyerek = Node(pri)
return True
else:
if self.jobbgyerek:
return self.jobbgyerek.beszur(pri)
else:
self.jobbgyerek = Node(pri)
return True
def keres(self, pri):
if self.gyoker:
return self.gyoker.keres(pri)
else:
return False
if(self.ertek == pri):
return True
elif self.ertek > pri:
if self.balgyerek:
return self.balgyerek.keres(pri)
else:
return False
else:
if self.jobbgyerek:
return self.jobbgyerek.keres(pri)
else:
return False
def minimumertek(self):
jelenlegi = self
while(jelenlegi.balgyerek is not None):
jelenlegi = jelenlegi.balgyerek
return self.ertek
bst = Node()
The __init__ method is run as soon as an object of a class is instantiated. Your __init__ method has two positional arguments: self which refers to the object instance and is passed automatically, and val which is assigned to self.ertek. However, you didn't pass the value of val. Hence, the error. Try passing the value of val at the class instantiation. e.g bst = Node('ertek value')
as an assignment during software engineering studies i have to implement my own list type in python, i did that using a linked list.
From my past experience whenever i use a line like
self = self.next
I must first save my head node, than in the end assign head back to self.
In this linked list implementation i totally forgot about it but everything seems to work fine.
Anyway i need help not with 'how to' do something, i just want to understand how does it work without keeping and assigning the head node back, hope it's fine, thanks!
class MYList:
def __init__(self,*args):
numargs = len(args)
self.value = None
self.next = None
if(numargs == 0):
pass
elif(numargs == 1):
self.value = args[0]
elif(numargs == 2):
self.value = args[0]
self.next = MYList(args[1])
else:
self.value = args[0]
self.next = MYList(*args[1:])
def get_value(self,k):
i=0
while(i<k):
self = self.next
i+=1
return self.value
def set_value(self,k,v):
i=0
while(i<k):
self = self.next
i+=1
self.value = v
def len(self):
i=0
while(self.next!=None):
self = self.next
i+=1
return i+1
def append(self, *args):
if(len(args) == 1):
v=args[0]
if(self.get_value(0) == None):
self.set_value(0, v)
else:
i = 0
while(self.next != None):
self = self.next
i += 1
self.next = MYList(v)
else:
v=args[0]
if(self.get_value(0) == None):
self.set_value(0, v)
else:
i = 0
while(self.next != None):
self = self.next
i += 1
self.next = MYList(v)
self.append(*args[1:])
def print(self):
if(self.value == None):
print("List is empty!")
else:
amount = self.len()
for _ in range(amount):
if(_ < amount-1):
print(self.value,end=',')
else:
print(self.value)
self=self.next
def pop(self,*args):
if(len(args) == 0):
count = self.len()
head=self
for _ in range(count-1):
self = self.next
v = self.value
self = head
for _ in range(count-2):
self = self.next
self.next = None
return v
elif(len(args)>1):
raise Exception("Enter a key to pop only!")
else:
k = args[0]
count = self.len() - 1
if(k>count):
raise Exception("Key too high!")
elif(k==0):
v=self.value
self.value = self.next.value
self.next = self.next.next
return v
else:
head=self
for _ in range(-1,k-1):
self = self.next
v = self.value
temp_next = self.next
self = head
for _ in range(-1,k-2):
self = self.next
self.next = temp_next
return v