1AKIFT POS Test (Group A)

2024-01-17

Max. 100 points

Name:

Task Max. Achieved
1 50
2 50
Sum 100
Grading: > 87.5: 1, >75: 2, >62.5 : 3, >50: 4, <=50: 5
  1. 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.
    5 points for the function signature
    5 points for the docstring
    10 points for returning correct result for empty lists
    5 points for correctly dealing with the first element
    10 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
  2. 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.
    5 points for the function signature
    5 points for the docstring
    20 points for a working representation of the translations
    10 points for producing the correctly translated data
    5 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])