# CMS Test (Group A)

2024-01-22

Max. 100 points

Name:

1 12
2 24
3 36
4 28
Sum 100
Grading: > 87.5: 1, >75: 2, >62.5 : 3, >50: 4, <=50: 5
1. Answer the following statements indicating whether they are True or False.
0-3 correct: 0 points, 4 correct: 4 points, 5 correct: 8 points, 6 correct: 12 points.
Betriebssyteme
Statement True False
Python is so successful because of its fast execution performance.
Inheritance and polymorphism are concepts of functional programming
Composition and aggregation are concepts of object oriented programming
Finding an element in a sequence is computationally more complex than sorting the sequence.
Python packages can be downloaded and installed using `pip`.
Pandas is a library that focuses on data analysis.
2. What is the output of the following code snippets? Write exactly what the output of each snippet is if the snippet is the sole content of a Python file. If the output is an error message, it is enough to write “ERROR”. If there is no visible output, write "-".
4 points for each correct answer.
1. ``````def sum(a, b):
return b + c
print(sum(4, 2))``````
ERROR
2. ``````def area(length):
return length ** 2
print(area(5))``````
25
3. ``````s = 'Hello World!'
s.lower()
print(s)``````
Hello World!
4. ``````class S:
def __init__(self):
self.__i = 0
s = S()
print(s.i)``````
ERROR
5. ``````def fun(a):
if a == 1:
return 1
return a + fun(a - 1)
print(fun(3))``````
6
6. ``````def print_together(a, b):
print(a, b)
print_together(b='XMAS!', a='Merry')``````
Merry XMAS!
3. Write a function `cumsum(l: List[int]) -> List[int]` in Python that takes a list of integers and returns a list containing the cumulative sum of the numbers. For example
`cumsum([9, 2, 3]) -> [9, 9+2, 9+2+3] -> [9, 11, 14]`
`cumsum([3, 5, -4, 2]) -> [3, 8, 4, 6]]`
Don't forget to deal with empty lists. Add a proper docstring to receive full points.
3 points for the function signature
3 points for the docstring
5 points for returning correct result for empty lists
5 points for correctly dealing with the first element
5 points for correct loop
10 points for correctly adding all subsequent elements
5 points for returning the correct result
``````def cumsum(l: List[int]) -> List[int]:
"""Return the cumulative sum of the given list."""
if not l: return []
r = l[:1]
for e in l[1:]:
r.append(r[-1] + e)
return r``````
4. Your task is to implement a function `to_rna(dna_strand: str) -> str` that determines the RNA complement of a given DNA sequence. Both DNA and RNA strands are a sequence of nucleotides. The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T). The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U). Given a DNA strand as a string (only consisting of upper case characters 'G', 'C', 'T' and 'A'), return its transcribed RNA strand as string. This is done by replacing each nucleotide with its complement:
```    G -> C
C -> G
T -> A
A -> U
```
For example, `to_rna('GCGGATA') -> 'CGCCUAU'`. Add a proper docstring to receive full points.
2 points for the function signature
2 points for the docstring
5 points for a working representation of the translations
10 points for producing the correctly translated data
4 points for the result being a string (not eg a list)
5 points for returning the correct result
``````def to_rna(dna_strand: str) -> str:
"""Return RNA strand for given DNA strand."""
l = []
for c in dna_strand:
if c == 'G':
l.append('C')
elif c == 'C':
l.append('G')
elif c == 'T':
l.append('A')
elif c == 'A':
l.append('U')
return ''.join(l)

# one of many alternatives that are more elegant:
def to_rna(dna: str) -> str:
"""Return RNA strand for given DNA strand."""
translations = {'G': 'C', 'C': 'G', 'T': 'A', 'A': 'U'}
return ''.join([translations[n] for n in dna])``````