Return value fail - python-3.x

I am working on a doubly linked list and I am having trouble getting returning the value I remove an item from the middle of the list. I am not really sure why and would appreciate the a second pair of eyes look this through.
If I append three numbers and then remove the number at index 1, it should return the value at index 1.
class Linked_List:
class __Node:
def __init__(self, val):
self.val = val
self.next = None
self.prev = None
return
def __init__(self):
self.__header = self.__Node(None)
self.__trailer = self.__Node(None)
self.__header.next = self.__trailer
self.__trailer.prev = self.__header
self.__size = 0
def __len__(self):
return self.__size
def remove_element_at(self, index):
if index > self.__size or index < 0 or index == self.__size:
raise IndexError
cur = self.__header.next
element_to_return = cur
if index == 0:
self.__header.next = self.__header.next.next
else:
for i in range(0, index):
cur = cur.next
element_to_return = cur.val
cur.next = cur.next.next
self.__size -= 1
return str(element_to_return.val)

I added the addValue() and a debug() method (tip: read How to debug small programs (#1) ).
I modified the start/end node to have a value for clarity. I reduced/restructured (rewrote) your the remove method - it had some unneeded overhead and pointer errors. For double linked lists you have to take care of both the folloing and preceeding element and change all the pointers correctly to keep the integrity of the chain.
class Linked_List:
class __Node:
def __init__(self, val):
self.val = val
self.next = None
self.prev = None
return
def __init__(self):
self.__header = self.__Node('Start') # start node
self.__trailer = self.__Node('End') # end node
self.__header.next = self.__trailer
self.__trailer.prev = self.__header
self.__size = 0
def debug(self):
cur = self.__header
print("prev.val cur.val next.val")
i = 0
while cur:
print(" " * i, f'{cur.prev.val if cur.prev else None} {cur.val} {cur.next.val if cur.next else None}')
i+=1
cur = cur.next
def addValue(self, val):
"""Adds the str of val at the end."""
n = self.__Node(str(val))
n.next = self.__trailer
n.prev = self.__trailer.prev
self.__trailer.prev.next = n
self.__trailer.prev = n
self.__size += 1
def remove_element_at(self, index):
if not (0 <= index < self.__size ): # simplified
# better message on error
raise IndexError (f'Index {index} out of bounds [0,{self.__size-1}]')
cur = self.__header.next # simplified, element_to_return not needed
# there is no need for special cases for 0 or size - this will handle all
for i in range(0, index):
cur = cur.next # go ever deeper, if we are at 0 or self.__size not entering
cur.prev.next = cur.next # repointer prev's next to our next
cur.next.prev = cur.prev # repointer next's prev to our prev
self.__size -= 1 # decrease size
return str(cur.val) # return our value
k = Linked_List()
for n in range(5):
k.addValue(n)
k.debug()
removed = k.remove_element_at(4)
k.debug()
print(f'removed: {removed}')
Output:
prev.val cur.val next.val
None Start 0
Start 0 1
0 1 2
1 2 3
2 3 4
3 4 End
4 End None
None Start 0
Start 0 1
0 1 2
1 2 3
2 3 End
3 End None
removed: 4
Edit:
My add method is self-build. Make sure youres gets the pointers correctly - use the debug method to assure that cur.prev.next points to cur and cur.next.prev is cur as well.

Related

"list object has no attribute " error while passing parameter to a function

I am trying to solve a problem to add two numbers using link list. I have used the below code which is complied correctly. But if I want to test it by passing parameters I am getting an error " list object has no attribute 'val'
#Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode)--> ListNode:
dummy = ListNode()
cur = dummy
carry = 0
while l1 or l2 or carry:
v1 = l1.val if l1 else 0
v2 = l2.val if l2 else 0
# new digit
val = v1 + v2 + carry
carry = val // 10
val = val % 10
cur.next = ListNode(val)
# update ptrs
cur = cur.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return dummy.next
a =Solution()
a.addTwoNumbers([1,2,3],[4,5,6])
edit
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def __init__(self):
self.head=None
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode()
cur = dummy
carry = 0
while l1 or l2 or carry:
v1 = l1.val if l1 else 0
v2 = l2.val if l2 else 0
# new digit
val = v1 + v2 + carry
carry = val // 10
val = val % 10
cur.next = ListNode(val)
# update ptrs
cur = cur.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return dummy.next
def insert_at_end(self,val):
if self.head is None:
self.head=ListNode(val,None)
return
itr=self.head
while itr.next:
itr=itr.next
itr.next=ListNode(val,None)
def print(self):
if self.head is None:
print("Linked List is empty")
return
itr=self.head
llstr=""
while itr:
llstr +=str(itr.val)
itr=itr.next
print(llstr)
ll=Solution()
ll.insert_at_end([1,2,3])
ll.insert_at_end([4,5,6])
ll.print()

AVL Tree, delete mininum node (Python 3)

I want to implement a huffman coding using AVL tree. I perform some insertion, then I delete some node and everything is fine, but suddenly after another deletion the tree delete every nodes on the right side from root. It happens when the tree looks like this (the right side may be a bit different, but the height is correct):
after deleting the minim node (M) the tree has only nodes (S, R).
I think that it is because of my mistake in rotation function. Could you help me understand where is the mistake.
import random
from collections import Counter
class HuffmanCoding:
def __init__(self, data=None, space=False):
# Transform data to the form of dictionary
if data is None:
data = input("Enter string for coding: ")
self.data = Counter(data)
elif isinstance(data, str):
self.data = Counter(data)
elif isinstance(data, dict):
self.data = data
else:
raise TypeError("Type of data is incorrect!")
# Check whether to delete the space or not
if not space:
self.data.pop(' ', None)
self.my_tree = AVL_Tree()
self.root = None
for k, v in self.data.items():
self.root = self.my_tree.insert(self.root, TreeNode(k, v))
def sth(self):
n1 = self.my_tree.delete_min(self.root)
n2 = self.my_tree.delete_min(self.root)
self._create_new_internal_node(n1, n2)
def _create_new_internal_node(self, n1, n2):
new_node = HuffmanNode(n1, n2)
self.my_tree.insert(self.root, new_node)
class HuffmanNode:
def __init__(self, n1, n2):
self.character = n1.character + n2.character
self.freq = n1.freq + n2.freq
self.left = None
self.right = None
self.height = 1
class TreeNode:
def __init__(self, character, freq):
self.character = character
self.freq = freq
self.left = None
self.right = None
self.height = 1
class AVL_Tree:
def insert(self, root, n):
character = n.character
freq = n.freq
# Normal insert, like in BST
if not root:
return TreeNode(character, freq)
elif freq < root.freq:
root.left = self.insert(root.left, n)
else:
root.right = self.insert(root.right, n)
# Update the height of the ancestor node
root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
# Get the balance
balance = self.get_balance(root)
# Left left
if balance > 1 and freq < root.left.freq:
return self.right_rotate(root)
# Right right
if balance < -1 and freq > root.right.freq:
return self.left_rotate(root)
# Left right
if balance > 1 and freq > root.left.freq:
root.left = self.left_rotate(root.left)
return self.right_rotate(root)
# Right left
if balance < -1 and freq < root.right.freq:
root.right = self.right_rotate(root.right)
return self.left_rotate(root)
return root
def delete_min(self, root):
if not root:
return root
# Get parent of most left node
min_parent = self.get_parent_of_min_value_node(root)
min_node = min_parent.left
# Check if most left node has a right son
if min_parent.left.right is not None:
min_parent.left = min_parent.left.right
else:
min_parent.left = None
# Update the height
root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
balance = self.get_balance(root)
# Left left
if balance > 1 and min_parent.freq < root.left.freq:
self.right_rotate(root)
# Right right
if balance < -1 and min_parent.freq > root.right.freq:
self.left_rotate(root)
# Left right
if balance > 1 and min_parent.freq > root.left.freq:
root.left = self.left_rotate(root.left)
self.right_rotate(root)
# Right left
if balance < -1 and min_parent.freq < root.right.freq:
root.right = self.right_rotate(root.right)
self.left_rotate(root)
return min_node
def get_height(self, root):
if not root:
return 0
return root.height
def get_balance(self, root):
if not root:
return 0
return self.get_height(root.left) - self.get_height(root.right)
def right_rotate(self, z):
y = z.left
T3 = y.right
y.right = z
z.left = T3
z.height = 1 + max(self.get_height(z.left), self.get_height(z.right))
y.height = 1 + max(self.get_height(y.left), self.get_height(y.right))
return y
def left_rotate(self, z):
y = z.right
T2 = y.left
# Perform rotation
y.left = z
z.right = T2
# Update heights
z.height = 1 + max(self.get_height(z.left), self.get_height(z.right))
y.height = 1 + max(self.get_height(y.left), self.get_height(y.right))
# Return the new root
return y
def pre_order(self, root):
if not root:
return
print("C: {0}, F: {1}".format(root.character, root.freq))
self.pre_order(root.left)
self.pre_order(root.right)
def in_order(self, root):
if not root:
return
self.in_order(root.left)
print("C: {0}, F: {1}".format(root.character, root.freq))
self.in_order(root.right)
def post_order(self, root):
if not root:
return
self.post_order(root.left)
self.post_order(root.right)
print("C: {0}, F: {1}".format(root.character, root.freq))
def get_min_value_node(self, root):
if root is None or root.left is None:
return root
return self.get_min_value_node(root.left)
def get_parent_of_min_value_node(self, root):
if root.left is None or root.left.left is None:
return root
return self.get_parent_of_min_value_node(root.left)
if __name__ == "__main__":
huffman_coding = HuffmanCoding("Blogoslawieni milosierni")
huffman_coding.my_tree.in_order(huffman_coding.root)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
huffman_coding.my_tree.in_order(huffman_coding.root)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
huffman_coding.my_tree.in_order(huffman_coding.root)
x = huffman_coding.root

Python - 'int' object is not callable

I'm writing a singly linked list that counts the number of words that get imported from a test file.
I initialized count inside of my class.
I defined the print function, that prints out each of the nodes, after they are sorted.
I defined a count class to iterate through the list and count up all the occurences of a word fed to it.
I want to pass in every node into count to count how many times it appears in my text file. When I try to do that I end up with 'int' object is not callable. Here is my code
class Linked_List:
def __init__(self):
self.head = None
self.count = 1
def print(self):
p = self.head
head = Linked_List_node(p.data)
while p is not None:
print(p.data, '-', self.count(p.data)) # This is where the error appears
p = p.next
def count(self, x):
# loop thru list for all x, if find x add 1 to count. Assign final count to that word.
with open('cleaned_test.txt', 'r') as f:
for line in f:
for word in line.split():
if word == x:
self.count += 1
def insert(self, x):
""""""
p = self.head
q = None
done = False
while not done:
if self.head == x:
done = True
elif p == None:
head = Linked_List_node(x)
q.next = head
done = True
elif x == p.data:
#head = Linked_List_node(x)
#head.counter += 1
done = True
elif x < p.data:
if self.head == p:
head = Linked_List_node(x)
head.next = p
self.head = head
done = True
else:
head = Linked_List_node(x)
head.next = p
q.next = head
done = True
q = p
if p is not None:
p = p.next
class Linked_List_node:
def __init__(self, value):
self.data = value
self.next = None
Relevant part from your code is:
class Linked_List:
def __init__(self):
# ...
self.count = 1
def count(self, x):
# ...
The self.count = 1 assignment overwrites the value of self.count for every Linked_List object that's created, so it refers to 1 instead of the method you defined on the class. Rename either the variable or the function.

trying to look for duplicates in an ordered list

So my programs function is to add the provided numbers to an ordered list, then search for the duplicates. I have a function that finds the duplicates of a number I asked and I know is a duplicate. Im trying to get the duplic function to read through the list and append the duplicates to a new list called seen.
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setNext(self,newnext):
self.next = newnext
class OrderedList:
def __init__(self):
self.head = None
def add(self, item):
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.getData() > item:
stop = True
else:
previous = current
current = current.getNext()
temp = Node(item)
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext (current)
previous.setNext(temp)
def prntlist(self):
prnt = self.head
while prnt != None:
print(prnt.data, end=" ")
prnt = prnt.next
print()
def duplic (self):
currnt = self.head
seen = set()
uniq = []
for i in range(int(currnt.data)):
if i not in seen:
uniq.append(i)
seen.add(i)
print (seen)
def count(self, item):# function to count the value
count = 0
ptr = self.head
while ptr != None:
if (ptr.data == item):
count += 1
ptr = ptr.next
return count
mylist = OrderedList()
mylist.add(23)
mylist.add(23)
mylist.add(10)
mylist.add(14)
mylist.add(5)
mylist.add(31)
mylist.add(35)
mylist.add(37)
mylist.add(26)
mylist.add(23)
mylist.add(29)
mylist.add(18)
mylist.add(2)
mylist.add(25)
mylist.prntlist()
print('Count of 23 in list: ', mylist.count(23))
print('Duplicates in list: ', mylist.duplic())
I would like it to print:
mylist
Count of 23 in list: 3
Duplicates in list: {23,23}
You may need to be more clear in the question..
From what i understand, you could write the function this way:
def duplic (self):
currnt = self.head
seen = set()
for i in currnt.data:
if currnt.data.count(i)>1:
seen.add(i)
print (seen)
The uniq seems to be useless. And, a set object cannot have two same elements, so you can't have {23,23}. (you can write this, but it will result in {23} in the end)

Heap Structure with Key Function in initalizer

I'm basically trying to implement this Heap Structure in Python and I've editing the portions under def heap-iffy and def add but I'm not sure how to how to use the current initialize with a key function. This function will be used to extract a value from each element added to the heap; these values, in turn, will be used to order the elements. f no key function is provided, the default max-heap behavior should be used — the "lambda x:x" default value for the initialize method does just that.
class Heap:
def __init__(self, key=lambda x:x):
self.data = []
self.key = key
#staticmethod
def _parent(idx):
return (idx-1)//2
#staticmethod
def _left(idx):
return idx*2+1
#staticmethod
def _right(idx):
return idx*2+2
def _heapify(self, idx=0):
enter code here
while True:
l = Heap._left(idx)
r = Heap._right(idx)
maxidx = idx
if l < len(self) and self.data[l] > self.data[idx]:
maxidx = l
if r < len(self) and self.data[r] > self.data[maxidx]:
maxidx = r
if maxidx != idx:
self.data[idx], self.data[maxidx] = self.data[maxidx], self.data[idx]
idx = maxidx
else:
break
def add(self, x):
enter code here
self.data.append(x)
i = len(self.data) - 1
p = Heap._parent(i)
while i > 0 and self.data[p] < self.data[i]:
self.data[p], self.data[i] = self.data[i], self.data[p]
i = p
p = Heap._parent(i)
def peek(self):
return self.data[0]
def pop(self):
ret = self.data[0]
self.data[0] = self.data[len(self.data)-1]
del self.data[len(self.data)-1]
self._heapify()
return ret
def __bool__(self):
return len(self.data) > 0
def __len__(self):
return len(self.data)
def __repr__(self):
return repr(self.data)

Resources