2024-01-17
Max. 100 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 50 | |
2 | 50 | |
Sum | 100 |
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 examplecumsum([9, 2, 3]) -> [9, 9+2, 9+2+3] -> [9, 11, 14]
cumsum([3, 5, -4, 2]) -> [3, 8, 4, 6]]
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
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 -> UFor example,
to_rna('GCGGATA') -> 'CGCCUAU'
.
Add a proper docstring to receive full points.
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])