0% found this document useful (0 votes)
18 views3 pages

Python Exam: OOP, Iterators, Algorithms

The document outlines a programming exam consisting of three exercises focused on Python concepts. Exercise 1 involves creating a BankAccount class with encapsulation and operator overloading, Exercise 2 requires implementing a PrimeGenerator iterator and a Fibonacci primes generator, and Exercise 3 tasks students with implementing Dijkstra's algorithm using advanced Python features. The exam emphasizes code style, documentation, and error handling, and is designed to be completed in 2 hours.

Uploaded by

horchani.wissem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Python Exam: OOP, Iterators, Algorithms

The document outlines a programming exam consisting of three exercises focused on Python concepts. Exercise 1 involves creating a BankAccount class with encapsulation and operator overloading, Exercise 2 requires implementing a PrimeGenerator iterator and a Fibonacci primes generator, and Exercise 3 tasks students with implementing Dijkstra's algorithm using advanced Python features. The exam emphasizes code style, documentation, and error handling, and is designed to be completed in 2 hours.

Uploaded by

horchani.wissem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Exercise 1: Object-Oriented Programming with Properties

and Magic Methods (30-40 minutes)


Create a BankAccount class that demonstrates proper encapsulation and operator
overloading:

Requirements:

● Private balance attribute that cannot be directly accessed


● Property decorators for safe balance access with validation
● Implement str , repr , and eq magic methods
● Overload + and - operators for deposits and withdrawals
● Context manager protocol ( enter and exit ) for transaction
logging
● Custom exception handling for insufficient funds

Example usage your class should support:

python
with BankAccount("Alice", 1000) as account:
account += 500 # deposit
account -= 200 # withdrawal
print([Link]) # Should print 1300

Exercise 2: Advanced Iterators and Generators (30-40


minutes)
Implement a custom iterator class PrimeGenerator and corresponding functions:

Part A: Create an iterator that generates prime numbers up to a given limit

● Implement iter and next methods


● Include proper StopIteration handling
● Add a reset method to restart iteration

Part B: Write a generator function fibonacci_primes(n) that yields the first n


numbers that are both Fibonacci numbers and prime numbers

Part C: Create a decorator @memoize that caches function results and apply it to
optimize a recursive function
Bonus: Make your iterator work with itertools functions like islice and
takewhile

Exercise 3: Graph Algorithm with Advanced Python


Features (50-60 minutes)
Implement Dijkstra's shortest path algorithm using Python's advanced features:

Requirements:

● Use [Link] and heapq module appropriately


● Implement the algorithm as a class method within a Graph class
● Use type hints throughout your implementation
● Include comprehensive docstrings following Google/NumPy style
● Implement path reconstruction to return actual shortest paths, not just
distances
● Add data validation using @property setters
● Handle edge cases (disconnected graphs, negative weights, self-loops)

Additional features to implement:

● A @classmethod constructor that builds a graph from an adjacency list


● Method chaining for graph operations
● Custom string representation showing graph statistics
● Unit tests using assertions to verify your algorithm works correctly

The class should support usage like:

python
g=
Graph.from_adjacency_list(
{ 'A': [('B', 4), ('C', 2)],
'B': [('C', 1), ('D', 5)],
'C': [('D', 8), ('E', 10)],
'D': [('E', 2)]
})

distance, path = [Link]('A', 'E')


print(f"Shortest distance: {distance}, Path: {' -> '.join(path)}")

Grading Distribution:

● Exercise 1: 25 points (Focus on OOP concepts and Python magic methods)


● Exercise 2: 25 points (Iterator protocol and generator functions)
● Exercise 3: 50 points (Algorithm implementation + advanced Python features)
Tips for students:

● Read all exercises first and allocate time accordingly


● Exercise 3 is the most complex - start with a basic implementation then add
features
● Code style, documentation, and error handling matter for full credit
● Test your code with the provided examples

This exam tests core Python concepts (OOP, iterators, decorators), advanced
language features (magic methods, context managers, type hints), and algorithmic
thinking while remaining challenging but achievable in 2 hours.

You might also like