-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab assignment 6.py
More file actions
235 lines (183 loc) · 5.68 KB
/
Copy pathLab assignment 6.py
File metadata and controls
235 lines (183 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Task_1
def exChange(i, j):
array[i], array[j] = array[j], array[i]
def selectionSort(array, i, j, flag):
size = len(array)
if (i < size - 1):
if (j < size):
if (array[i] > array[j]):
exChange(i, j)
selectionSort(array, i, j + 1, 0)
else:
selectionSort(array, i + 1, i+2, 1)
array = [64, 22, 55, 37, 56, 700, 10]
selectionSort(array, 0, 1, 1)
print(array)
################################################################################
# Task_2
def insertion(val, array):
if (len(array) == 0):
return [val]
elif (val < array[0]):
return [val] + array
else:
return (array[0:1] + insertion(val, array[1:]))
def sorting(array):
if (len(array) == 0):
return array
if (len(array) == 1):
return array
else:
return (insertion(array[0], sorting(array[1:])))
array = [23, 24, 35, 66, 47, 10]
print(sorting(array))
###############################################################################
# Task_3
class Node:
def __init__(self, Value=None, next=None):
self.Value = Value
self.next = next
class Singly_Linked:
def __init__(self):
self.head = None
def push(self, value):
node = Node(value)
node.next = self.head
self.head = node
def bubbleSorting(self):
temp2 = None
while self.head.next != temp2:
x = self.head
while x.next != temp2:
y = x.next
if (x.Value > y.Value):
y.Value, x.Value = x.Value, y.Value
x = x.next
temp2 = x
def showlist(self):
temp = self.head
while temp != None:
print(temp.Value, end=",")
temp = temp.next
print()
test = Singly_Linked()
array = [55, 22, 82, 10]
for i in array:
test.push(i)
print("UnSorted list: ", end=" ")
test.showlist()
test.bubbleSorting()
print("Sorted list: ", end=" ")
test.showlist()
##########################################################################
# Task_4
class Node:
def __init__(self, Value=None, next=None):
self.Value = Value
self.next = next
class Singly_Linked:
def __init__(self):
self.head = None
def push(self, value):
node = Node(value)
node.next = self.head
self.head = node
def selection_sort(self):
temp2 = self.head
while temp2 != None:
min_val = temp2
x = temp2.next
while x != None:
if x.Value < min_val.Value:
min_val = x
x = x.next
temp2.Value, min_val.Value = min_val.Value, temp2.Value
temp2 = temp2.next
def showlist(self):
temp = self.head
while temp != None:
print(temp.Value, end=",")
temp = temp.next
print()
test = Singly_Linked()
array = [55, 22, 82, 10]
for i in array:
test.push(i)
test.selection_sort()
print("Sorted List:", end=" ")
test.showlist()
###############################################################################
# task_5
class Node:
def __init__(self, value, next, prev):
self.value = value
self.next = next
self.prev = prev
class Doublylinkedlist:
def __init__(self, array):
self.head = Node(array[0], None, None)
tail = self.head
for x in range(1, len(array)):
new_node = Node(array[x], None, tail)
tail.next = new_node
tail = new_node
def showList(self):
if self.head.next is None:
print("Empty list")
else:
head = self.head
while self.head is not None:
if head.next is None:
print(head.value)
break
else:
print(head.value, end=" ")
head = head.next
def insertionSort(self):
head = self.head
if head == None:
return
else:
while head is not None:
tail = head.next
while tail and tail.prev != None and tail.value < tail.prev.value:
tail.value, tail.prev.value = tail.prev.value, tail.value
tail = tail.prev
head = head.next
array = [7, 6, 9, 3, 5, 99, 1]
doublylinkedlist = Doublylinkedlist(array)
doublylinkedlist.insertionSort()
doublylinkedlist.showList()
#######################################################################
# task_6
def binarySearch(test, search, head, tail):
mid_val = (head + tail) // 2
if(mid_val >= len(test) or mid_val < 0):
return "Error"
mid_item = test[mid_val]
if (head > tail):
return "Error"
if (mid_item == search):
return mid_val
if (mid_item < search):
head = mid_val + 1
else:
tail = mid_val - 1
return binarySearch(test, search, head, tail)
test = [10, 347, 453, 954, 475, 7, 5627, 6854]
index = binarySearch(test, 475, 0, len(test))
print(index)
##############################################################################
# task_7
memoization = {}
def nth_fibonacci(nth):
if nth in memoization:
return memoization[nth]
if nth == 0 or nth == 1:
return nth
else:
item = nth_fibonacci(nth-1) + nth_fibonacci(nth-2)
memoization[nth] = item
return item
n = int(input())
print(n, "th Fibonacci number is :", nth_fibonacci(n))