From 4fc90562d34d8ff4a1432e1dba9ebfe408a90e62 Mon Sep 17 00:00:00 2001 From: adil Yunus Date: Thu, 14 Oct 2021 21:18:32 +0200 Subject: [PATCH] 5.hafta odevleri --- antwoord1.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ antwoord2.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ antwoord3.py | 42 +++++++++++++++++++++++++++++++++ antwoord4.py | 28 ++++++++++++++++++++++ antwoord5.py | 45 ++++++++++++++++++++++++++++++++++++ hackerrank1.py | 46 ++++++++++++++++++++++++++++++++++++ hackerrank2.py | 46 ++++++++++++++++++++++++++++++++++++ 7 files changed, 333 insertions(+) create mode 100644 antwoord1.py create mode 100644 antwoord2.py create mode 100644 antwoord3.py create mode 100644 antwoord4.py create mode 100644 antwoord5.py create mode 100644 hackerrank1.py create mode 100644 hackerrank2.py diff --git a/antwoord1.py b/antwoord1.py new file mode 100644 index 0000000..6d38472 --- /dev/null +++ b/antwoord1.py @@ -0,0 +1,63 @@ +''' +## 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): + self.flat = ['A Type','B Type','C Type','D Type'] + if self.income >= 25000: + print(self.flat[0]) + elif 20000 <= self.income < 25000: + print(self.flat[1]) + elif 15000 <= self.income < 20000: + print(self.flat[2]) + elif self.income < 15000: + print(self.flat[3]) + print('------------------------------') + def show_data(self): + print(f'society name is : {self.society_name}') + print(f'house number is : {self.house_no}') + print(f'no of members is : {self.no_of_members}') + print(f'income is : {self.income}') + + def show(self): + self.show_data() + self.allocate_flat() + +society1 = Society("aaa",11,123,35000) +society1.show() + +society2 = Society("bbb",12,234,22000) +society2.show() + +society3 = Society("ccc",13,345,18000) +society3.show() + +society4 = Society("ddd",14,456,14000) +society4.show() diff --git a/antwoord2.py b/antwoord2.py new file mode 100644 index 0000000..fb52748 --- /dev/null +++ b/antwoord2.py @@ -0,0 +1,63 @@ +## 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,): + self.item_code = 0 + self.item = None + self.price = None + self.qty = None + self.net_price = None + self.discount = None + + def calculate_discount(self): + if self.qty <= 10: + self.discount = 0 + elif 11 <= self.qty < 20: + self.discount = 15 + elif self.qty >= 20: + self.discount = 20 + dis_1 = self.price * self.qty * self.discount /100 + self.net_price = self.price * self.qty - dis_1 + return self.net_price + + def buy(self): + self.item_code = int(input('Enter item code : ')) + self.item = input('Enter item name : ') + self.price = int(input('Enter price : ')) + self.qty = int(input('Enter qty : ')) + self.calculate_discount() + + def show(self): + self.buy() + print(f'Code :{self.item_code}') + print(f'name :{self.item}') + print(f'Price :{self.price}') + print(f'Qty :{self.qty}') + print(f'Net Price :{self.net_price}') + print(f'Discount :{self.discount}%') + +buy1 = ItemInfo() +print(buy1.show()) \ No newline at end of file diff --git a/antwoord3.py b/antwoord3.py new file mode 100644 index 0000000..319217e --- /dev/null +++ b/antwoord3.py @@ -0,0 +1,42 @@ +"""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 multiply(cls,a): + return cls.multiplier * a + + def subtract(self,b,c): + self.b = b + self.c = c + return self.b -self.c + def value(self): + tulp = (self.x,self.y) + return tulp + +obj = Numbers(20,10) +print(obj.add()) +print(obj.multiply(5)) +print(obj.subtract(13,6)) +print(obj.value()) \ No newline at end of file diff --git a/antwoord4.py b/antwoord4.py new file mode 100644 index 0000000..b5270e1 --- /dev/null +++ b/antwoord4.py @@ -0,0 +1,28 @@ +## 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): + self.id = Employee.new_id + Employee.new_id+=1 + + def say_id(self): + return f'My id is : {self.id}' + +e1 = Employee() +print(e1.say_id()) +e2 = Employee() +print(e2.say_id()) \ No newline at end of file diff --git a/antwoord5.py b/antwoord5.py new file mode 100644 index 0000000..58ead2e --- /dev/null +++ b/antwoord5.py @@ -0,0 +1,45 @@ +## 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,mileage): + self.name = name + self.max_speed = max_speed + self.mileage = mileage + + def __str__(self): + return f'Vehicle Model{self.name} has max speed {self.max_speed} and mileage {self.mileage}' + +class Bus(Vehicle): + + def __init__(self,name,max_speed,mileage,capacity): + self.capacity = capacity + super().__init__(name,max_speed,mileage) + + def __str__(self): + return f'Bus Breng has max speed {self.max_speed} and mileage {self.mileage} with capacity {self.capacity}' + + def update_capacity(self,up): + self.capacity = up + +auto_1 = Vehicle('mustang',260,9999) +bus_1 = Bus('Benz',150,8888,56) + +print(auto_1) +print(bus_1) + +bus_1.update_capacity(99) +print(bus_1) diff --git a/hackerrank1.py b/hackerrank1.py new file mode 100644 index 0000000..3ef2a9a --- /dev/null +++ b/hackerrank1.py @@ -0,0 +1,46 @@ +# * 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) + + def calculate(self): + self.scores = scores + a = 0 + for i in range(len(scores)): + a+=scores[i] + a = a/len(scores) + # a = a/len(scores) + if 90 <= a <=100: + return "O" + elif 80 <= a <90: + return "E" + elif 70 <= a <80: + return "A" + elif 55 <= a <70: + return "P" + elif 40 <= a <55: + return "D" + elif a < 40: + 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()) \ No newline at end of file diff --git a/hackerrank2.py b/hackerrank2.py new file mode 100644 index 0000000..82a06ba --- /dev/null +++ b/hackerrank2.py @@ -0,0 +1,46 @@ + +#* **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) + complex(no.real, no.imaginary) + return Complex(a.real, a.imag) + def __sub__(self, no): + a = complex(self.real, self.imaginary) - complex(no.real, no.imaginary) + return Complex(a.real, a.imag) + def __mul__(self, no): + a = complex(self.real, self.imaginary) * complex(no.real, no.imaginary) + return Complex(a.real, a.imag) + def __truediv__(self, no): + a = complex(self.real, self.imaginary) / complex(no.real, no.imaginary) + return Complex(a.real, a.imag) + def mod(self): + a = complex(self.real, self.imaginary) + return Complex(abs(a), 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