diff --git a/hackerrank.py b/hackerrank.py new file mode 100644 index 0000000..dc8ca42 --- /dev/null +++ b/hackerrank.py @@ -0,0 +1,90 @@ +# ## HackerRank Questions + +# * **Inheritance:** https://www.hackerrank.com/challenges/inheritance/problem + +class Person: + def __init__(self, firstName, lastName, idNumber): + self.firstName = firstName + self.lastName = lastName + self.idNumber = idNumber + def printPerson(self): + print("Name:", self.lastName + ",", self.firstName) + print("ID:", self.idNumber) + + +class Student(Person): + def __init__(self, firstName, lastName, idNumber,scores): + super().__init__(firstName, lastName, idNumber) + self.scores=sum(scores)/len(scores) + def calculate(self): + if 100>=self.scores>=90: + return 'O' + elif 80<=self.scores<90: + return 'E' + elif 70<=self.scores<80: + return 'A' + elif 55<=self.scores<70: + return 'P' + elif 40<=self.scores<55: + return 'D' + elif 40>self.scores: + return 'T' + +line = input().split() +firstName = line[0] +lastName = line[1] +idNum = line[2] +numScores = int(input()) # not needed for Python +scores = list( map(int, input().split()) ) +s = Student(firstName, lastName, idNum, scores) +s.printPerson() +print("Grade:", s.calculate()) + +# +# * **Classes: Dealing with Complex Numbers:** https://www.hackerrank.com/challenges/class-1-dealing-with-complex-numbers/problem + +import math + +class Complex(object): + def __init__(self, real, imaginary): + self.real=real + self.imaginary=imaginary + def __add__(self, no): + a = complex(self.real, self.imaginary) + b = complex(no.real, no.imaginary) + return Complex((a+b).real , (a+b).imag).__str__() + + def __sub__(self, no): + a = complex(self.real, self.imaginary) + b = complex(no.real, no.imaginary) + return Complex((a-b).real , (a-b).imag).__str__() + def __mul__(self, no): + a = complex(self.real, self.imaginary) + b = complex(no.real, no.imaginary) + return Complex((a*b).real , (a*b).imag).__str__() + def __truediv__(self, no): + a = complex(self.real, self.imaginary) + b = complex(no.real, no.imaginary) + return Complex((a/b).real , (a/b).imag).__str__() + def mod(self): + return Complex(abs(complex(self.real, self.imaginary)), 0) + def __str__(self): + if self.imaginary == 0: + result = "%.2f+0.00i" % (self.real) + elif self.real == 0: + if self.imaginary >= 0: + result = "0.00+%.2fi" % (self.imaginary) + else: + result = "0.00-%.2fi" % (abs(self.imaginary)) + elif self.imaginary > 0: + result = "%.2f+%.2fi" % (self.real, self.imaginary) + else: + result = "%.2f-%.2fi" % (self.real, abs(self.imaginary)) + return result + +if __name__ == '__main__': + c = map(float, input().split()) + d = map(float, input().split()) + x = Complex(*c) + y = Complex(*d) + print(*map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()]), sep='\n') \ No newline at end of file diff --git a/q1.py b/q1.py new file mode 100644 index 0000000..67b4bd7 --- /dev/null +++ b/q1.py @@ -0,0 +1,41 @@ +# Question 1: +# Create the class `Society` with following information: + +# `society_name`, `house_no`, `no_of_members`, `flat`, `income` + +# **Methods :** + +# * An `__init__` method to assign initial values of `society_name`, `house_no`, `no_of_members`, `income` +# * `allocate_flat()` to allocate flat according to income using the below table -> according to income, it will decide to flat type +# * `show_data()` to display the details of the entire class. +# * Create one object for each flat type, for each object call `allocate_flat()` and `show_data()` + +# | Income | Flat | +# | ------------- |:-------------:| +# | >=25000 | A Type | +# | >=20000 and <25000 | B Type | +# | >=15000 and <20000 | C Type | +# | <15000 | D Type | + +class Society: + def __init__(self,society_name,house_no,no_of_members,income): + self.society_name=society_name + self.house_no=house_no + self.no_of_members=no_of_members + self.income=income + def allocate_flat(self): + if self.income>=25000: + flat='A Type' + elif 25000>self.income>=20000: + flat='B Type' + elif 20000>self.income>=15000: + flat='C Type' + else: + flat='D Type' + return flat + def show_data(self): + print(f'Society name: {self.society_name} \nHouse no: {self.house_no}\nNo of Members: {self.no_of_members}\nIncome: {self.income}\nFlat:{self.allocate_flat()}') +society_name1=Society('Test',25,6,22500) +society_name1.show_data() +society_name2=Society('Test2',12,4,16000) +society_name2.show_data() \ No newline at end of file diff --git a/q2.py b/q2.py new file mode 100644 index 0000000..f838763 --- /dev/null +++ b/q2.py @@ -0,0 +1,42 @@ +# Question 2: +# Define a class named `ItemInfo` with the following description: + +# `item_code`(Item Code), `item`(item name), `price`(Price of each item), `qty`(quantity in stock), `discount`(Discount percentage on the item), `net_price`(Price after discount) + +# **Methods :** +# * A member method `calculate_discount()` to calculate discount as per the following rules: +# * If `qty <= 10` —> discount is `0` +# * If `qty (11 to 20 inclusive)` —> discount is `15` +# * If `qty >= 20` —> discount is `20` +# * A constructor init method to assign the initial values for `item_code` to `0` and `price`, `qty`, `net_price` and `discount` to `null` +# * A function called `buy()` to allow user to enter values for `item_code`, `item`, `price`, `qty`. Then call function `calculate_discount()` to calculate the `discount` and `net_price`(price * qty - discount). +# * A function `show_all()` or similar name to allow user to view the content of all the data members. + +class ItemInfo: + def __init__(self,item_code=0,item=None,price=None,qty=None,net_price=None,discount=None): + self.item=item + self.item_code=item_code + self.price=price + self.qty=qty + self.discount=discount + self.net_price=net_price + def calculate_discount(self): + if self.qty<=10: + self.discount=0 + elif 20>=self.qty>11: + self.discount=15 + else: + self.discount=20 + return self.discount + def buy(self): + self.item=input('Item: ') + self.item_code=input('Item Code: ') + self.price=float(input('Price: ')) + self.qty=int(input('Quantity in stock: ')) + self.calculate_discount() + self.net_price=self.price*((100-self.discount)/100) + def show_all(self): + print(f'Item code: {self.item_code} \nItem name: {self.item}\nPrice: {self.price}\nQuantity in Stock: {self.qty}\nDiscount:{self.calculate_discount()}\nNet Price:{self.net_price}') +pen = ItemInfo() +pen.buy() +pen.show_all() \ No newline at end of file diff --git a/q3.py b/q3.py new file mode 100644 index 0000000..ecd5e64 --- /dev/null +++ b/q3.py @@ -0,0 +1,39 @@ +# ## Question 3: + +# Create a class called `Numbers`, which has a single class attribute called `multiplier`, and a constructor which takes the parameters `x` and `y` (these should all be numbers). + +# * Write a method called `add` which returns the sum of the attributes `x` and `y`. +# * Write a class method called `multiply`, which takes a single number parameter `a` and returns the product of `a` and `multiplier`. +# * Write a method called `subtract`, which takes two number parameters, `b` and `c`, and returns `b - c`. +# * Write a method called `value` which returns a tuple containing the values of `x` and `y`. +# * Create a numbers object and call all the methods you wrote and print the results. + +class Numbers: + multiplier=1 + def __init__(self,x,y): + self.x=x + self.y=y + def add(self): + return self.x+self.y + @classmethod + def change_multiplier(cls,n_multiplier): + cls.multiplier=n_multiplier + return cls.multiplier + @classmethod + def multiply(cls,a): + return f'{cls.multiplier} * {a} = {cls.multiplier*a}' + @staticmethod + def subtract(b,c): + return b-c + def value(self): + return (self.x,self.y) +numbers=Numbers(5,10) +print(numbers.multiplier) +print(numbers.add()) +numbers.multiplier=7 +print(numbers.multiplier) +print(numbers.multiply(3)) #1*3 +print(numbers.change_multiplier(7)) +print(numbers.multiply(3)) +print(numbers.subtract(10,6)) +print(numbers.value()) diff --git a/q4.py b/q4.py new file mode 100644 index 0000000..212801c --- /dev/null +++ b/q4.py @@ -0,0 +1,26 @@ +# ## Question 4: + +# * Define the `Employee` class with an `__init__()` method +# * Define a class variable `new_id` and set it equal to `1` +# * Each Employee instance will need its own unique ID. Thus, inside `__init__()`, define `self.id` and set it equal to the class variable `new_id` +# * Lastly, increment `new_id` by `1` +# * Define a `say_id()` method +# * Inside `say_id()`, output the string `"My id is "` and then the instance id. +# * Define the variable e1 and set it to an instance of Employee +# * Define the variable e2 and set it to an instance of Employee +# * Have both e1 and e2 output their ids + +class Employee: + new_id=1 + def __init__(self,name): + self.id=Employee.new_id + self.name=name + Employee.new_id+=1 + def say_id(self): + return f'My id: {self.id}, name: {self.name}' +e1=Employee("E1") +e2=Employee("E2") +e3=Employee("E3") +print(e1.say_id()) +print(e2.say_id()) +print(e3.say_id()) \ No newline at end of file diff --git a/q5.py b/q5.py new file mode 100644 index 0000000..5aff393 --- /dev/null +++ b/q5.py @@ -0,0 +1,32 @@ +# ## Question 5: + +# * Create a `Vehicle` class with `name`, `max_speed` and `mileage` instance attributes +# * Add function `__str__` to vehicle class and print the info about vehicle as: `"Vehicle Model X has max speed 180 and mileage 12"` +# * Create a child class `Bus` that will inherit all of the variables and methods of the `Vehicle` class +# * Add attribute `capacity` to class `Bus` +# * Update `Bus` class such that print message will be: `Bus Breng has max speed 180 and mileage 50 with capacity 100` (**Hint:** Override \__str__ method) +# * Add `update_capacity()` method to the class `Bus` +# * Create a `Vehicle` and a `Bus` object and print both of them +# * call `update_capacity()` method for the earlier created `Bus` object and print it, see the difference + +class Vehicle: + def __init__(self,name,max_speed,millage): + self.name=name + self.max_speed=max_speed + self.millage=millage + def __str__(self): + return f'Vehicle Model {self.name} has max speed {self.max_speed} and millage {self.millage}' +class Bus(Vehicle): + def __init__(self, name, max_speed, millage,capacity): + super().__init__(name, max_speed, millage) + self.capacity=capacity + def __str__(self): + return super().__str__()+f' capacity: {self.capacity}' + def update_capacity(self,update): + self.capacity=update +ve1=Vehicle('Veh X',220,10) +bus1=Bus('Bus X',180,12,45) +print(ve1) +print(bus1) +bus1.update_capacity(50) +print(bus1) \ No newline at end of file