Add new course
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
<!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>
|
||||
@@ -0,0 +1,24 @@
|
||||
def jumps(n, a, b):
|
||||
if n < 0:
|
||||
return 0
|
||||
if n == 0:
|
||||
return 1
|
||||
|
||||
# Initialize an array to store the number of ways to reach each position
|
||||
num_ways = [0] * (n + 1)
|
||||
num_ways[0] = 1
|
||||
|
||||
# Calculate the number of ways to reach each position
|
||||
for i in range(n + 1):
|
||||
num_ways[i] += num_ways[i - a]
|
||||
num_ways[i] += num_ways[i - b]
|
||||
|
||||
return num_ways[n]
|
||||
|
||||
|
||||
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
|
||||
@@ -0,0 +1,15 @@
|
||||
def sums(arr):
|
||||
sums = set()
|
||||
for i in range(len(arr)):
|
||||
for j in range(i, len(arr)):
|
||||
for k in range(j, len(arr)):
|
||||
sums.add(arr[i] + arr[j] + arr[k])
|
||||
return len(sums)
|
||||
|
||||
|
||||
# Test
|
||||
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
|
||||
@@ -0,0 +1,32 @@
|
||||
def sums(arr):
|
||||
# Create a set to store all the unique sums
|
||||
sums = set()
|
||||
|
||||
# Initialize a list with length equal to the length of arr
|
||||
# to store the previous results
|
||||
prev_results = [0] * len(arr)
|
||||
|
||||
# Iterate through each element in arr
|
||||
for i in range(len(arr)):
|
||||
# Initialize a temporary set to store the new sums
|
||||
temp = set()
|
||||
# Iterate through all the previous results
|
||||
for j in range(i):
|
||||
# Add the current element to each of the previous sums
|
||||
# and add the new sum to the set
|
||||
temp.add(arr[i] + prev_results[j])
|
||||
# Add the current element to the set
|
||||
temp.add(arr[i])
|
||||
# Update the list of previous results with the new sums
|
||||
prev_results[i] = temp
|
||||
# Update the set of all sums with the new sums
|
||||
sums = sums.union(temp)
|
||||
return len(sums)
|
||||
|
||||
|
||||
# Test
|
||||
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
|
||||
Reference in New Issue
Block a user