from heapq import heappop, heappush
def heapsort(list1):
heap = []
for ele in list1:
heappush(heap, ele)
sort = []
while heap:
sort.append(heappop(heap))
return sort
list1 = [27, 21, 55, 15, 60, 4, 11, 17, 2, 87]
print(heapsort(list1))
Solve Problems by Coding Solutions - A Complete solution for python programming
from heapq import heappop, heappush
def heapsort(list1):
heap = []
for ele in list1:
heappush(heap, ele)
sort = []
while heap:
sort.append(heappop(heap))
return sort
list1 = [27, 21, 55, 15, 60, 4, 11, 17, 2, 87]
print(heapsort(list1))
def bubble_sort(list1):
for i in range(0,len(list1)-1):
for j in range(len(list1)-1):
if(list1[j]>list1[j+1]):
temp = list1[j]
list1[j] = list1[j+1]
list1[j+1] = temp
return list1
list1 = [5, 3, 8, 6, 7, 2]
print("The unsorted list is: ", list1)
print("The sorted list is: ", bubble_sort(list1))
def calculate_hcf(x, y):
if x > y:
smaller = y
else:
smaller = x
for i in range(1,smaller + 1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("The H.C.F. of", num1,"and", num2,"is", calculate_hcf(num1, num2))
class Node:
def __init__(self,data):
self.data = data;
self.previous = None;
self.next = None;
class DoublyLinkedList:
def __init__(self):
self.head = None;
self.tail = None;
def addNode(self, data):
newNode = Node(data);
if(self.head == None):
self.head = self.tail = newNode;
self.head.previous = None;
self.tail.next = None;
else:
self.tail.next = newNode;
newNode.previous = self.tail;
self.tail = newNode;
self.tail.next = None;
def display(self):
current = self.head;
if(self.head == None):
print("List is empty");
return;
print("Nodes of doubly linked list: ");
while(current != None):
print(current.data),;
current = current.next;
dList = DoublyLinkedList();
dList.addNode(1);
dList.addNode(2);
dList.addNode(3);
dList.addNode(4);
dList.addNode(5);
dList.display();
from captcha.image import ImageCaptcha
image = ImageCaptcha(width = 280, height = 90)
captcha_text = 'python'
data = image.generate(captcha_text)
image.write(captcha_text,'CAPTCHA.png')