Add new course

This commit is contained in:
AndrewTrieu
2023-01-08 18:43:01 +02:00
parent 75313f3f05
commit 6f5c7f67b4
49 changed files with 835669 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
<!doctype html>
<html>
<head>
<title>Week 7 Programming Assignments (9 points)</title>
<meta charset="utf-8">
</head>
<body>
<div>
<h4><span>Assignment 7.1: The Quicksort</span> <span style="font-weight: normal;">(4 point)</span><br></h4>
</div>
<div>Familiarize yourself with the principle of Quicksort from the background material and implement Quicksort in Python.
Create a function <strong>qsort(A: list, i: int, j: int)</strong> which has the following input parameters:</div>
<ul>
<ul>
<li>\(A\): a (whole) list of integers</li>
<li>\(i\): a start index of selected partition</li>
<li>\(j\): a end index of selected partition</li>
</ul>
</ul>
<div>The function has no return value since the sorting process is done to the original list. You can select the pivot index/value as you wish.</div>
<br>
<div>Limits:</div>
<ul>
<ul>
<li>maximum list size is \(10^4\)</li>
<li>all elements of the list are integers between \(1 \dots 10^4\)</li>
</ul>
</ul>
<div>A code template with an example program: </div>
<p></p>
<div style="border:2px solid black">
<pre>def qsort(A, i, j):
# TODO
if __name__ == "__main__":
A = [9, 7, 1, 8, 5, 3, 6, 2, 4]
print(A) # [9, 7, 1, 8, 5, 3, 6, 2, 4]
qsort(A, 0, len(A)-1)
print(A) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
</pre>
</div>
<br>
<div>Submit your solution in CodeGrade as <strong>quicksort.py</strong>.</div>
<br>
<br>
<div>
<h4>Assignment 7.2: Car Sales<span style="font-weight: normal;"> (3 points)</span><br></h4>
</div>
<div>A car shop has cars \(A = [a_1, a_2, ..., a_n]\) (one of each) where \(a_i\) is the price of the car \(i\). Customers \(B = [b_1, b_2, ..., b_m]\) arrive to the shop.
\(b_i\) is the price that the customer \(i\) can afford. What is the maximum amount of sales that can be made?<br></div>
<br>
<div>For example the shop has cars \(A = [20, 10, 15, 26]\) and there are customers \(B = [11, 25, 15, 9]\) it is possible to make \(3\) sales.</div>
<ul>
<ul>
<li>The first customer (\(11\)) gets car that cost \(10\), The second customer (\(25\)) gets the car that cost \(20\), and the third customer (\(15\)) gets the car that cost \(15\).</li>
</ul>
</ul>
<div>Create a function <strong>sales(A: list, B: list)</strong> in Python which returns the number of possible sales.</div>
<br>
<div>Limits:</div>
<ul>
<ul>
<li>\(1 \leq n,m \leq 10^4\)</li>
<li>\(1 \leq a_i, b_i \leq 10^4\)</li>
</ul>
</ul>
<div>Target: the function performs in \(\Theta(n \log n)\) time.</div>
<br>
<div>A code template with an example program: </div>
<p></p>
<div style="border:2px solid black">
<pre>def sales(cars, customers) -&gt; int:
# TODO
if __name__ == "__main__":
print(sales([20, 10, 15], [11, 25, 15])) # 3
print(sales([13, 7, 2, 3, 12, 4, 19], [3, 25, 16, 14])) # 4
print(sales([24, 6, 20, 21, 12, 5], [25, 1, 24, 15])) # 3
print(sales([14, 9, 10, 15, 18, 20], [24, 17, 9, 22, 12, 4])) # 5
</pre>
</div>
<br>
<div>Submit your solution in CodeGrade as <strong>sales.py</strong>.</div>
<br>
<br>
<div>
<h4>Assignment 7.3: Subsets<span style="font-weight: normal;"> (2 points)</span><br></h4>
</div>
<div>A given set that has numbers from \(1\) to \(N\) in increasing order (\(\{1, 2, 3, 4, \dots, N\}\)), create a function <strong>subsets(N: int)</strong> in Python which produces a list of all possible subsets.</div>
<br>
<div>For example when \(N = 3\) the subsets are \([1]\), \([2]\), \([1, 2]\), \([3]\), \([1, 3]\), \([2, 3]\) and \([1, 2, 3]\).</div>
<br>
<div>The function must return the list of subsets in specific order (see the example program below).<br></div><p></p><div>Limits: \(1 \leq N \leq 20\)<br></div><div><br></div><div>A code template with an example program:
</div><p></p>
<div style="border:2px solid black">
<pre>def subsets(n: int) -&gt; list:
# TODO
if __name__ == "__main__":
print(subsets(3)) # [[1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
print(subsets(4)) # [[1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3],
# [4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4],
# [2, 3, 4], [1, 2, 3, 4]]
S = subsets(10)
print(S[95]) # [6, 7]
print(S[254]) # [1, 2, 3, 4, 5, 6, 7, 8]
print(S[826]) # [1, 2, 4, 5, 6, 9, 10]
</pre>
</div>
<p><br></p><p>Submit your solution in CodeGrade as <strong>subsets.py</strong></p>
</body>
</html>

View File

@@ -0,0 +1,24 @@
def qsort(A, i, j):
if i >= j:
return
p = partition(A, i, j)
qsort(A, i, p-1)
qsort(A, p+1, j)
def partition(A, i, j):
pivot = A[j]
p = i
for q in range(i, j):
if A[q] < pivot:
A[p], A[q] = A[q], A[p]
p += 1
A[p], A[j] = A[j], A[p]
return p
if __name__ == "__main__":
A = [9, 7, 1, 8, 5, 3, 6, 2, 4]
print(A) # [9, 7, 1, 8, 5, 3, 6, 2, 4]
qsort(A, 0, len(A)-1)
print(A) # [1, 2, 3, 4, 5, 6, 7, 8, 9]

View File

@@ -0,0 +1,18 @@
def sales(cars, customers) -> int:
cars.sort()
customers.sort()
sales = 0
for customer in customers:
for car in cars:
if car <= customer:
sales += 1
cars.remove(car)
break
return sales
if __name__ == "__main__":
print(sales([20, 10, 15], [11, 25, 15])) # 3
print(sales([13, 7, 2, 3, 12, 4, 19], [3, 25, 16, 14])) # 4
print(sales([24, 6, 20, 21, 12, 5], [25, 1, 24, 15])) # 3
print(sales([14, 9, 10, 15, 18, 20], [24, 17, 9, 22, 12, 4])) # 5

View File

@@ -0,0 +1,26 @@
def subsets(a: int) -> list:
s = [i for i in range(1, a+1)]
return [x for x in get_sets(s) if x]
def get_sets(s):
size = 2**len(s)
for cnt in range(0, size):
final = []
for i in range(0, len(s)):
if ((cnt & (1 << i)) > 0):
final.append(s[i])
yield final
if __name__ == "__main__":
print(subsets(3)) # [[1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
print(subsets(4)) # [[1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3],
# [4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4],
# [2, 3, 4], [1, 2, 3, 4]]
S = subsets(10)
print(S[95]) # [6, 7]
print(S[254]) # [1, 2, 3, 4, 5, 6, 7, 8]
print(S[826]) # [1, 2, 4, 5, 6, 9, 10]