749 Assignment 2
749 Assignment 2
• SU?t<1 PAUL
1
TER : IV
- - - - - - - -•1 •---=-- =- - - - - - - -
Ql) Write a code to display the following pattern
1
22
333
4444
55555
def calculate_length(input_string):
length= 0
for_ in input_string:
length+= 1
return length
def convert_case(input_string):
converted_string = 1111
for char in input_string:
if char.isupper():
converted_string += char.lower()
elif char.islower():
converted_string += char.upper()
else:
converted_string += char # Keep non-alphabetic characters unchanged
return converted_string
class BaseClass:
def greet(self):
print("Hello from BaseClass")
class DerivedClass(BaseClass):
def greet(self):
super().greet() # Calls the greet method from the BaseClass
print("Hello from DerivedClass")
class BaseClass2:
def greet(self):
print("Hello from BaseClass2")
class B(A):
def greet(self):
print("Hello from 8 11 )
class C(A):
def greet(self):
print("Hello from C")
# Creating an instance of D
obj= DO
def _str_(self):
return"({}, {})".format(self.x, self.y)
import math
class Quadratic:
def _ init_ (self, a, b, c):
self.a= a
self.b = b
self.c = c
def find_roots(self):
discriminant= self.b**2 - 4*self.a*self.c
if discriminant > 0:
rootl = (-self.b + math.sqrt(discriminant)) / (2*self.a)
root2 = (-self.b - math.sqrt(discriminant)) / (2*self.a)
print("Roots are real and distinct.")
print("Root l:", rootl)
print("Root 2:", root2)
elif discriminant== O:
root= -self.b / (2*self.a)
print("Roots are real and equal.")
print("Root:", root)
else:
real_part = -self.b / (2*self.a)
imaginary_part = math.sqrt(abs(discriminant)) / (2*self.a)
print("Roots are complex.")
print("Root l:", real_part, "+", imaginary_part, "i")
print("Root 2:", real_part, "-", imaginary_part, "i")
class Clock:
def _init_(self, hour, minute, second):
self.hour= hour
self.minute= minute
self.second = second
def display_time(self):
print("Time:", f"{self.hour:02d}:{self.minute:02d}:{self.second:02d}")
class Calendar:
def _init_(self, day, month, year):
self.day = day
self.month = month
self.year= year
def display_date(self):
print{"Date:", f"{self.day:02d}-{self.month:02d}-{self.year}")
def display_calendar_clock(self):
self.display_date()
self.display_time()
for i in range(max_len):
coeff_sum = 0
if i < len(self.coefficients):
coeff_sum+= self.coefficients[i)
if i < len(other.coefficients):
coeff_sum+= other.coefficients[i)
result_coefficients.append(coeff_sum)
def d isplay_polynomial(self):
terms=[)
for power, coeff in enumerate(self.coefficients[::-1)):
if coeff != 0 :
if power == 0:
term = str(coeff)
elif power == 1:
term = f "{coeff}x"
else:
term = f"{coeff}x"{power}"
terms.append(term)
polynomial_str =" + " .join(terms)
print("Polynomial:", polynomial_str)
# Define a list
my_list = [10, 20, 30, 40, 50]
# Create a d ictionary
my_dict = {'a' : 10, 'b': 20, 'c': 30, 'd ': 40}