|
|
|
|
@@ -0,0 +1,128 @@
|
|
|
|
|
<!doctype html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<title>Week 1 Programming Assignments (9 points)</title>
|
|
|
|
|
<meta charset="utf-8">
|
|
|
|
|
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div>
|
|
|
|
|
<h4><strong> Assignment 1.1: Insertion Sort</strong> (3 points)</h4>
|
|
|
|
|
</div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> Following pseudo code sorts an array of integers. Command swap switches values of two variables. </div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<pre>function isort(A)
|
|
|
|
|
for i = 1 to size(A)-1
|
|
|
|
|
j = i-1
|
|
|
|
|
while (j >= 0) and (A[j] > A[j+1])
|
|
|
|
|
swap(A[j], A[j+1])
|
|
|
|
|
j = j-1
|
|
|
|
|
return
|
|
|
|
|
</pre>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> Create the following function in Python: </div>
|
|
|
|
|
<ul>
|
|
|
|
|
<ul>
|
|
|
|
|
<li> <strong>isort(A: list)</strong>: sorts a given list of integers. Implement the pseudo code to this function. </li>
|
|
|
|
|
</ul>
|
|
|
|
|
</ul>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> Limits: </div>
|
|
|
|
|
<ul>
|
|
|
|
|
<ul>
|
|
|
|
|
<li> the maximum length of the list is \(10^3\) </li>
|
|
|
|
|
<li> each integer is between \(1...10^3\) </li>
|
|
|
|
|
</ul>
|
|
|
|
|
</ul>
|
|
|
|
|
<div>A code template with an example program: </div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div style="border:2px solid black">
|
|
|
|
|
<pre>def isort(A):
|
|
|
|
|
# TODO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
A = [4, 3, 6, 2, 9, 7, 1, 8, 5]
|
|
|
|
|
isort(A)
|
|
|
|
|
print(A) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
</pre>
|
|
|
|
|
</div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> Submit your solution in CodeGrade as <strong>isort.py</strong>. </div>
|
|
|
|
|
<br>
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<h4><strong> Assignment 1.2 Prime Numbers</strong> <span>(3 points)</span></h4>
|
|
|
|
|
</div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> For the background read the first paragraph of this article: <a href="https://en.wikipedia.org/wiki/Prime_number">https://en.wikipedia.org/wiki/Prime_number</a></div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> Given a number \(N\) how many prime numbers are less or equal to \(N\)? For example if \(N = 7\) there are four prime numbers: \(2\), \(3\), \(5\) and \(7\) (note that 1 is not a prime number). </div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> Create the following function in Python: </div>
|
|
|
|
|
<ul>
|
|
|
|
|
<ul>
|
|
|
|
|
<li> <strong>primes(N: int)</strong>: returns the numbers of primes that are less or equal to \(N\) </li>
|
|
|
|
|
</ul>
|
|
|
|
|
</ul>
|
|
|
|
|
<div> Limits: \(1 \leq N \leq 10^5\) </div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div><div>A code template with an example program: </div></div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div style="border:2px solid black">
|
|
|
|
|
<pre>def primes(N):
|
|
|
|
|
# TODO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
print(primes(7)) # 4
|
|
|
|
|
print(primes(15)) # 6
|
|
|
|
|
print(primes(50)) # 15
|
|
|
|
|
</pre>
|
|
|
|
|
</div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> Submit your solution in CodeGrade as <strong>primes.py</strong>.</div>
|
|
|
|
|
<br>
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<h4><strong> Assignment 1.3: Is it a Triangle?</strong> (3 points)</h4>
|
|
|
|
|
</div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> Three integers \(a\), \(b\) and \(c\) presents the side lenghts of a triangle. Can you build any triangle from those three sides?</div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> For example:</div>
|
|
|
|
|
<ol>
|
|
|
|
|
<li> sides \(3\), \(4\) and \(5\) makes a right angle triangle </li>
|
|
|
|
|
<li> sides \(5\), \(5\) and \(3\) makes an isosceles triangle </li>
|
|
|
|
|
<li> sides \(7\), \(3\) and \(3\) doesn't make a triangle </li>
|
|
|
|
|
<li> sides \(4\), \(3\) and \(-1\) doesn't make a triangle </li>
|
|
|
|
|
</ol>
|
|
|
|
|
<div> Create the following function in Python:</div>
|
|
|
|
|
<ul>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><strong>triangle(a: int, b: int, c: int)</strong>: returns a boolean <strong>True</strong> if triangle can be built, <strong>False</strong> if not </li>
|
|
|
|
|
</ul>
|
|
|
|
|
</ul>
|
|
|
|
|
<div><div>A code template with an example program: </div></div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div style="border:2px solid black">
|
|
|
|
|
<pre>def triangle(a, b, c):
|
|
|
|
|
# TODO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
print(triangle(3, 5, 4)) # True
|
|
|
|
|
print(triangle(-1, 2, 3)) # False
|
|
|
|
|
print(triangle(5, 9, 14)) # False
|
|
|
|
|
print(triangle(30, 12, 29)) # True
|
|
|
|
|
</pre>
|
|
|
|
|
</div>
|
|
|
|
|
<p></p>
|
|
|
|
|
<div> Submit your solution in CodeGrade as <strong>triangle.py</strong>.</div>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|