Skip to content

Commit 0ec5e58

Browse files
author
Mayank Kumar
committed
Added usage of slots, generator.send()
1 parent 743451d commit 0ec5e58

4 files changed

Lines changed: 57 additions & 4 deletions

File tree

2048/commands.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# The commands allowed are up, down, left and right
1+
# The commands allowed are up, down, left and right
2+
3+
# WIP

TreeADT/bst.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,34 +234,42 @@ def floor(root, key):
234234
root.right.right = BSTNode(7)
235235

236236
val = 15
237+
print "Search BST: "
237238
print "Search", val, "in BST: ", search(root, 15)
239+
print "*"*80
238240

239241
print "minimum value in BST:", search_min(root)
240242
print "maximum value in BST:", search_max(root)
241243
print "maximum without recursion:", search_max_non_recursion(root)
244+
print "*"*80
242245

243246
x = 1
244247
y = 12
245248
print "LCA of", x, "and", y, "is", lowest_common_ancestor(root, x, y)
249+
print "*"*80
246250

247251
print "Tree is BST:", is_bst(root)
248252
print "Tree is BST using in order traversal", check(is_bst_inorder(root))
253+
print "*"*80
249254

250255
key = 6
251256
suc = None
252257
pre = None
253258
val = get_pre_suc(root, key, suc, pre)
254259
print "predecessor and successor of", key, "are", val[0], val[1]
260+
print "*"*80
255261

256262
k = 3
257-
print k, "rd smallest element is:", kth_smallest_element(root, k)
263+
print k, " smallest element is:", kth_smallest_element(root, k)
264+
print "*"*80
258265

259266
a = [1,2,3,4,5,6,7]
260267
l = 0
261268
r = len(a)
262269
new_tree_root = buildBST(a, l, r-1)
263270
print "New BST built. Root nodes data:", new_tree_root.data
271+
print "*"*80
264272

265273
key = 7.1
266274
print "ceiling value of", key, "is", ceil(root, key)
267-
print "floor value of", key, "is", floor(root, key)
275+
print "floor value of", key, "is", floor(root, key)

UsageSlots.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class foo(object):
2+
pass
3+
4+
class bar(object):
5+
__slots__ = ['a', 'b']
6+
7+
print "Evaluation for class foo() ===>"
8+
ob1 = foo()
9+
ob1.a = 1
10+
print "Value of foo().a: ", ob1.a
11+
# print dir(ob1)
12+
print "__dict__ attribute: ", ob1.__dict__
13+
14+
print ""
15+
print "Evaluation for class bar() ===>"
16+
ob2 = bar()
17+
# print dir(ob2)
18+
try:
19+
print ob2.__dict__
20+
except AttributeError:
21+
print "bar() does not contain __dict__ attribute"
22+
print "type(__slots__): ", type(ob2.__slots__)
23+
try:
24+
ob2.c = 3
25+
print ob2.c
26+
except AttributeError:
27+
print "Cannot assign attribute 'c' to class because of defined __slots__"

iterators_and_generators.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,20 @@ def factorial(n):
7272
fact *= some_count
7373

7474
n = 5
75-
print("First %d factorials are: %s" %(n, list(factorial(n))))
75+
print("First %d factorials are: %s" %(n, list(factorial(n))))
76+
77+
78+
print "*"*80
79+
# use of send in generators
80+
def fibonacci():
81+
x, y, z = 0, 1, 0
82+
while z < 4:
83+
x, y = y, x+y
84+
z = yield x
85+
print x, z
86+
87+
ob = fibonacci()
88+
for i in xrange(100):
89+
print "Yielded value: ", ob.next()
90+
if i == 5:
91+
ob.send(i)

0 commit comments

Comments
 (0)