$24.99
1. Write a Python class to find the three elements that sum to zero from a set of n real numbers. Input array : [-25, -10, -7, -3, 2, 4, 8, 10]
Output : [[-10, 2, 8], [-7, -3, 10]]
2. Write a Python class to find validity of a string of parentheses, '(', ')', '{', '}', '[' and ']. These brackets must be close in the correct order, for example "()" and "()[]{}" are valid but "[)", "({[)]" and "{{{" are invalid.
3. Write a Python class named Circle constructed by a radius and two methods which will compute the area and the perimeter of a circle.
4. Write a new Python numeric style class called Distance. We want to add two distances together, subtract one distance from another, divide a distance by an integer, multiply a distance by an integer etc.
We should therefore be able to support the following program:
d1 = Distance(6) d2 = Distance(3)
print( d1 + d2) print (d1 - d2) print (d1 / 2) print(d2 // 2) print(d2 * 2)
To model such operations, we will have __add__, __sub__, __truediv__, __floordiv__, __mul__ methods in the class definition respectively.
5. Write the following program with classes: Given a deck of cards, the task is to distribute them between two players.
Approach:
- To shuffle the deck of cards we need to use the shuffle module. Import the required module - Declare a class named Cards which will have variables suites and values, now instead of using self.suites and self.values, we are going to declare them as global variables.
- Declare a class Deck which will have an empty list named as mycardset, and the suites and values will be appended to mycardset list.
- Declare a class ShuffleCards along with a method named shuffle() that would check the number of cards and then shuffles them.
- To remove some cards we will create a popCard() method in ShuffleCards class.
6. Write a program to convert a dictionary into a class.
Approach:
Let’s take a simple dictionary “my_dict” which has the “Name”, “Rank”, and “Subject” as keys and they have the corresponding values as “Geeks”, “1223”, “Python”. We are calling a function here Dict2Class which takes our dictionary as an input and converts it to class. We then loop over our dictionary by using setattr() function to add each of the keys as attributes to the class.
setattr() is used to assign the object attribute its value. Apart from ways to assign values to class variables, through constructors and object functions, this method gives you an alternative way to assign value.
Syntax : setattr(obj, var, val) Parameters :
Returns :
None