Jan2020-English-Python
Jan2020-English-Python
Contents
Assignment A: Riemann series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Assignment B: Packing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Assignment E: Tokenization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Submission details
You must hand in your solution electronically:
2. You must upload all your solutions at DTU’s Online exam site. Each assignment must be uploaded
as one separate .py file, given the same name as the function in the assignment:
(a) riemann.py
(b) packing.py
(c) dice_sum_count.py
(d) imputed_sum.py
(e) tokenize.py
The files must be handed in separately (not as a zip-file) and must have these exact filenames.
After the exam, your solutions submitted to DTU Inside will be automatically evaluated on CodeJudge on
a range of different tests, to check that they work correctly in general. The assessment of your solution is
based only on how many of the automated tests it passes.
• Each solution shall not contain any additional code beyond the specified function, though import
statements can be included.
• Remember, you can check if your solutions follow the specifications by uploading them to CodeJudge.
• Note that all vectors and matrices used as input or output must be numpy arrays.
1 of 6
Assignment A Riemann series
where d e is the ceil function. We want to compute the sum after n terms from one of the series.
Problem definition
Create a function called riemann which takes as input: n that is the number of terms and the form specified
as either “a” or “b” corresponding to equations 1 or 2, respectively. The function should return the sum of
the series from the n number of terms, depending on the form.
Solution template
def riemann(n, form):
#insert your code
return s
Input
n Scalar with the number of terms (integer, larger than zero)
form String that is either “a” or “b”.
Output
s Scalar with the sum of the series (float)
Example
Consider the example with the number of terms n = 7 and the form = “a”
7
X (−1)i+1
sa = = 1 − 1 + 1/2 − 1/2 + 1/3 − 1/3 + 1/4 (3)
i=1
di/2e
= 0 + 0 + 0 + 0.25 = 0.25. (4)
2 of 6
Assignment B Packing
We want to test if a two-dimensional box-shaped object can fit in a two-dimensional box. The sizes of the
box and object are each specified with a two-dimensional vector. We want to know the amount of missing
space for each dimension, which is the difference between the object size and the box size. If there is enough
space in a dimension the function should return 0 for that dimension. The object can be rotated and only
the case with the best fit should be returned.
Problem definition
Create a function called packing which takes as input: A 2-dimensional vector with the size of the box and
a 2-dimensional vector with the size of the object. The function should return the missing fit computed as
the difference in each dimension. If there is enough space in a dimension then that dimension should be set
to zero. The object can be rotated and only the case with the smallest sum should be returned. The sum is
computed as the sum over the elements of the missing fits. If the two sums are equal, then the non-rotated
version is to be preferred.
Solution template
def packing(box, object):
# insert your code
return d
Input
box 2-dimensional vector with size specification of the box.
object 2-dimensional vector with size specification of the object.
Output
d 2-dimensional vector with missing fit.
Example
Regard the box, b = [2, 5], and the object, g = [4, 2.1]. When rotated, the size of the object becomes
g̃ = [2.1, 4]. The missing space is computed as d (for the non-rotated case) and d̃ (for the rotated case):
d0 = [2, 0] (8)
0
d̃ = [0.1, 0] (9)
The sums of the element of these two vectors are 2 + 0 = 2 for the non-rotated case and 0.1 + 0 = 0.1 for
the rotated case, i.e., the rotated case is the one that fits best and the associated missing fit is returned:
d = [0.1, 0] (10)
3 of 6
Assignment C Dice sum count
For a game of dice with one, two or three dice, we want to count how many times that the sum of the dice
is equal or below a certain threshold when counting all the possible combination of the dice.
Problem definition
Create a function called dice_sum_count that takes two inputs: The threshold and the number of dice. The
function should return the number of dice sum combinations that are equal or below a given threshold.
Solution template
def dice_sum_count(threshold, dice):
# insert your code
return count
Input
threshold Scalar with the threshold (float)
dice Scalar with the number of dice, either 1, 2 or 3 (integer)
Output
count Scalar with the number of sums equal or below the threshold (integer)
Example
Consider the case where the threshold is 4 and number of dice are 2. The 36 possible combination of dice
are:
[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 1], [2, 2], [2, 3], . . . , [3, 1], [3, 2], . . . , [6, 5], [6, 6] (11)
The sum of each of these games are
2, 3, 4, 5, 6, 7, 3, 4, 5, . . . 4, 5, . . . 11, 12 (12)
count = 6 (14)
C
4 of 6
Assignment D Imputated sum
For a series of numbers, we want to compute the sum. A problem with the series is that some of the numbers
are “missing” indicated with the number 999. The “missing” numbers need to be computed as the mean of
the immediate neighboring values.
Consider an example series of numbers x,
Problem definition
Create a function imputed_sum which takes a vector with numbers and where missing values are indicated
with the value 999. The missing values must be computed as the mean of the two immediate neighboring
values. If the missing value is in the very start of the series or the very end, then the missing value should
be set to the value of its immediate neighbor. The series may contain zero or more missing values, but
it can be assumed that no two missing values follow each other immediately and that there is always one
non-missing value.
Solution template
def imputed_sum(x):
#insert your code
return s
Input
x Vector with series of numbers, where missing values are indicated with 999.
Output
s Scalar with sum.
Example
For the example above we have
x = [17, −4.1, 3, 999, 7, 18, 999] (17)
The resulting sum should be returned as
s = 63.9 (18)
D
5 of 6
Assignment E Tokenization
Given a string representing a text from a Breton-like language, we want to find the count of individual
words. We want to handle the case with an apostroph (a single quotation mark) between “c” followed by
“h”, i.e., “c’h”. At the point with the apostroph between these two letters the word should not be split. For
other letter combinations with apostroph, the string should be split into separate words.
Problem definition
Create a function called tokenize which takes a string as input. The output of the function should return
the number of words in the string. The word separation is where there is a space or an apostroph, except
when the apostroph is between “c” followed by “h”. It can be assumed that all characters are either the
letters from a to z, the space character or the apostroph. It can also be assumed that there is never two
spaces or two apostrophes right after each other, that the apostroph is always between letters and that there
is always one or more words.
Solution template
def tokenize(text):
# insert your code
return words
Input
text String representing a text.
Output
words Skalar with the number of words in the text (integer).
Example
Consider the string
words = 8 (19)
E
6 of 6