Stack in python with O(1) time complexity - python-3.x

I'm trying to write a code in python that imitates a stack, it has 3 properties:
push(x): append x to the end of the list
pop: pops rightmost value and return it
increase(k): increases the top k elements of list by 1, meaning if the list is [1,2,3] with a k value of 2; the updated list is [1,3,4].
I was able to do this easily, but i'm required to do it with a O(1) time complexity, here is my code:
from collections import deque
class Stack:
def __init__(self):
self.s = []
self.new = []
def push(self, x):
self.s.append(x)
self.new.append(0)
def pop(self):
return self.new.pop() + self.s.pop()
def increase(self, k):
b = self.new[-k:]
a = [1] * k
self.new = self.new[0:-k]
self.new += map(sum, zip(a,b))
if __name__ == "__main__":
s = Stack()
s.push(1)
s.push(2)
s.push(3)
s.increase(2)
print(s.pop()) # 4
s.push(1)
s.increase(2)
print(s.pop()) # 2
print(s.pop()) # 4
print(s.pop()) # 1
The list itself does not have to change, but these are the desired answers when calling the pop() function. Meaning only the illusion of the list being manipulated is enough.
Edit:
I found the fastest way to do this, here is the code:
class Stack:
def __init__(self):
self.s = []
self.new = [0]
def push(self, x):
self.s.append(x)
self.new.append(0)
def pop(self):
self.new[-2] += self.new[-1]
return self.new.pop() + self.s.pop()
def increase(self, k):
self.new[-1] += 1
self.new[-1-k] -= 1

Related

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.

Kruskal algorithm in python

import heapq
from collections import defaultdict
a = list(map(int, input().split()))
nodes = a[0]
disjoint_set = [-1]*(nodes+1)
rank_set = [0]*(nodes+1)
edges = a[1]
heap = []
def get_parent(u):
if disjoint_set[u] == -1:
return u
return get_parent(disjoint_set[u])
def make_union(x, y):
x_parent = get_parent(x)
y_parent = get_parent(y)
if rank_set[x_parent] == rank_set[y_parent]:
disjoint_set[x_parent] = y_parent
rank_set[x_parent] +=1
elif rank_set[x_parent] > rank_set[y_parent]:
disjoint_set[x_parent] = y_parent
else:
disjoint_set[y_parent] = x_parent
def not_cycle(*item):
x_parent = get_parent(item[1])
y_parent = get_parent(item[2])
if x_parent == y_parent:
return False;
make_union(x_parent, y_parent)
return True
while(edges!=0):
edge = list(map(int, input().split()))
heapq.heappush(heap, [edge[2], edge[0], edge[1]])
edges-=1
cnt = 0
total = 0
while(cnt!=nodes-1):
item = heapq.heappop(heap)
if(not_cycle(*item) is True):
total+= item[0]
cnt+=1
print(total)
I implemented the kruskal algorthm in python. I am getting RecursionError:maximum recursion depth exceeded in comparison error. make_union and get_parent are method of disjoint set algorithm. I am getting the error in get_parent method. How to solve this?
In not_cycle you are passing the parents to make_union but then in make_union you are trying to get the parents again. After the first change the parents will no longer be -1 and you will recurse "forever"[1]
[1] "forever" in this case is until the maximum depth of your stack.
-
aa,bb=list(map(int,input().split()))
c=[] for i in range(bb):
z=list(map(int,input().split()))
c.append(z) c.sort(key=lambda x: x[2])
a=[]
b=[]
for i in c:
a.append((i[0]-1,i[1]-1))
b.append(i[2])
arr=[]
size=[]
for i in range(len(b)):
arr.append(i)
size.append(1)
def root(i):
while arr[i]!=i:
i=arr[i]
return i
def unions(arr,size,p,q):
root_a=root(p)
root_b=root(q)
if size[root_a]>size[root_b]:
arr[root_b]=arr[root_a]
size[root_a]+=size[root_b]
else:
arr[root_a]=arr[root_b]
size[root_b]+=size[root_a]
def kruskals(b,a,aa):
te=[]
i=0
while (len(te))<aa-1:
(p,q)=a[i]
if root(p)!=root(q):
te.append(b[i])
unions(arr,size,p,q)
i+=1
return sum(te)
print(kruskals(b,a,aa))

how to get the updated size of dynamic array in python

How should I edit my code?
import ctypes
import sys
class DynamicArray(object):
def __init__(self):
self.n = 0 #counter
self.capacity = 1
self.A = self.make_array(self.capacity)
def __len__(self):
'''Returns the number of elements'''
b = sys.getsizeof(self.A)
print(b)
return self.n
def __getitem__(self,k):
'''Return elements in array with specified Index'''
if not 0<=k<self.n:
return IndexError('Index out of bounds')
return self.A[k]
def append(self,ele):
'''Add element in the array'''
if self.n==self.capacity:
self._resize(2*self.capacity)
self.A[self.n]=ele
self.n+=1
def _resize(self,new_cap):
B = self.make_array(new_cap)
for k in range(self.n):
B[k]=self.A[k]
self.A = B
self.capacity = new_cap
def make_array(self,new_cap):
'''Make a raw array using ctype module'''
return (new_cap*ctypes.py_object)()
arr = DynamicArray()
arr.append(1)
len(arr)
Output:
80
1
arr.append(1)
len(arr)
Output:
80
2
see https://docs.python.org/3/library/sys.html#sys.getsizeof
from doc:
Return the size of an object in bytes. The object can be any type of
object. All built-in objects will return correct results, but this
does not have to hold true for third-party extensions as it is
implementation specific.
Only the memory consumption directly attributed to the object is
accounted for, not the memory consumption of objects it refers to.
I hope it is clear.

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)

union of two default dictionaries

am trying to create a union of two default dictionaries. Here is the code for the method in my class:
def __add__(self,right):
mergedbag = copy.copy(self.bag_value)
for item in right:
if item not in mergedbag:mergedbag[item] = 0
mergedbag[item] += right[item]
return mergedbag
I create two default dictionaries:
b = Bag(['d','a','b','d','c','b','d'])
c = Bag(['d','a','b','d','c','b','d'])
the result of
print(b+c)
should be the total count of elements after the union..
Bag(a[a],b[4],c[3],d[6])
This is the error I keep getting:
Traceback (most recent call last):
File "D:\workspace33\courselib\driver.py", line 229, in driver
Command[print(b+c)]: exec(old,local,globl)
File "<string>", line 1, in <module>
File "D:\workspace33\Project2\src\bag.py", line 58, in __add__
mergedbag[item] += right[item]
TypeError: 'Bag' object is not subscriptable
Here is the new code:
class Bag:
def __init__(self, items = []):
self.bag_value = defaultdict(int)
for item in items:
self.bag_value[item] += 1
def __repr__(self):
bag_list = []
for item, count in self.bag_value.items():
bag_list.extend(list(item*count))
return 'Bag(' + str(bag_list) + ')'
def __str__(self):
return 'Bag(' + ','.join(str(item) + '[' + str(count) + ']' for item, count in self.bag_value.items()) + ')'
def __len__(self):
bag_len = 0
for value in self.bag_value:
bag_len += self.bag_value[value]
return bag_len
def unique(self):
return len(self.bag_value)
def __contains__(self, item):
return item in self.bag_value
def count(self, item):
return(self.bag_items.count(item))
def add(self, new):
self.bag_value[new] += 1
def __add__(self,right):
mergedbag = copy.copy(self.bag_value)
for item in right:
if item not in mergedbag:mergedbag[item] = 0
mergedbag[item] += right[item]
return mergedbag
def remove(self, item):
if item in self.bag_items:
del(item)
else:
raise ValueError(type_as_str(item) + ' not in bag.')
def __eq__(self, right):
if type(right) is not Bag:
raise TypeError('Cannot compare Bag with' + type_as_str(right) + '. Can only compare Bag with Bag')
else:
return (len(self) == len(right)) and (self.unique() == right.unique())
def __ne__(self, right):
return not self.__eq__(right)
def _bag_gen(self, bag_value):
for item in self.bag_value:
for count in range(self.bag_value[item]):
yield item
def __iter__(self):
return self._bag_gen(self.bag_value)
if __name__ == '__main__':
# bag = Bag(['d','a','b','d','c','b','d'])
# bag2 = Bag(['d','a','b','d','c','b','d'])
# bag3 = Bag(['d','a','b','d','c','b'])
# print(bag == bag2)
# print(bag == bag3)
# print(bag != bag2)
# print(bag != bag3)
import driver
driver.driver()
First of all, it is important to note that your Bag class is basically the same as a Counter. If you need any specific extension, just inherit from it and you are done. I would say that the following code just address all the functionality you are implementing yourself in your Bag class:
from collections import Counter
class Bag(Counter):
pass
b = Bag(['d','a','b','d','c','b','d'])
c = Bag(['d','a','b','d','c','b','d'])
print (b)
print (c)
print (b+c)
If you are doing an exercise to learn, the problem is that you are not implementing setitem and getitem methods in your Bag class to allow the [] notation. You could implement them or just access the attribute bag_value:
def __add__(self, right):
mergedbag = Bag()
mergedbag.bag_value = copy.copy(self.bag_value)
for item in right.bag_value.keys():
mergedbag.bag_value[item] += right.bag_value[item]
return mergedbag
Be sure to create and return a Bag object
The definition is
def __add__(self,other):
So you ought to merge the contents of self.items with other.items and then return the result.
Also, not sure if bag1+bag2 will work, but my pyfu is weak, you will explicitly have to iterate through them and add the counts together.
import copy
c = {'a':2,'b':1}
d = {'b':1,'c':1}
#
mergedBag = copy.copy(c) #might wanna use deepcopy?
for k in d:
if k not in mergedBag: mergedBag[k] = 0
mergedBag[k] += d[k]
print mergedBag

Resources