2 Collections POO Testing
2 Collections POO Testing
Testing
3. Lists:
4. Tuples:
1
(b) Write a function that swaps the first and last elements of a tuple.
Example:
Input: (1,2,3,4,5)
Output: (5,2,3,4,1)
(c) Write a function that finds the maximum and minimum values in a tuple.
Example:
Input: (4, 7, 1, 9)
Output: (9,1)
(d) Write a function that converts a tuple of numbers into a single concatenated string.
Example:
Input: (1,2,3,4)
Output: "1234"
5. Dictionaries
(a) Write a function that merges two dictionaries, summing values of common keys.
Example: Input: {’a’:1, ’b’:2}, {’b’:3, ’c’:4}
Output: {’a’:1, ’b’:5, ’c’:4}
(b) Write a function that inverts a dictionary (keys become values and vice versa).
Example: Input: {’a’: 1, ’b’: 2}
Output: {1: ’a’, 2: ’b’}
(c) Write a function to find the most frequently occurring value in a dictionary.
Example: Input: {’a’: 3, ’b’: 2, ’c’: 3}
Output: 3
(d) Write a function that groups words by their first letter from a given list. The function should
return a dictionary where the keys are the first letters and the values are lists of words.
Example: Input: ["apple", "banana", "apricot", "blueberry", "cherry"]
Output: {"a": ["apple", "apricot"], "b": ["banana", "blueberry"], "c": ["cherry"]}
(e) Write a function that finds the key associated with the highest value in a dictionary.
Example: Input: {’a’: 10, ’b’: 25, ’c’: 17}
Output: ’b’
6. Sets:
(a) Write a function that returns the union, intersection, and difference of two sets.
Example:
Input: {1,2,3}, {3,4,5}
Output:
Union: {1,2,3,4,5}
Intersection: {3}
Difference (Set 1 - Set 2): {1,2}
Difference (Set 2 - Set 1): {4,5}
(b) Write a function that finds the symmetric difference between two sets.
Example:
Input: {1,2,3}, {2,3,4}
Output: {1,4}
(c) Write a function to check if two sets are disjoint.
Example:
Input: {1,2,3}, {4,5,6}
Output: True
2
Object-Oriented Programming (Chapter 3)
1. Define a class Rectangle in Python with:
• Attributes: length and width.
• A method area() that returns the area of the rectangle.
• A method perimeter() that returns the perimeter of the rectangle.
2. Define a class Circle with an attribute radius. Include methods:
• area() that calculates the area.
• circumference() that calculates the circumference.
Example: Circle(5).area() returns approximately 78.54.
3. Define a class Student with attributes name, age, and grades. Include methods:
• average() returning the average of grades.
• is passing() returning True if the average grade is above a threshold (e.g., 60).
Example: Student("Alice",20,[80,90]).average() returns 85.
4. Define a class BankAccount with attributes balance and methods deposit() and withdraw().
Example: After depositing 50 into an account initialized with 100, the balance is 150.
5. Implement inheritance by defining a superclass Vehicle with attributes make and model. Create
subclasses Car and Bike with additional attributes doors for Car and type for Bike.
Example: Car("Ford","Mustang",4) creates a car object.
Testing (Chapter 4)
1. Write a Python function is positive(n) that returns True if n is positive and False otherwise.
Use an assert statement to test your function.
Example: Input: 5, Output: True; Input: -3, Output: False
2. Write unit tests for the Rectangle class using Python’s unittest framework.
3. Explain the concept of Test-Driven Development (TDD) and illustrate it by writing tests first for
a simple function that calculates the factorial of a number.
4. Write tests that specifically check edge cases, incorrect usage, and error handling for a function
that divides two numbers.
5. Explain why tests should be maintained even after they pass successfully.
3
def m y s t e r y f u n c t i o n ( n ) :
total = 0
f o r i in range ( n ) :
f o r j in range ( i ) :
t o t a l += j
return t o t a l