class Circle:
pi = 3.141592653589793238
def __init__(self, radius):
self.r = radius
def calc_circum(self):
return 2*self.r*Circle.pi
def calc_area(self):
return Circle.pi*self.r*self.r
C1 = Circle(5)
print(C1.calc_area())
print(C1.calc_circum())
Author: Sayan Dey
Class Object Program using multiple methods in Python
class Laptop():
def __init__(self, brand_name, model_name, price):
#initializing instance varaibles
self.bn = brand_name
self.mn = model_name
self.p = price
def apply_disc(self, disc):
after_disc = self.p - (self.p * (disc/100))
return after_disc
L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)
print(L1.apply_disc(20))
# print(L1.bn, L1.mn, L1.p)
# print(L2.bn, L2.mn, L2.p)
# print(L3.bn, L3.mn, L3.p)
Basic Class and Object implementation using Python
- The init() method gets called depends upon how many objects we create of that class.
- init() method always starts with self keyword according to naming convention. It is not always necessary to write self.
- init() is a method as well as a constructor in Python.
class Person:
def __init__(self, first_name, last_name, age):
print("Init method / constructor gets called here.")
self.fn = first_name
self.ln = last_name
self.a = age
p1 = Person('Sayan', 'Dey', 21)
p2 = Person('Dey', 'Sayan', 21)
print(p1.fn)
print(p2.ln)
Sort Colors in Python without using sort() – Google Interview Question
def sort_colour(arr):
red = 0
blue = 0
for i in range(len(arr)):
if arr[i] == 1:
red+=1
elif arr[i] == 2:
blue+=1
return [1]*red + [2]*blue + [3]*(len(arr) - red - blue)
a = [1, 2, 3, 1, 3, 2, 1, 2, 3, 1]
print(sort_colour(a))
Sieve of Eratosthenes in Python
from math import sqrt
def sieve_prime(num):
prime_arr = [1] * (num+1)
prime_arr[0] = 0
prime_arr[1] = 0
for i in range(2, int(sqrt(num))+1):
if prime_arr[i] == 1:
j=2
while i*j<num+1:
prime_arr[j*i] = 0
j+=1
return prime_arr
print(sieve_prime(15))
Find Prime Number using shortest time complexity using Python
from math import sqrt
def check_Prime(num):
if num>1:
for i in range(2, int(sqrt(num/2))):
if(num%i==0):
return(f"{num} is Not Prime")
return(f"{num} is Prime")
print(check_Prime(18))
Sum of Maximum Sub Array elements in Python
def max_sum_SubArray(arr):
current_sum = 0
max_so_far = arr[0]
for i in range(len(arr)):
current_sum = current_sum + arr[i]
if max_so_far < current_sum:
max_so_far = current_sum
elif current_sum < 0:
current_sum = 0
return max_so_far
arr = [4, -3, -2, 2, 3, 1, -2, -3, 6, -6, -4, 2, 1]
print(max_sum_SubArray(arr))
Length of last word from a given string using Python
def length():
string = input("Enter the stirng : ").split()
last_word = len(string[-1])
print(last_word)
length()
Remove duplicated from Sorted Array in Python
def remove_duplicates(arr):
arr = sorted(arr)
# print(arr)
rmv_dpl = list(set(arr))
return rmv_dpl
a = [9, 7, 5, 8, 6, 1, 2, 4, 3, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(remove_duplicates(a))
Hotel Booking System using Python
def hotel_booking(arr, dept, room):
event = [(t, 'RED') for t in arr] + [(t, "BLUE") for t in dept]
event = sorted(event)
guest = 0
for e in event:
if e[1] == 'RED':
guest+=1
else:
guest-=1
if guest > room:
return 'Sorry the rooms are full'
return 'Rooms available'
arr = [1, 3, 5]
dept = [2, 6, 8]
k = 1
print(hotel_booking(arr, dept, k))