Skip to content

Commit f300301

Browse files
authored
Add files via upload
0 parents  commit f300301

7 files changed

+2037
-0
lines changed

Common_data_structures_class.py

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Jan 24 19:21:12 2018
4+
@author: Tirtha
5+
"""
6+
7+
class Stack:
8+
"""
9+
Stack data structure using list
10+
"""
11+
def __init__(self,value):
12+
"""
13+
Class initializer: Produces a stack with a single value or a list of values
14+
"""
15+
self._items = []
16+
if type(value)==list:
17+
for v in value:
18+
self._items.append(v)
19+
self._height = len(value)
20+
else:
21+
self._items.append(value)
22+
self._height = 1
23+
24+
def pop(self):
25+
if self._height == 0:
26+
print ("Stack is empty. Nothing to pop.")
27+
return None
28+
else:
29+
self._height -=1
30+
return self._items.pop()
31+
32+
def push(self,value):
33+
self._height +=1
34+
self._items.append(value)
35+
36+
def isEmpty(self):
37+
return self._height==0
38+
39+
def draw(self):
40+
if self.isEmpty():
41+
pass
42+
return None
43+
else:
44+
n=self._height
45+
print('['+str(self._items[n-1])+']')
46+
for i in range(n-2,-1,-1):
47+
print(" | ")
48+
print('['+str(self._items[i])+']')
49+
50+
###==============================================================================================
51+
52+
class Queue:
53+
"""
54+
Queue data structure using list
55+
"""
56+
def __init__(self,value):
57+
"""
58+
Class initializer: Produces a queue with a single value or a list of values
59+
"""
60+
self._items = []
61+
if type(value)==list:
62+
for v in value:
63+
self._items.append(v)
64+
self._length = len(value)
65+
else:
66+
self._items.append(value)
67+
self._length = 1
68+
69+
def dequeue(self):
70+
if self._length == 0:
71+
print ("Queue is empty. Nothing to dequeue.")
72+
return None
73+
else:
74+
self._length -=1
75+
return self._items.pop(0)
76+
77+
def enqueue(self,value):
78+
self._length +=1
79+
self._items.append(value)
80+
81+
def isEmpty(self):
82+
return self._length==0
83+
84+
def draw(self):
85+
if self.isEmpty():
86+
pass
87+
return None
88+
else:
89+
n=self._length
90+
for i in range(n-1):
91+
print('['+str(self._items[i])+']-',end='')
92+
print('['+str(self._items[n-1])+']')
93+
94+
###=================================================================================
95+
96+
class Tree:
97+
"""
98+
Recursive definition of Tree class plus various helper functions/methods
99+
"""
100+
def __init__(self, value, children):
101+
"""
102+
Class initializer: Produces a single root node having a specific string value;
103+
children is a list of references to the root of the children branches.
104+
Note th children are trees themselves, hence the recursion.
105+
"""
106+
self._value = value
107+
self._children = children
108+
109+
def strep(self):
110+
"""
111+
Generates a string representation of the tree
112+
"""
113+
text = ""
114+
text+=str(self._value)
115+
116+
for child in self._children:
117+
text+='['
118+
text+=child.strep()
119+
text+=']'
120+
return text
121+
122+
def get_value(self):
123+
return self._value
124+
125+
def children(self):
126+
for child in self._children:
127+
yield child
128+
129+
def num_nodes(self):
130+
result = 1
131+
for child in self._children:
132+
result+=child.num_nodes()
133+
return result
134+
135+
def num_leaves(self):
136+
if len(self._children)==0:
137+
return 1
138+
else:
139+
result=0
140+
for child in self._children:
141+
result += child.num_leaves()
142+
return result
143+
144+
def height(self):
145+
height=0
146+
for child in self._children:
147+
height = max(height,child.height()+1)
148+
return height
149+
150+
###================================================================================
151+
152+
class Arithmatic (Tree):
153+
154+
def __init__(self, value, children):
155+
Tree.__init__(self, value, children)
156+
157+
def strexp(self):
158+
if len(self._children)==0:
159+
return str(self._value)
160+
else:
161+
text='('
162+
text+=self._children[0].strexp()
163+
text+=str(self._value)
164+
text+=self._children[1].strexp()
165+
text+=')'
166+
return text
167+
168+
def evaluate_exp(self):
169+
if len(self._children)==0:
170+
if ('.' in self._value):
171+
return float(self._value)
172+
else:
173+
return int(self._value)
174+
else:
175+
function = self._value
176+
left_value = self._children[0].evaluate_exp()
177+
right_value = self._children[1].evaluate_exp()
178+
if function=='+':
179+
return left_value+right_value
180+
if function=='-':
181+
return left_value-right_value
182+
if function=='*':
183+
return left_value*right_value
184+
if function=='/':
185+
return left_value/right_value
186+
if function=='%':
187+
return left_value%right_value
188+
if function=='^':
189+
return left_value**right_value

Common_data_structures_test.py

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Jan 24 21:26:42 2018
4+
@author: Tirtha
5+
"""
6+
7+
from Common_data_structures_class import Stack, Queue, Tree, Arithmatic
8+
9+
###Test code for Stack
10+
###======================================================
11+
init_list=[11,22,33,44]
12+
print("Initializing stack with a list:",init_list)
13+
s1 = Stack(init_list)
14+
s1.draw()
15+
print("height of the stack:",s1._height)
16+
print("Popping the top of the stack:",s1.pop())
17+
print("height of the stack now:",s1._height)
18+
print("Pushing 20...")
19+
s1.push(20)
20+
print("Pushing 30...")
21+
s1.push(30)
22+
s1.draw()
23+
print("height of the stack now:",s1._height)
24+
print("Popping the top of the stack:",s1.pop())
25+
print("Height now:",s1._height)
26+
print("Popping the top of the stack:",s1.pop())
27+
print("Height now:",s1._height)
28+
print("Popping the top of the stack:",s1.pop())
29+
print("Height now:",s1._height)
30+
print("Popping the top of the stack:",s1.pop())
31+
print("Height now:",s1._height)
32+
print("Popping the top of the stack:",s1.pop())
33+
print("Height now:",s1._height)
34+
print("Popping the top of the stack:",s1.pop())
35+
print("Height now:",s1._height)
36+
print("Is the stack empty?",s1.isEmpty())
37+
print("Pushing a float")
38+
s1.push(3.56)
39+
print("Pushing a string")
40+
s1.push("Hello")
41+
s1.draw()
42+
print("Popping the top of the stack:",s1.pop())
43+
print("Popping the top of the stack:",s1.pop())
44+
print("Is the stack empty?",s1.isEmpty())
45+
46+
###Test code for Queue
47+
###======================================================
48+
init_list=[99,98,97,96]
49+
print("Initializing queue with a list:",init_list)
50+
q1 = Queue(init_list)
51+
q1.draw()
52+
print("Length of the queue:",q1._length)
53+
print("Dequeing from the queue:",q1.dequeue())
54+
print("Length of the queue now:",q1._length)
55+
print("Enqueing 20...")
56+
q1.enqueue(20)
57+
print("Enqueing 30...")
58+
q1.enqueue(30)
59+
q1.draw()
60+
print("Length of the queue now:",q1._length)
61+
print("Dequeing from the queue:",q1.dequeue())
62+
print("Length now:",q1._length)
63+
print("Dequeing from the queue:",q1.dequeue())
64+
print("Length now:",q1._length)
65+
print("Dequeing from the queue:",q1.dequeue())
66+
print("Length now:",q1._length)
67+
print("Dequeing from the queue:",q1.dequeue())
68+
print("Length now:",q1._length)
69+
print("Dequeing from the queue:",q1.dequeue())
70+
print("Length now:",q1._length)
71+
print("Dequeing from the queue:",q1.dequeue())
72+
print("Length now:",q1._length)
73+
print("Is the queue empty?",q1.isEmpty())
74+
print("Enqueing a float")
75+
q1.enqueue(3.56)
76+
print("Enqueing a string")
77+
q1.enqueue("Hello")
78+
q1.draw()
79+
print("Dequeing from the queue:",q1.dequeue())
80+
print("Dequeing from the queue:",q1.dequeue())
81+
print("Is the queue empty?",q1.isEmpty())
82+
83+
###Test code for Tree
84+
###======================================================
85+
tree_a = Tree('a',[])
86+
tree_b = Tree('b',[])
87+
88+
tree_cab = Tree('c',[tree_a,tree_b])
89+
90+
print (tree_a.strep())
91+
print(tree_b.get_value())
92+
print(tree_cab.strep())
93+
94+
tree_4 = Tree('d',[tree_cab,tree_b,tree_a])
95+
print (tree_4.strep())
96+
print(tree_4.num_nodes())
97+
print(tree_4.num_leaves())
98+
print(tree_4.height())
99+
100+
###Test code for Arithmatic
101+
###======================================================
102+
#exp1=Arithmatic('*',[])
103+
exp2=Arithmatic('1.2',[])
104+
exp3=Arithmatic('5.2',[])
105+
exp4=Arithmatic('*',[exp2,exp3])
106+
exp5=Arithmatic('4.2',[])
107+
exp6=Arithmatic('7.5',[])
108+
exp7=Arithmatic('+',[exp5,exp6])
109+
exp8=Arithmatic('/',[exp4,exp7])
110+
exp9=Arithmatic('2',[])
111+
exp10=Arithmatic('^',[exp9,exp8])
112+
113+
print("The expression is:",exp10.strexp())
114+
print("The evluated value is:",exp10.evaluate_exp())
115+
print("String representation is:",exp10.strep())
116+
print("Depth of this computation graph tree is:",exp10.height())

Graph-1.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Thu Jun 14 20:55:21 2018
4+
5+
@author: Tirtha
6+
"""
7+
8+
class Node (object):
9+
def __init__(self,name):
10+
self.name=name
11+
12+
def getName(self):
13+
return (self.name)
14+
15+
def __str__(self):
16+
return (self.name)
17+
18+
class Edge (object):
19+
def __init__(self,src,dest):
20+
self.src=src
21+
self.dest=dest
22+
23+
def getSource(self):
24+
return (self.src)
25+
26+
def getDest(self):
27+
return (self.dest)
28+
29+
def __str__(self):
30+
return (self.src+"->"self.dest)

0 commit comments

Comments
 (0)