string operations
string operations
-Maghizhvanban
-3122235002068
Class Diagram:
Question 1
1.Define a vector inheriting from standard list. Let the vector is restricted to having only integer
number in the list. Overload appropriate function and operator so that when any other type of
element is inserted the program raises an error.
2. Overload the operators ‘+’ & ‘–‘ to add and subtract two vectors respectively. Ex. V1 =[2,3,4], V2 =
[1], V+V2 =[3,3,4] V1-V2 = [1,3,4] {Odd Register numbered Students}
3. Overload the operators ‘+’ & ‘–‘ operator to result with the following definitions. Ex. V1 =[2,3,4],
V2 = [1,2], V+V2 =[1,2,3,4] – To merge two lists in ascending order without any duplicates. V-V2 =
[1,2,3] , where the new vector has items which are different in both the vectors. {Even Register
numbered Students}
4.Write a function called GetRatios(Vec1, Vec2) which reads the value of both the vectors in
appropriate index and generates the ratio vector Ratio[]. The Ratio[i] is calculated by Vec1[i]/Vec2[i].
Write the code for handling exceptions using try, except and raise in the following conditions:
(i) When there is a value ‘Zero’ at some index ‘x’ of Vec2, place ‘NaN’ in Ratio[x]
(iii) Create an user-defined exception object when the length of both the vectors are zero
CODE:
class v_0(Exception):
def __init__(self,err):
super.__init__(err)
class Vector(list):
def __init__(self,*value):
self.value = value
for _ in value:
if type(_) == int or type(_) == None:
pass
else:
raise Exception("Enter a integer value")
super().__init__(value)
def getratio(self,v1,v2):
if len(v1.value) != len(v2.value):
raise Exception('Enter a vector of same dimension')
if len(v1.value) == len(v2.value) == 0:
raise v_0("Both vector are zero")
tem = []
for i in v1:
for j in v2:
try:
tem.append(i/j)
break
except ZeroDivisionError:
tem.append(None)
v = Vector(*tem)
return v
Question2:
The constructor must assign the first name and last name of an employee. Define a function called
from_string() which gets a single string from the user, splits and assigns to the first and last name. For
example, if the string is ‘Seetha Raman’, the function should assign first name as Seetha and second
name as Raman and the function returns the object. Can you design from_string() as a class or
instance function? Justify your response. Demonstrate object creation by passing two strings as well
as one string.
@classmethod
Question 3:
Explain the concept of Overriding and Operator Overloading concepts with the following example:
**Operator Overloading** allows built-in operators (like `+`, `-`, etc.) to be redefined for
user-defined classes, giving them new behavior based on the class's context.
For example, a subclass `Dog` might override a method `speak()` from its parent class `Animal` to
provide a different implementation. Similarly, you can overload the `+` operator in a class to define
how two objects of that class should be added together.
Implement the classes Movie() and MovieList() as described in the above figure. Override the
appropriate functions so that the MovieList is generated based on the genre assigned in the instance
variable when first object is created. For example, if the genre is defined as thriller, the list accepts
only thriller movies. When two lists are given as input, the list with more number of movies are
returned.
CODE:
class MovieList:
def __init__(self, genre):
self.genre = genre
self.movies = []
def __repr__(self):
# Format the movie list as 'MovieList(genre): title1 (genre), title2 (genre)'
movie_strs = [str(movie) for movie in self.movies]
return f"MovieList({self.genre}): {', '.join(movie_strs)}"
OUTPUT: