1AKIFT POS Test (Group B)

2024-01-31

Max. 100 points

Name:

Task Max. Achieved
1 20
2 40
3 40
Sum 100
Grading: > 87.5: 1, >75: 2, >62.5 : 3, >50: 4, <=50: 5
  1. (20 points)
    Implement a function reverse(s: str) -> str that reverses a given string. To receive full points, make sure that the function has a proper docstring and a doctest. For example: reverse("stressed") -> "desserts"
    reverse("strops") -> "sports"
    reverse("racecar") -> "racecar"
    5 points for the function signature
    5 points for the docstring
    5 points for the docstest
    5 points for the implementation
    def reverse(text: str) -> str:
        """
        Return a reversed version of the given string.
    
        >>> reverse("stressed")
        "desserts"
        """
        return text[::-1]
  2. (40 points)
    Implement rot13(text) assuming that text only contains upper case characters. Use the ord(char)->int and chr(i:int)->str functions in your implementation. Add a proper docstring in order to receive full points.
    5 points for correct function signature
    5 points for proper docstring
    5 points for result list
    5 points for for loop
    10 points for correct conditions
    5 points for joining the list
    5 points for returning the correct result
    def rot13(text):
        """Return the input string after returning the characters by 13 places."""
        result = []
        for char in text:
            if 'A' <= char = 'M':
                num = ord(char) + 13
            elif 'N' <= char <= 'Z':
                num = ord(char) - 13
            else:
                num = ord(char)
            result.append(chr(num))
        return "".join(result)
  3. (40 points)
    Write a function sumproduct(first iterable, second iterable) in Python that takes two sequences of numbers with equal length and returns the sum of the product of the numbers.
    [e.g. sumproduct((1, 3, 4), (9, 2, 3)) ⇒ 1*9 + 3*2 + 4*3 = 27; sumproduct([3, 5], [2, 4]) ⇒ 26]. Add a proper docstring and a doctest to receive full points.
    Grading: 1 point for correct function signature, 1 point for proper docstring, 1 point for a doctest, 2 for returning the correct result. (6 points)
    5 points for the function signature
    5 points for the docstring
    10 points for the doctest
    15 points for the correct calculation of the sumproduct
    5 points for returning the sum
    def sumproduct(first, second):
        """
        Return the sum of the products of `zip(first, second)`.
    
        >>> sumproduct((1, 3, 4), (9, 2, 3))
        27
        """
        sum = 0
        for i, j in zip(first, second):
            sum += i * j
        return sum