# Excercises for list comprehensions # Alexander Kosik ######################### ### Excercise 1 ######################### def only_positive(list_with_numbers): """ Return a list using a list comprehension with only positive numbers from the input list Args: list_with_numbers: Must be a list. E.g. [12, -10, -3.5, 24.2, -4, 31] Returns: returns a list with only positive numbers. From the input above: it must return [12, 24.2, 31] """ pass # TODO ######################### ### Excercise 2 ######################### def print_public_methods(cls): """ This functions should return all public methods of a given class using a list comprehension. A public method is every method not starting wit an underscore `_` Hint: You can get all methods of the passed type with `dir(cls)` If stuck, checkout the `startswith` method of string type Args: cls is a class for which to check the methods Returns: returns a list using a list comprehension with all public list methods """ pass # TODO ######################### ### Excercise 3 ######################### def flatten(list_of_lists): """ Return a flattened list from the the input Args: list_of_list: Must be a list of list. E.g. [[1, 2, 3], [4, 5, 6]] Returns: Must return a flattened list. From the example above it must return [1, 2, 3, 4, 5, 6] """ pass # TODO ######################### ### Excercise 4 ######################### def is_prime(number): """ Return True if number is a prime number, else False Use a list comprehension to check if the remainder of a division is zero You can use the the builtin `all()` function to check if all values in a interable are True Args: Interger number Returns: True if number is a prime number E.g. is_prime(4) should return False, is_prime(17) should return True """ pass # TODO ######################### ### Excercise 5 ######################### def matrix_addition(matrix1, matrix2): """ Returns a new list of list where every corresponding number from matrix1 is added to the corresponding number from matrix 2. You can assume, that you only get valid input with fitting matrix sizes. e.g. matrix1 = [[1, 1], [2, 2]] matrix2 = [[4, 4], [5, 5]] result = [[5, 5], [7, 7]] Args: matrix1, matrix2: list of list with valid integer numbers. Sizes are fitting. Returns: Returns the addition of every number of matrix1 to the corresponding number of matrix2 Hints: - Try using it without comprehension at first, if you get stuck - the builtin `zip()` function can be handy for this exercise """ pass # TODO