Add new course
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Week 3: Programming Assignments (8 points)</title>
|
||||
<meta charset="utf-8">
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<h4><strong>Assignment 3.1: Linked List </strong>(5 points)<strong><br></strong></h4>
|
||||
</div>
|
||||
<div>Implement the linked list data structure in Python. Create a class <strong>Node</strong> which stores the data and link to another Node class. Create also the class <strong>LinkedList</strong> which maintains the linked list created by Node classes and has the following methods:
|
||||
</div>
|
||||
<ul>
|
||||
<ul>
|
||||
<li><strong>append(data: object)</strong>: inserts a new Node containing the data to the end of the list.<br></li>
|
||||
<li><strong>insert(data: object, index: int)</strong>: inserts a new Node containing the data to the position indicated by the index (the index of the first node is 0).</li>
|
||||
<li><strong>delete(index: int)</strong>: deletes a node from the position indicated by the index.</li>
|
||||
<li><strong>index(data: object)</strong>: search the Node containing the data and returns its index. Returns \(-1\) if not found.</li>
|
||||
<li><strong>print()</strong>: prints the content of linked list (format: node1 -> node2 -> node3).</li>
|
||||
</ul>
|
||||
</ul>
|
||||
<div>A code template with an example program: </div>
|
||||
<p></p>
|
||||
<div style="border:2px solid black">
|
||||
<pre>class Node:
|
||||
# TODO
|
||||
|
||||
|
||||
class LinkedList:
|
||||
# TODO
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
L = LinkedList()
|
||||
L.append(1)
|
||||
L.append(3)
|
||||
L.print() # 1 -> 3
|
||||
L.insert(10, 1)
|
||||
L.insert(15, 0)
|
||||
L.print() # 15 -> 1 -> 10 -> 3
|
||||
print(L.index(1)) # 1
|
||||
L.delete(0)
|
||||
L.print() # 1 -> 10 -> 3
|
||||
</pre>
|
||||
</div>
|
||||
<p></p>
|
||||
<div>Submit your solution in CodeGrade as <strong>linked.py</strong>.</div>
|
||||
<div><br><br></div>
|
||||
|
||||
|
||||
<div>
|
||||
<h4><strong>Assignment 3.2: Binary Search </strong>(3 points)</h4>
|
||||
</div>
|
||||
<div>Implement the binary search algorithm in Python. Create a function <strong>search(A: list, item: int)</strong> which returns the index of the item in the list. If the item is not found the function returns \(-1\). You can assume that the list contains only
|
||||
integers and it is always sorted.</div>
|
||||
<div><br>Limits:</div>
|
||||
<ul>
|
||||
<ul>
|
||||
<li>Integers in the list are between \(1...10^6\)</li>
|
||||
<li>The maximum length of the list is \(10^6\)</li>
|
||||
</ul>
|
||||
</ul>
|
||||
<div>Target: the algorithm performs in \(\Theta(\log(n))\) time.</div>
|
||||
<p></p>
|
||||
<div>A code template with an example program: </div>
|
||||
<p></p>
|
||||
<div style="border:2px solid black">
|
||||
<pre>def search(A: list, item: int):
|
||||
# TODO
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
A = [1, 2, 3, 6, 10, 15, 22, 27, 30, 31]
|
||||
print(search(A, 6)) # 3
|
||||
print(search(A, 7)) # -1
|
||||
print(search(A, 30)) # 8
|
||||
</pre>
|
||||
</div>
|
||||
<div><br></div><div>Submit your solution in CodeGrade as <strong>binarysearch.py</strong>.</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,20 @@
|
||||
def search(A: list, item: int):
|
||||
low = 0
|
||||
high = len(A) - 1
|
||||
mid = 0
|
||||
while low <= high:
|
||||
mid = (high + low) // 2
|
||||
if A[mid] < item:
|
||||
low = mid + 1
|
||||
elif A[mid] > item:
|
||||
high = mid - 1
|
||||
else:
|
||||
return mid
|
||||
return -1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
A = [1, 2, 3, 6, 10, 15, 22, 27, 30, 31]
|
||||
print(search(A, 6)) # 3
|
||||
print(search(A, 7)) # -1
|
||||
print(search(A, 30)) # 8
|
||||
@@ -0,0 +1,97 @@
|
||||
class Node:
|
||||
def __init__(self, data=None):
|
||||
self.data = data
|
||||
self.next = None
|
||||
|
||||
|
||||
class LinkedList:
|
||||
def __init__(self):
|
||||
self.head = None
|
||||
|
||||
def append(self, data):
|
||||
new = Node(data)
|
||||
if self.head is None:
|
||||
self.head = new
|
||||
return
|
||||
last = self.head
|
||||
while (last.next):
|
||||
last = last.next
|
||||
last.next = new
|
||||
|
||||
def print(self):
|
||||
val = self.head
|
||||
while val is not None:
|
||||
if val.next is None:
|
||||
print(val.data)
|
||||
else:
|
||||
print(val.data, end=" -> ")
|
||||
val = val.next
|
||||
|
||||
def length(self):
|
||||
temp = self.head
|
||||
count = 0
|
||||
while (temp != None):
|
||||
count += 1
|
||||
temp = temp.next
|
||||
return count
|
||||
|
||||
def insert(self, data, index):
|
||||
new = Node(data)
|
||||
if (index == 0):
|
||||
new.next = self.head
|
||||
self.head = new
|
||||
else:
|
||||
temp = self.head
|
||||
for i in range(1, index):
|
||||
if (temp != None):
|
||||
temp = temp.next
|
||||
if (temp != None):
|
||||
new.next = temp.next
|
||||
temp.next = new
|
||||
else:
|
||||
print("\nThe previous node is null.")
|
||||
|
||||
def index(self, data):
|
||||
temp = self.head
|
||||
index = 0
|
||||
while (temp != None):
|
||||
if (temp.data == data):
|
||||
return index
|
||||
temp = temp.next
|
||||
index += 1
|
||||
return -1
|
||||
|
||||
def delete(self, index):
|
||||
if (index == 0):
|
||||
self.head = self.head.next
|
||||
elif (index >= self.length()):
|
||||
return
|
||||
else:
|
||||
temp = self.head
|
||||
for i in range(1, index):
|
||||
if (temp != None):
|
||||
temp = temp.next
|
||||
if (temp != None):
|
||||
temp.next = temp.next.next
|
||||
else:
|
||||
print("\nThe previous node is null.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
L = LinkedList()
|
||||
L.append(2)
|
||||
L.append(3)
|
||||
L.append(1)
|
||||
L.append(4)
|
||||
L.print() # 1 -> 3
|
||||
L.insert(4, 3)
|
||||
L.insert(1, 0)
|
||||
L.insert(3, 2)
|
||||
L.insert(2, 1)
|
||||
L.print() # 15 -> 1 -> 10 -> 3
|
||||
print(L.index(1)) # 1
|
||||
L.delete(0)
|
||||
L.delete(1)
|
||||
L.delete(4)
|
||||
L.delete(5)
|
||||
L.print() # 1 -> 10 -> 3
|
||||
Reference in New Issue
Block a user