CMS Test (Group A)

2025-01-28

Max. 100 points

Name:

Task Max. Achieved
1 12
2 24
3 24
4 40
Sum 100
Grading: >= 88: 1, >=76: 2, >=64 : 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.
    Statement True False
    Python is a compiled language.
    Tuples are mutable.
    The == operator is used to compare values.
    Object oriented programming is a programming paradigm that uses classes.
    Python packages can be downloaded and installed using the numpy command.
    PuLP 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 my_function(n):
          return n % 2 == 1
      print(my_function(3))
      True
    2. def my_function(n):
          return [i**2 for i in range(n)]
      print(my_function(4))
      [0, 1, 4, 9]
    3. def my_function(my_list):
          return my_list.sortby(t for t in [str, int, int])
      print(my_function(["c", 3, 1]))
      Error
    4. def my_function(n):
          for i in range(n):
              if i / 5 == 1:
                  return i
      x = my_function(10)
      -
    5. class my_class():
          def __init__(self, x):
              self.x = x
      
          def my_method(self, y):
              return (self.x**2 + y**2)**(0.5)
      foo = my_class(3)
      print(foo.my_method(4))
      5
    6. def print_together(a, b):
          print(a, b)
      print_together(b='luck!', a='Good')
      Good Luck!
  3. 24 points
    Write a function count_non_zeros(lst) that takes a list of integers as argument. The function must return the number of non-zeros in the list. Use a list comprehension to solve this problem, and write a proper docstring including a doctest to get the full points.
    TODO: @Michael: up to you
    def cumsum(l: List[int]) -> List[int]:
        """
        Return the number of non-zeros in the list
    
        >>> count_non_zeros([0, 0]])
        0
        >>> count_non_zeros([0, 1])
        1
        """
        if not l: return []
        r = l[:1]
        for e in l[1:]:
            r.append(r[-1] + e)
        return r
  4. 40 points
    When doing electronics projects you have to work with resistors. Resistors are small. If you printed the resistance value on them, it would be hard to read. To get around this problem, color-coded bands are printed on the resistors to denote their resistance values.
    The following colors correspond to the values of the first two rings:
    black: 0, brown: 1, red: 2, orange: 3, yellow: 4, green: 5, blue: 6, violet: 7, grey: 8, white: 9
    To represent the number 33 on a resistor, two orange rings would be used. The third ring represents the order of magnitude of the resistance value. A black ring indicates that the result of the first two rings has to be multiplied by 1. A brown ring indicates multiplication by 10. Red indicates multiplication by 100 and so forth.
    Write a function generate_colors(resistance) that takes a desired resistance value and returns a list of colors to be printed. For example:
    >>> generate_colors(330)
    ['orange', 'orange', 'brown']
    >>> generate_colors(8200)
    ['grey', 'red', 'red']
    Use function annotations and a docstring including a doctest to receive full points.
    4 points for correct signature including type annotations
    4 points for a proper doctest
    8 points for representing the color value mapping
    4 points for creating a list to hold the resulting colors
    4 points for each correct color name
    4 points for appending the colors to the list
    4 points for returning the result
    values = [  # value corresponds to index of color
        "black", "brown", "red", "orange", "yellow", "green", "blue", "violet",
        "grey", "white"]
    
    def generate_colors(resistance: int) -> list[str]:
        """
        Generate the colors on a resistor given its resistance.
    
        >>> generate_colors(330)
        ['orange', 'orange', 'brown']
        """
        colors = []
        resistance = str(resistance)
        colors.append(values[int(resistance[0])])
        colors.append(values[int(resistance[1])])
        colors.append(values[len(resistance[2:])])
        return colors