0% found this document useful (0 votes)
32 views2 pages

Misc

The document contains code snippets demonstrating different Python concepts like sorting algorithms, inheritance, abstraction, encapsulation. It shows examples of selection sort, bubble sort, binary search, abstract base classes with different car classes inheriting from it, private attributes and methods using double underscores.

Uploaded by

Paradox Banerjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

Misc

The document contains code snippets demonstrating different Python concepts like sorting algorithms, inheritance, abstraction, encapsulation. It shows examples of selection sort, bubble sort, binary search, abstract base classes with different car classes inheriting from it, private attributes and methods using double underscores.

Uploaded by

Paradox Banerjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

# sort

my_list = [-15, -26, 15, 1, 23, -64, 23, 76] # abstraction


new_list = [] from abc import ABC, abstractmethod

while my_list:
min = my_list[0] class Car(ABC):
for x in my_list: def mileage(self):
if x < min: pass
min = x
new_list.append(min)
my_list.remove(min) class Tesla(Car):
def mileage(self):
print(new_list) print("The mileage is 30kmph")

class Suzuki(Car):
# bubble sort def mileage(self):
print("The mileage is 25kmph ")
def bubbleSort(arr):
n = len(arr)
for i in range(n): class Duster(Car):
for j in range(0, n - i - 1): def mileage(self):
if arr[j] > arr[j + 1]: print("The mileage is 24kmph ")
arr[j], arr[j + 1] = arr[j +
1], arr[j]
class Renault(Car):
def mileage(self):
arr = [64, 34, 25, 12, 22, 11, 90] print("The mileage is 27kmph ")
bubbleSort(arr)
print("Sorted array is:")
for i in range(len(arr)): t = Tesla()
print("%d" % arr[i]) t.mileage()
r = Renault()
#bindary search r.mileage()
def binary_search(arr, low, high, x): s = Suzuki()
if high >= low: s.mileage()
mid = (high + low) // 2 d = Duster()
d.mileage()
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, # encapsulation
mid - 1, x)
else: class Software:
return binary_search(arr, mid + def __init__(self):
1, high, x) self.__update()
else:
def install(self):
return -1 print("Installing")

def __update(self):
arr = [2, 3, 4, 10, 40] print("Updating")
x = 10

result = binary_search(arr, 0, len(arr) - 1, gpay = Software()


x) gpay.install()

if result != -1:
print("Element is present at index", class Car:
str(result)) __maxSpeed = 0
else:
print("Element is not present in array") def __init__(self):
self.__maxSpeed = 200
print(self.__maxSpeed)

def setspeed(self, speed):


self.__maxSpeed = speed
print(self.__maxSpeed)

c = Car()
c.setspeed(400)
c.__maxSpeed = 100

You might also like