December2016 - Python
December2016 - Python
Contents
Assignment A: CVR Number Checksum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Assignment C: Capitalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Assignment D: Submatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Assignment E: Approximating Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Submission details
You must hand in your solution electronically:
2. You must upload your solutions on CampusNet. Each assignment must be uploaded as one separate
.py file, given the same name as the function in the assignment:
(a) cvrchecksum.py
(b) nextleapyear.py
(c) capitalize.py
(d) submatrix.py
(e) approximatepi.py
The files must be handed in separately (not as a zip-file) and must have these exact filenames.
After the exam, your solutions will be automatically evaluated on CodeJudge on a range of different tests,
to check that they work correctly in general. The assessment of you 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.
• 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 CVR Number Checksum
It is mandatory for Danish companies to register for a 8 digit identification number called CVR number (Det
Centrale Virksomhedsregister). The last digit is a check-sum which is computed according to the so-called
modulus 11 rule with the weights 2-7-6-5-4-3-2. The checksum is computed in the following way:
• The seven first digits are multiplied elementwise with the weights 2-7-6-5-4-3-2.
• The modulus (remainder after integer division) of this sum and 11 is determined.
Problem definition
Create a function named cvrchecksum that takes as input the seven first digits of a CVR number and
computes the correct checksum.
Solution template
def cvrchecksum(cvr):
#insert your code
return checksum
Input
cvr 7-digit integer for which the checksum is calculated.
Output
checksum An integer (0 − 9) which is the checksum for cvr.
Example
We want to calculate the checksum, x, for CVR number 3006094x. We compute the checksum in the
following way:
71 mod 11 = 5
2 of 6
Assignment B Next Leap Year
In the common Gregorian calendar a year has on average 365.2425 days. Most years have 365 days, with
February having 28 days.
Occasionally, a leap year occurs, in which February has 29 days. Leap years are determined by the
following rules:
• However: if the year is divisable by 100 the year is not a leap year, unless it is divisable by 400.
Problem definition
Create a function named nextleapyear that takes as input a year, and returns the first following leap year.
Solution template
def nextleapyear(y):
#insert your code
return l
Input
y Positive integer number, which represents the year.
Output
l Positive integer, which represents the first following leap year. Note that always: l > y.
Example
With year 1896 as the input, the next leap year would be 1904. While 1900 is divisible by 4, it is also
divisible by 100, and not by 400.
Other examples:
3 of 6
Assignment C Capitalization
A common grammatical error in writing is the lack of capitalization (upper case first letters). Many instances
of this mistanke can be automatically corrected by applying the following rules:
• The first word after the punctuation marks period (.), exclamation mark (!) and question mark (?)
must be capitalized.
Problem definition
Create a function named capitalize that takes as input a string and returns the string with the capital-
ization rules applied. Capital letters in the input string must be kept even if they voilate the capitalization
rule and you can assume no more than one subsequent space. The letters that need to be handled are:
Letters abcdefghijklmnopqrstuvwxyz
Capitals ABCDEFGHIJKLMNOPQRSTUVWXYZ
Solution template
def capitalize(strin):
#insert your code
return strout
Input
strin Input string which might contain capitalization mistakes.
Output
strout The same string with capitalization rules applied.
Example
strin hello! how are you? please remember capitalization. EVERY time.
strout Hello! How are you? Please remember capitalization. EVERY time.
4 of 6
Assignment D Submatrix
A submatrix is obtained from a matrix by deleting a number of rows and/or colums. The remaining entries
are collected in their original relative positions to form the submatrix.
Problem definition
Create a function named submatrix that takes as input a matrix and a row and column index, and returns
the submatrix formed by removing the row and column.
Solution template
def submatrix(M, r, c):
#insert your code
return S
Input
M Input matrix of size m × n.
r Row index. If 1 ≤ r ≤ m, the rth row shall be removed. Otherwise, no row is removed.
c Column index. If 1 ≤ c ≤ n, the cth column shall be removed. Otherwise, no column is removed.
Output
S Submatrix of M after removing row r and column c.
Example
In the example shown below, the third row and the second colum are removed from a 3 × 4 matrix to form
a 2 × 3 submatrix:
1 2 3 4 " #
1 3 4
5 6 7 8→
5 7 8
9 10 11 12
Other examples:
1 0 0 0
1 2 3 4 1 2 3 4 0 1 2 3 4
1 0 0
M 5 6 7 8 5 6 7 8 5 6 7 8
0 0 1 0
9 10 11 12 9 10 11 12 9 10 11 12
0 0 0 1
r 2 1 4 5
c 1 0 2 1
" # " # 1 0 0 2 3 4
2 3 4 5 6 7 8
S 0 0 0 6 7 8
10 11 12 9 10 11 12
0 1 0 10 11 12
5 of 6
Assignment E Approximating Pi
0 0 0 0
0 0.5 1 0 0.5 1 0 0.5 1 0 0.5 1
val = 3 val = 3.25 val = 3.1523 val = 3.1414
Problem definition
Create a function named approximatepi that takes as input the positive integer number N , which is
the number of points to use in the pi approximation along one dimension. The function shall return the
approximation of π given this value of N computed as:
K
·4
N2
Where K is the number of points with distance less than one to the origin. The point coordinates are given
by:
xi,j 1
2N + Nj
= , i ∈ 0, 1, 2, ..., N − 1, j ∈ 0, 1, 2, ..., N − 1
yi,j 1 i
2N + N
Solution template
def approximatepi(N):
#insert your code
return val
Input
N Integer number in the range [1; 10000].
Output
val The approximated value of π.
Example
If we choose N = 2, a grid of N 2 = 4 points is formed. The point coordinates are:
6 of 6