Skip to content

Commit d015b23

Browse files

19 files changed

+190
-96
lines changed

Chapter01/chapter1.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
###############
1010

1111
var = 13.2
12-
print(var)
12+
print(type(var))
1313
#13.2
1414

1515
type (var)
1616
#<class ‘float’>
1717

1818
var = "Now the type is string"
19-
print(var)
19+
print(type(var))
2020

2121
type(var)
2222
#<class ‘str’>
@@ -36,6 +36,7 @@
3636
'''
3737

3838
bool(False)
39+
print(bool(False))
3940
#False
4041
va1 = 0
4142
print(bool(va1))
@@ -50,9 +51,11 @@
5051
#True
5152

5253
#### Strings
53-
Str1 = 'Hello how are you'
54+
str1 = 'Hello how are you'
5455
str2 = "Hello how are you"
5556
str3 = 'multiline'+'string';
57+
print(str1)
58+
print(str2)
5659
print(str3)
5760

5861
f = 'data'
@@ -69,6 +72,22 @@
6972
print(3 * st)
7073
#'data.data.data.'
7174

75+
###### Range ######
76+
77+
print(list(range(10)))
78+
print(range(10))
79+
print(list(range(10)))
80+
print(range(1,10,2))
81+
print(list(range(1,10,2)))
82+
print(list(range(20,10,-2)))
83+
84+
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
85+
#range(0, 10)
86+
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
87+
#range(1, 10, 2)
88+
#[1, 3, 5, 7, 9]
89+
#[20, 18, 16, 14, 12]
90+
7291

7392
###### Lists ######
7493

@@ -209,7 +228,7 @@
209228
if thirdList is Secondlist:
210229
print("Both are pointing to the same object")
211230
else:
212-
print("Both are not pointing to the same object ")
231+
print("Both are not pointing to the same object")
213232
'''
214233
Output:
215234
Both are equal
@@ -223,12 +242,12 @@
223242
Firstlist = []
224243
Secondlist = []
225244
if Firstlist is not Secondlist:
226-
print("Both are pointing to the different object")
245+
print("Both Firstlist and Secondlist variables are the same object")
227246
else:
228-
print("Both are not pointing to the different object ")
247+
print("Both Firstlist and Secondlist variables are not the same object")
229248

230249
#Output:
231-
# Both are pointing to the different object
250+
# Both Firstlist and Secondlist variables are the same object
232251

233252

234253

@@ -461,6 +480,7 @@
461480

462481
from collections import deque
463482
s = deque() # Creates an empty deque
483+
print(s)
464484
my_queue = deque([1, 2, 'Name'])
465485
print(my_queue)
466486
#deque([1, 2, 'Name'])
@@ -506,9 +526,9 @@
506526

507527
print(chain)
508528
#ChainMap({'data': 1, 'structure': 2}, {'python': 3, 'language': 4})
509-
print (list(chain.keys()))
529+
print(list(chain.keys()))
510530
#['python', 'language', 'data', 'structure']
511-
print (list(chain.values()))
531+
print(list(chain.values()))
512532
#[3, 4, 1, 2]
513533
print(chain["data"])
514534
#1

Chapter02/matrix_chain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ def matrix_chain(mat, i, j):
55
return 0
66
minimum_computations = sys.maxsize
77
for k in range(i, j):
8-
count = (MatrixChain(mat, i, k) + MatrixChain(mat, k+1, j)+ mat[i-1] * mat[k] * mat[j])
8+
count = (matrix_chain(mat, i, k) + matrix_chain(mat, k+1, j)+ mat[i-1] * mat[k] * mat[j])
99
if count < minimum_computations:
1010
minimum_computations = count
1111
return minimum_computations
1212

1313
matrix_sizes = [20, 30, 45, 50]
14-
print("Minimum multiplications are", MatrixChain(matrix_sizes , 1, len(matrix_sizes)-1))
14+
print("Minimum multiplications are", matrix_chain(matrix_sizes , 1, len(matrix_sizes)-1))

Chapter04/CircularList.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,26 @@ def append(self, data):
2828
def delete(self, data):
2929
current = self.head
3030
prev = self.head
31+
flag = False
3132
while prev == current or prev != self.tail:
3233
if current.data == data:
3334
if current == self.head:
35+
#item to be deleted is head node
3436
self.head = current.next
3537
self.tail.next = self.head
38+
elif current == self.tail:
39+
#item to be deleted is tail node
40+
self.tail = prev
41+
prev.next = self.head
3642
else:
43+
#item to be deleted is an intermediate node
3744
prev.next = current.next
3845
self.size -= 1
3946
return
4047
prev = current
4148
current = current.next
49+
if flag is False:
50+
print("Item not present in the list")
4251

4352

4453
def iter(self):
@@ -93,11 +102,3 @@ def iter(self):
93102
counter += 1
94103
if counter > 2:
95104
break
96-
97-
98-
99-
100-
101-
102-
103-

Chapter04/append_at_any_location_singly_linkedlist.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def __init__(self, data=None):
77

88
class SinglyLinkedList:
99
def __init__ (self):
10+
self.tail = None
1011
self.head = None
1112
self.size = 0
1213

Chapter04/delete_operation_singly_linkedlist.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ def __init__(self, data=None):
66

77
class SinglyLinkedList:
88
def __init__ (self):
9-
self.tail = None
9+
self.head = None
1010
self.size = 0
1111

1212
def append(self, data):
1313
# Encapsulate the data in a Node
1414
node = Node(data)
15-
if self.tail is None:
16-
self.tail = node
15+
if self.head is None:
16+
self.head = node
1717
else:
18-
current = self.tail
18+
current = self.head
1919
while current.next:
2020
current = current.next
2121
current.next = node
@@ -29,8 +29,8 @@ def delete_first_node (self):
2929

3030

3131
def delete_last_node (self):
32-
current = self.tail
33-
prev = self.tail
32+
current = self.head
33+
prev = self.head
3434
while current:
3535
if current.next is None:
3636
prev.next = current.next
@@ -40,12 +40,12 @@ def delete_last_node (self):
4040

4141

4242
def delete(self, data):
43-
current = self.tail
44-
prev = self.tail
43+
current = self.head
44+
prev = self.head
4545
while current:
4646
if current.data == data:
47-
if current == self.tail:
48-
self.tail = current.next
47+
if current == self.head:
48+
self.head = current.next
4949
else:
5050
prev.next = current.next
5151
self.size -= 1
@@ -59,18 +59,26 @@ def delete(self, data):
5959
words.append('ham')
6060
words.append('spam')
6161

62+
words.delete_first_node()
6263

64+
current = words.head
65+
while current:
66+
print(current.data)
67+
current = current.next
68+
69+
70+
6371
words.delete_last_node()
6472

65-
current = words.tail
73+
current = words.head
6674
while current:
6775
print(current.data)
6876
current = current.next
6977

7078

7179

7280
words.delete('ham')
73-
current = words.tail
81+
current = words.head
7482
while current:
7583
print(current.data)
7684
current = current.next

Chapter04/doubly_linked_list.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
class Node(object):
1+
class Node:
22
def __init__ (self, data = None, next = None, prev = None):
33
self.data = data
44
self.next = next
55
self.prev = prev
66

7-
class DoublyLinkedList(object):
7+
class DoublyLinkedList:
88
def __init__ (self):
99
self.head = None
1010
self.tail = None
1111
self.count = 0
1212

1313
def append(self, data):
14-
#Append an item to the list.
14+
#Append an item at the end of the list.
1515
new_node = Node(data, None, None)
1616
if self.head is None:
1717
self.head = new_node
@@ -49,21 +49,68 @@ def append_at_a_location(self, data):
4949
prev = current
5050
current = current.next
5151

52+
def iter(self):
53+
current = self.head
54+
while current:
55+
val = current.data
56+
current = current.next
57+
yield val
58+
59+
60+
def contains(self, data):
61+
for node_data in self.iter():
62+
if data == node_data:
63+
print(" Data item is present in the list. ")
64+
return
65+
print(" Data item is not present in the list. ")
66+
return
67+
68+
5269

5370

5471
words = DoublyLinkedList()
5572
words.append('egg')
5673
words.append('ham')
5774
words.append('spam')
5875

76+
print("Items in doubly linked list before append")
5977
current = words.head
6078
while current:
6179
print(current.data)
6280
current = current.next
6381

82+
words.append_at_start('book')
83+
84+
print("Items in doubly linked list after append")
85+
current = words.head
86+
while current:
87+
print(current.data)
88+
current = current.next
89+
90+
91+
words.append('book')
92+
93+
print("Items in doubly linked list after adding element at end.")
94+
current = words.head
95+
while current:
96+
print(current.data)
97+
current = current.next
6498

99+
100+
65101
words.append_at_a_location('ham')
102+
print("Doubly linked list after adding an element after word \"ham\" in the list.")
66103
current = words.head
67104
while current:
68105
print(current.data)
69106
current = current.next
107+
108+
109+
words = DoublyLinkedList()
110+
words.append('egg')
111+
words.append('ham')
112+
words.append('spam')
113+
114+
115+
words.contains("ham")
116+
words.contains("ham2")

Chapter05/List_based_queue.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@ def dequeue(self):
2323

2424

2525

26-
q= ListQueue()
27-
q.enqueue(4)
28-
q.enqueue('dog')
29-
q.enqueue('cat')
30-
q.enqueue('monday')
26+
q = ListQueue()
27+
q.enqueue(20)
28+
q.enqueue(30)
29+
q.enqueue(40)
30+
q.enqueue(50)
31+
print(q.items)
32+
#Queue is full
33+
#[20, 30, 40]
3134

3235

33-
a= q.size1()
36+
data = q.dequeue()
37+
print(data)
38+
print(q.items)
39+
#20
40+
#[30, 40]
41+
42+
43+
a = q.size1()
3444
print(a)

Chapter05/Stack.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ def peek(self):
4343

4444

4545

46-
words = stack()
47-
words.push('4')
48-
words.push('5')
49-
words.push('6')
50-
words.push('7')
46+
words = Stack()
47+
words.push('egg')
48+
words.push('ham')
49+
words.push('spam')
5150

5251
#print the stack elements.
5352
current = words.top

0 commit comments

Comments
 (0)