Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Computer Lab 14, comprehensive exercises

  • 1. Implement the following function:

def abs_2(x):
    """Return an absolute value of x-2, i.e., y=|x-2|.
 
    :param x: int, float. 
    :return: int, float. The absolute value of x-2.
    >>> abs_2(0)
    2
    """

  • 2. Implement the following function (pure function!). You can re-use abs_2().

def abs_2_list(lst):
    """Return a list of absolute values of x-2, where x's are elements of lst.
 
    :param lst: list of ints/floats. 
    :return: list of ints/floats.
    >>> abs_2_list([0, 2, 4])
    [2, 0, 2]
    """

  • 3. Consider a file with the following content:

number_file.txt

1,2,3,4
90,23,78,0,23,23
9,8,7,-5,-12,0,12,12,12
0,-8
3,3,3,3
Implement a function which outputs a list of sums of numbers from each line of the file. Return None if the file does not exist.
def sum_file(file_name):
    """Return a list of row sums.
 
    :param file_name: Path to a text file. 
    :return: list of ints/floats.
    >>> sum_file("number_file.txt")
    [10, 237, 43, -8, 12]
    """

  • 4. Implement four methods (__init__, __str__, __eq__, __add__) for the following class according to their tests.

import unittest
 
class ColorRGB:
    """Represents a color in the R (red), G (green), B (blue) format."""
 
class TestColorRGB(unittest.TestCase):
    """Test ColorRGB methods"""
 
    def test_init(self):
        """Test that the initializer method __init__ takes 3 parameters"""
        ColorRGB(255, 0, 255)
 
    def test_str(self):
        """Test string conversion"""
        self.assertEqual("ColorRGB(0,127,127)", str(ColorRGB(0, 127, 127)))
 
    def test_eq(self):
        """Test deep equality for two ColorRGB instances"""
        self.assertEqual(ColorRGB(0,30,255), ColorRGB(0,30,255))
 
    def test_add(self):
        """Test the plus operator between two instances of the ColorRGB class"""
        color1 = ColorRGB(30, 100, 255)
        color2 = ColorRGB(0, 255, 30)
        self.assertEqual(ColorRGB(30, 255, 255), color1 + color2)
        self.assertEqual(ColorRGB(30, 100, 255), color1)
        self.assertEqual(ColorRGB(0, 255, 30), color2)
 
 
if __name__ == "__main__":
    # Execute all unit tests
    unittest.main()

  • 5. Implement the following function:

def random_integers(length, lower_bound, upper_bound):
    """Build a list of randomly chosen integers.
 
    :param length: Length of the resulting list
    :param lower_bound: Lower bound of each random integer (INCLUDED)
    :param upper_bound: Upper bound of each random integer (INCLUDED)
    :return: List of random integers
    """

  • 6. Create the following lists using list comprehension:

# 1.
fruits = ["apple", "banana", "pear", "kiwi", "strawberry", "lime", "lemon", "orange"]
# only words of even number of letters, transform them to uppercase
 
# 2.
# List of |x-2| where x's are all integers in the closed interval [-10, 10].

courses/be5b33prg/labs/week_14.txt · Last modified: 2021/01/06 13:44 by nemymila