# PRG - Exam #
02. 02. 2024
This is the assignment for the PRG exam. Follow the instructions precisely and please note:
- If the module executes any functions when imported (e.g. print), points will be subtracted.
- If the module fails to be imported (due to syntactic errors), the solution is invalid.
The modules (packages) you implement should be packed in a zip file (files should be in the root of
the zip file, zipping directly the files, not the containing folder) and uploaded to BRUTE. For
every module, class and function you implement, do not forget to add a docstring. Code quality
(mainly following naming conventions and writing docstrings) is part of the grading of your
solution.
## Task 1 [3p] ##
Create a module named "traceback_code" which will produce the following traceback when executed.
Line numbers are allowed to differ between the showed traceback and the traceback produced by your code.
```
Traceback (most recent call last):
File "traceback_code.py", line 13, in
square_root_list(number_list)
File "traceback_code.py", line 7, in square_root_list
return_list.append(math.sqrt(number))
ValueError: math domain error
```
## Task 2 [3p] ##
Your task is to write a GENERATOR function in Python named lin_space that generates a sequence of n linearly spaced
numbers between two specified numbers x1 and x2, inclusive. These linearly spaced numbers represent a sequence of
evenly spaced points between x1 and x2. [2p]
The function lin_space must accept three parameters:
x1 (the starting number of the sequence),
x2 (the ending number of the sequence),
and an optional parameter n.
The parameter n determines how many numbers to generate in the sequence. If n is not provided when the function is
called, the function should default to generating 100 evenly spaced points.
The function should YIELD each number in this sequence of linearly spaced numbers.
After defining the function, demonstrate its functionality by testing it with a for loop. [1p]
An example of this in action would be: for x1 = 1, x2 = 6, and n = 6, the generated sequence should be: 1, 2, 3, 4, 5, 6.
Your solution should be placed in a module named sequence_generator.
## Task 3 [9p] ##
Create a module named "task3_functions" and implement three functions according to the following
unit tests. You can put the unit tests (from below) to the same module.
Note that the use of functions such as eval(), exec() and similar is not allowed.
```
import unittest
class TestFunctions(unittest.TestCase):
# [3p]
def test_equations(self):
self.assertTrue(integer_equation("5 = 2 + 3"))
self.assertFalse(integer_equation("-2 = 2 + 3"))
self.assertTrue(integer_equation("-1 = 2 - 3"))
self.assertFalse(integer_equation("0 = 12 - 11"))
self.assertTrue(integer_equation("132 = 12 * 11"))
self.assertFalse(integer_equation("-20 = 5 * 3"))
self.assertTrue(integer_equation("5 = 20 / 4"))
self.assertFalse(integer_equation("9 = 16 / 2"))
# [3p]
def test_monotonic(self):
self.assertEqual(is_monotonic([1, 2, 3, 4, 5]), "increasing")
self.assertEqual(is_monotonic([-50, 0, 2, 8, 1000]), "increasing")
self.assertEqual(is_monotonic([5, 4, 3, 2, 1]), "decreasing")
self.assertEqual(is_monotonic([80, 65, 34, 19, 1]), "decreasing")
self.assertEqual(is_monotonic([80, 65, 0, 19, 1]), "non-monotonic")
self.assertEqual(is_monotonic([1, -1, 1, -1, 1]), "non-monotonic")
self.assertEqual(is_monotonic([1, 2, 3, 4, 5, 6, 5]), "non-monotonic")
# [3p]
def test_parentheses_extraction(self):
self.assertEqual(extract_parentheses_content("Text with (one set) of parentheses"), ["one set"])
self.assertEqual(extract_parentheses_content("Example with (first) and (second) sets"), ["first", "second"])
self.assertEqual(extract_parentheses_content("(Just one) at the beginning"), ["Just one"])
self.assertEqual(extract_parentheses_content("At the end is (a set)"), ["a set"])
self.assertEqual(extract_parentheses_content("Two sets (together)()"), ["together", ""])
self.assertEqual(extract_parentheses_content("No parentheses at all"), [])
self.assertEqual(extract_parentheses_content("Empty () here"), [""])
self.assertEqual(extract_parentheses_content("(Multiple words) inside one"), ["Multiple words"])
self.assertEqual(extract_parentheses_content("Numbers (1234) and (5678) included"), ["1234", "5678"])
self.assertEqual(extract_parentheses_content("Special characters (@#$%) inside"), ["@#$%"])
```
## Task 4 [10p] ##
Create a package with a single module containing the class definition below.
1. [1p] Choose the name of the package and module appropriately. Be sure to follow best practices.
```
class Cmplx:
"""
Represents a complex number.
This class stores the real and imaginary parts of a complex number. The 're' attribute represents the real part,
and the 'im' attribute represents the imaginary part of the complex number.
"""
def __init__(self, re, im):
self.re = re
self.im = im
```
In this module, implement the following tasks:
2. [1p] Implement a method within the Cmplx class that provides a user-friendly string representation of a Cmplx object.
3. [1p] Create a Python class CmplxVector to represent a vector of complex numbers.
It should include:
- Attributes:
* vec (list): A list of Cmplx objects.
- Functionality:
Constructor should initialize the complex vector with a list of Cmplx objects.
4. [1p] Implement a method within CmplxVector that provides a user-friendly string representation of a vector of complex
numbers, incorporating the functionality from step 2.
5. [2p] Overload the == operator in the CmplxVector class to enable comparison between two CmplxVecotr objects in a
meaningful way. If the vectors are not of the same size, this operation should return False.
6. [1p] In the CmplxVector class, implement a conjugate() method that creates and returns a new CmplxVector instance,
where each Cmplx object in the vector has its imaginary part's sign inverted from the original.
7. [1p] Write function "save_vector" which takes two arguments - a CmplxVector instance and a file
path, and will store the data from the instance. The structure of the file is up to you.
8. [2p] Write function "load_vector" which takes a single argument - a file path, and returns
a CmplxVector instance containing the data previously saved by save_vector.