103 lines
3.2 KiB
HTML
103 lines
3.2 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<title>Week 8 Programming Assignments (8 points)</title>
|
|
<meta charset="utf-8">
|
|
|
|
</head>
|
|
<body>
|
|
<h4><strong>Assignment 8.1: Jumps</strong> (4 points)</h4>
|
|
<div>You are playing a game which has \(n\) levels. You start from the level \(0\) and your goal is to reach the level
|
|
\(n\) by jumping from a level to another. From every level you're able to jump only to \(a\) or \(b\)
|
|
levels higher. In how many different ways can you complete the game?
|
|
</div>
|
|
<br>
|
|
<div>e.g.: let \(n=8\), \(a=2\) and \(b=3\), there are \(4\) different ways to pass the game.</div>
|
|
<ul>
|
|
<ul>
|
|
<li>\(0\rightarrow 2\rightarrow 4\rightarrow 6\rightarrow 8\)</li>
|
|
<li>\(0\rightarrow 2\rightarrow 5\rightarrow 8\)</li>
|
|
<li>\(0\rightarrow 3\rightarrow 5\rightarrow 8\)</li>
|
|
<li>\(0\rightarrow 3\rightarrow 6\rightarrow 8\)</li>
|
|
</ul>
|
|
</ul>
|
|
|
|
<div>Create a function <strong>jumps(n: int, a: int, b: int)</strong> in Python which returns the number of all possible ways to complete the game.</div>
|
|
<br>
|
|
|
|
<div>Limist (you can assume that):</div>
|
|
<ul>
|
|
<ul>
|
|
<li>\(n \leq 10000\)</li>
|
|
<li>\(1 \leq a < b \leq n\)</li>
|
|
</ul>
|
|
</ul>
|
|
|
|
<div>Targets:</div>
|
|
<ol>
|
|
<li>correct solution: \(2\) points</li>
|
|
<li>performs in \(\Theta (n)\) time: \(+2\) points (the solution must be correct)</li>
|
|
</ol>
|
|
|
|
<div>A code template with an example program:</div>
|
|
<br>
|
|
<div style="border: 2px solid black">
|
|
<pre>def jumps(n, a, b):
|
|
# TODO
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(jumps(4, 1, 2)) # 5
|
|
print(jumps(8, 2, 3)) # 4
|
|
print(jumps(11, 6, 7)) # 0
|
|
print(jumps(30, 3, 5)) # 58
|
|
print(jumps(100, 4, 5)) # 1167937
|
|
</pre>
|
|
</div>
|
|
<br>
|
|
<div>Submit your solution in CodeGrade as <strong>jumps.py</strong>.</div>
|
|
<br><br>
|
|
<br>
|
|
|
|
<div>
|
|
<h4><strong>Assignment 8.2: All Sums </strong>(4 points)</h4>
|
|
</div>
|
|
<div>\(A\) is a list consisting of \(n\) integers. How many different sums can be generated with the given integers?</div>
|
|
<br>
|
|
<div>For example:</div>
|
|
<ul>
|
|
<ul>
|
|
<li>list \([1, 2, 3]\) has 6 possible sums: \(1\), \(2\), \(3\), \(4\), \(5\) and \(6\)</li>
|
|
<li>list \([2, 2, 3]\) has 5 possible sums: \(2\), \(3\), \(4\), \(5\) and \(7\)</li>
|
|
</ul>
|
|
</ul>
|
|
<div>Create a function <strong>sums(A: list)</strong> in Python which computes the number of all different sums.</div>
|
|
<br>
|
|
<div>Limits (you can assume that): \(1 \leq n, a_i \leq 100\)</div>
|
|
<br>
|
|
<div>Target: The algorithm computes the awnser in less than \(1\) second (in CodeGrade)</div>
|
|
<ol>
|
|
<li>correct solution: \(2\) points</li>
|
|
<li>performs in \(\Theta (n^3)\) time: \(+2\) points (the solution be must correct)</li>
|
|
<ul>
|
|
<li>(extra: consider the running time of your algorithm)</li>
|
|
</ul>
|
|
</ol>
|
|
<div>A code template with an example program:</div>
|
|
<br>
|
|
<div style="border: 2px solid black">
|
|
<pre>def sums(items):
|
|
# TODO
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(sums([1, 2, 3])) # 6
|
|
print(sums([2, 2, 3])) # 5
|
|
print(sums([1, 3, 5, 1, 3, 5])) # 18
|
|
print(sums([1, 15, 5, 23, 100, 55, 2])) # 121
|
|
</pre>
|
|
</div>
|
|
<br>
|
|
<div>Submit your solution in CodeGrade as <strong>sums.py</strong>.</div>
|
|
</body>
|
|
</html> |