Class method for merge sort recursively - python

I am trying to create a method for merge sort recursively. I am not so familiar with objects and i am trying to get this right. I know the algorithm works as a stand alone function, but when trying to implement as a class method, i get error.
The problem lies in this two lines of code:
left_list = left_list.merge_sort()
right_list = right_list.merge_sort()
class Lists(object):
def __init__(self):
self.capacity = 8
self.arr = [None] * capacity
self.size = 0
def merge_sort(self):
if self.size <= 1:
return self.arr
middle = self.size // 2
left_list = self.arr[:middle]
right_list = self.arr[middle:]
left_list = left_list.merge_sort()
right_list = right_list.merge_sort()
return list(self.merge(left_list, right_list))
def merge(self, left_half, right_half):
res = []
while len(left_half) != 0 and len(right_half) != 0:
if left_half[0] < right_half[0]:
res.append(left_half[0])
left_half.remove(left_half[0])
else:
res.append(right_half[0])
right_half.remove(right_half[0])
if len(left_half) == 0:
res = res + right_half
else:
res = res + left_half
return res
And of course is another function append, that appends each time at the self.arr and increases the size by 1 each time an element is appended.

Related

Python "object() takes no parameters" error

I'm having this problem when I clearly use init function to declare the parameters. Link my code there:
class Node:
def _init_(self,pieza,turno,listaAsignados,parent,costeCamino,coste):#Initialize a Node
self.pieza = pieza
self.turno = turno
self.listaAsignados = listaAsignados
self.parent = parent
self.costeCamino = costeCamino
self.costeNodo = costeNodo
def calculateCost(C, i, j, listaPadre,M): #Calculate Cost of the next element
initCost = 0
cost = 0
listaDisponibles = [1]*M
for i in range(i,M-1):
minNum = 9999999999
minIndex = -1
for j in range(j,M-1):
if(not listaPadre[j] and listaDisponibles[j] and C[i][j]< minNum ):
minIndex = j
minNumber = costMatrix[i][j]
cost = cost+ minNumber
return cost
import heapq
def branch_bound(C):
"""
-C = Matrix of costs
"""
items=[]
priorityQueue = []
heapq.heapify(priorityQueue)
listaAsignados = [0]*M#Asigned list for the matrix
raiz = Node(-1,-1,listaAsignados,None,0,0)
heapq.heappush(listaAsignados,[raiz.cost,raiz])
while (priorityQueue):
examinateNode = heapq.heappop(priorityQueue)
examinateNode = examinateNode[1]
i = examinateNode.pieza+1
if (i == M):
return examinateNode.cost
for j in range(0,M-1):
if(examinateNode.listaAsignados[j] == 0):
costeCamino = examinateNode.pathCost+ C[i][j]
costeHijo = costeCamino+ calculateCost(C, i, j, examinateNode.listaAsignados,M)
nodoHijo = Node(i,j,examinateNode.listaAsignados,examinateNode,costeCamino,costeHijo)
heapq.heappush(listaAsignados,[nodoHijo.cost,nodoHijo])
return items
If someone can explain me why this error is going on I will appreciate it. I don't know why if I have a constructor the error is going on:
<ipython-input-10-8d5dfd71f776> in branch_bound(C)
11 heapq.heapify(priorityQueue)
12 listaAsignados = [0]*M
13 raiz = Node(-1,-1,listaAsignados,None,0,0)<-------
14 heapq.heappush(listaAsignados,[raiz.cost,raiz])
15
TypeError: object() takes no parameters
You need to write __init__ instead of _init_.

Python - high disk usage in SumTree

I've encountered some weird behaviour of my python program. Basically when I tried to create adn fill a SumTree of length larger than 1000, my disk usage increases a lot to ~300MB/s then the programme died.
I'm pretty sure there's no file r/w involved in this process, and the problem is with the add function. The code is shown below.
import numpy as np
class SumTree():
trans_idx = 0
def __init__(self, capacity):
self.num_samples = 0
self.capacity = capacity
self.tree = np.zeros(2 * capacity - 1)
self.transitions = np.empty(self.capacity, dtype=object)
def add(self, p, experience):
tree_idx = self.trans_idx + self.capacity - 1
self.transitions[self.trans_idx] = experience
self.transitions.append(experience)
self.update(tree_idx, p)
self.trans_idx += 1
if self.trans_idx >= self.capacity:
self.trans_idx = 0
self.num_samples = min(self.num_samples + 1, self.capacity)
def update(self, tree_idx, p):
diff = p - self.tree[tree_idx]
self.tree[tree_idx] = p
while tree_idx != 0:
tree_idx = (tree_idx - 1) // 2
self.tree[tree_idx] += diff
def get_leaf(self, value):
parent_idx = 0
while True:
childleft_idx = 2 * parent_idx + 1
childright_idx = childleft_idx + 1
if childleft_idx >= len(self.tree):
leaf_idx = parent_idx
break
else:
if value <= self.tree[childleft_idx]:
parent_idx = childleft_idx
else:
value -= self.tree[childleft_idx]
parent_idx = childright_idx
data_idx = leaf_idx - self.capacity + 1
return leaf_idx, self.tree[leaf_idx], self.transitions[data_idx]
#property
def total_p(self):
return self.tree[0] # the root
#property
def volume(self):
return self.num_samples # number of transistions stored
Here's an example where this SumTree object will be used:
def add(self, experience)
max_p = np.max(self.tree.tree[-self.tree.capacity:])
if max_p == 0:
max_p = 1.0
exp = self.Experience(*experience)
self.tree.add(max_p, exp)
where Experience is a named tuple and self.tree is a Sumtree instance, when I removed the last line the high disk usage disappears.
Can anyone help me with this?
I finally sort this out because each experience is a tuple of namedtuple and I'm creating another namedtuple Experience from it. Fixed by changing experience to a tuple of numpy arrays.

Issue with python __eq__ method in checking if 2 lists are equal

I have a python program in which I have a class called Vector and an empty list inside of that class which is being populated runtime.
Here is the init:
def __init__(self,n):
self.vector = [];
self.n = n;
for x in range(n):
self.vector.append(False);
And here is the eq:
def __eq__(self, other):
t = True
for x in range(self.n):
if self.vector[x] != other.vector[x]:
t = False;
return t
however, when I try to check if 2 objects of this type are equal, I always get true, even though I changed values inside of vector in Vector class.
Here is the code where I do the above:
vectors = []
n = tmp.size();
k = calculateCombinationCount(n,int(n/2))
for i in range(k):
for j in range(0,n-1):
if (tmp.vector[j] != tmp.vector[j+1]):
t = True
for x in vectors:
if x == tmp:
t = False;
if t:
vectors.append(tmp)
tmp.printVector();
tmp.swap(j,j+1);
I would appreciate any help that you can provide. Thank you :)
EDIT:
def swap(self,i,j):
tmp = self.vector[i]
self.vector[i] = self.vector[j]
self.vector[j] = tmp
def calculateCombinationCount(n,r):
k = factorial(n)/(factorial(int(r))*factorial(int(n-r)))
return int(k)
Right so I've updated your code to be much more pythonic (I can tell you come from another language, Java?).
from math import factorial
class Vector:
def __init__(self, size):
self.size = size
self.vector = [False] * size
def __eq__(self, other):
"""
Same if self.size == other.size
"""
assert self.size == other.size, (self.size, other.size)
return self.vector == other.vector
def print_vector(self):
print(self.vector)
def swap(self, i, j):
"""
More efficient and pythonic
"""
self.vector[i], self.vector[j] = self.vector[j], self.vector[i]
def calculate_combination_count(n, r):
"""
This is slow, I'd replace it with scipy.special.comb
https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.comb.html#scipy.special.comb
"""
return factorial(n) // (factorial(r) * factorial(n-r))
tmp = Vector(10)
vectors = []
n = tmp.size
k = calculate_combination_count(n, n // 2)
for i in range(k):
for j in range(0, n-1):
if tmp.vector[j] != tmp.vector[j + 1]:
if not any(vec == tmp for vec in vectors): # much more efficient
vectors.append(tmp)
tmp.print_vector()
tmp.swap(j, j + 1)
else: # Just to prove why it doesn't work
print('tmp.vector is all False: {}'.format(not all(tmp.vector)))
This prints out tmp.vector is all False: True repeatedly. I think this is your problem.
If you

How can I create a running average of the last N items in a time series?

My basic idea was to create a linked list, and as each new value comes in, add 1/N times the new value and subtract 1/N times the first value, then move the pointer to first along by one and free the memory that had been associated with first.
This won't ultimately be implemented in Python but just to get the process clear in my head, I tried to write it in Python, but my implementation is flawed. Do I need a doubly linked list for this? Is there an alternative approach (not linked-list based) that would be better?
Here's my attempt so far:
class Link:
def __init__(self,val):
self.next = None
self.value = val
class LinkedList:
def __init__(self,maxlength):
self.current_link = None
self.maxlength = maxlength
self.sum = 0.
self.average = None
self.length = 0
self._first_link = None
def add_link(self,val):
new_link = Link(val)
new_link.next = self.current_link
self.current_link = new_link
if self._first_link is None:
self._first_link = self.current_link
self.sum += val
if self.length < self.maxlength:
self.length += 1
else:
self.sum -= self._first_link.value
self._first_link = self._first_link.next # this line is flawed
self.average = self.sum/self.length
def get_first(self):
return self._first_link.value
# Main
ll = LinkedList(5)
for ii in xrange(10):
ll.add_link(ii)
print ii,ll.get_first(),ll.average
The problem is that _first_link gets set to a value that doesn’t have a next. That is, _first_link gets set to the first item that's added, but its next is None, so I don't see how to move it along by 1 as I want to. This is what makes me wonder if a doubly linked list is needed.
I'd appreciate any advice.
I think the simplest implementation is to use a circular linked list (a.k.a. a ring):
class Link(object):
def __init__(self, value=0.0):
self.next = None
self.value = value
class LinkedRing(object):
def __init__(self, length):
self.sum = 0.0
self.length = length
self.current = Link()
# Initialize all the nodes:
last = self.current
for i in xrange(length-1): # one link is already created
last.next = Link()
last = last.next
last.next = self.current # close the ring
def add_val(self, val):
self.sum -= current.value
self.sum += val
self.current.value = val
self.current = self.current.next
def average(self):
return self.sum / self.length
# Test example:
rolling_sum = LinkedRing(5)
while True:
x = float(raw_input())
rolling_sum.add_val(x)
print(">> Average: %f" % rolling_sum.average())
You can implement this using collections.deque and the numerically stable math for maintaining running averages:
import collections
class AveragingBuffer(object):
def __init__(self, maxlen):
assert( maxlen>1)
self.q=collections.deque(maxlen=maxlen)
self.xbar=0.0
def append(self, x):
if len(self.q)==self.q.maxlen:
# remove first item, update running average
d=self.q.popleft()
self.xbar=self.xbar+(self.xbar-d)/float(len(self.q))
# append new item, update running average
self.q.append(x)
self.xbar=self.xbar+(x-self.xbar)/float(len(self.q))
if __name__=="__main__":
import scipy
ab=AveragingBuffer(10)
for i in xrange(32):
ab.append(scipy.rand())
print ab.xbar, scipy.average(ab.q), len(ab.q)
Okay, I thought of a solution that works in O[1] time. I'm still curious if anyone has a linked-list-based solution, but this solution avoids the LL entirely:
class Recent:
def __init__(self,maxlength):
self.maxlength = maxlength
self.length = 0
self.values = [0 for ii in xrange(maxlength)]
self.index = 0
self.total = 0.
self.average = 0.
def add_val(self,val):
last = self.values[self.index%self.maxlength]
self.values[self.index%self.maxlength] = val
self.total += val
self.total -= last
if self.length < self.maxlength:
self.length += 1
self.average = self.total / self.length
self.index += 1
def print_vals(self):
print ""
for ii in xrange(self.length):
print ii,self.values[ii%self.maxlength]
print "average:",self.average
# Example to show it works
rr = Recent(5)
for ii in xrange(3):
rr.add_val(ii)
rr.print_vals()
for ii in xrange(13):
rr.add_val(ii)
rr.print_vals()

error in converting linked list to a single number in python

here this is the node definition
class Node(object):
def __init__(self,value=None):
self.value = value
self.next = None
this is the conversion for the code a number to linked list
def number_to_list(number):
head,tail = None,None
p = True
for x in str(number):
if x=='-':
p = False
continue
else:
if p:
node = Node(int(x))
else:
node = Node(int("-"+x))
if head:
tail.next = node
else:
head = node
tail = node
return head
pass
this is code for conversion of linked list to number
def list_to_number(head):
neg = False
num = ''
for number in head:
val = str(number)
if (val.find('-')!= -1):
neg = True
num=num+val.replace('-','')
if (neg==False):
return int(num)
else:
return -1*int(num)
pass
here it is the test cases
def test_number_to_list():
import listutils
head = number_to_list(120)
assert [1,2,0] == listutils.from_linked_list(head)
assert 120 == list_to_number(head)
head = number_to_list(0)
assert [0] == listutils.from_linked_list(head)
assert 0 == list_to_number(head)
head = number_to_list(-120)
assert [-1, -2, 0] == listutils.from_linked_list(head)
assert -120 == list_to_number(head)
here from_linked_list means
# avoids infinite loops
def from_linked_list(head):
result = []
counter = 0
while head and counter < 100: # tests don't use more than 100 nodes, so bail if you loop 100 times.
result.append(head.value)
head = head.next
counter += 1
return result
at last in this the problem is while converting the linked list to single number it is encountering an error i.e.,node object is not iterable
please help me out of this to write the code
def list_to_number(head):
neg = False
num = ''
for number in head:
val = str(number)
TypeError: 'Node' object is not iterable
here this is the traceback
The
for number in head:
is not a correct way to iterate of the list.
You need to start from head and then follow the chain of next references.
Note that if __iter__ is defined on Node like this:
class Node(object):
def __init__(self,value=None):
self.value = value
self.next = None
def __iter__(self):
that = self
while that is not None:
yield that.value
that = that.next
Then:
for number in head:
Would actually work.

Categories

Resources