program to create a doubly linked list from a ternary tree

class Node:    

    def __init__(self,data):    

        self.data = data;    

        self.left = None;    

        self.middle = None;    

        self.right = None;    

class TernaryTreeToDLL:    

    def __init__(self):    

        self.root = None;    

        self.head = None;    

        self.tail = None;    

    def convertTernaryToDLL(self, node):    

        if(node == None):    

            return;    

        left = node.left;    

        middle = node.middle;    

        right = node.right;    

        if(self.head == None):    

            self.head = self.tail = node;    

            node.middle = None;    

            self.head.left = None;    

            self.tail.right = None;    

        else:    

            self.tail.right = node;    

            node.left = self.tail;    

            node.middle = None;    

            self.tail = node;    

            self.tail.right = None;    

        self.convertTernaryToDLL(left);    

        self.convertTernaryToDLL(middle);    

        self.convertTernaryToDLL(right);        

    def displayDLL(self):    

        current = self.head;    

        if(self.head == None):    

            print("List is empty");    

            return;    

        print("Nodes of generated doubly linked list: ");    

        while(current != None):    

            print(current.data),    

            current = current.right;    

tree = TernaryTreeToDLL();    

tree.root = Node(5);    

tree.root.left = Node(10);    

tree.root.middle = Node(12);    

tree.root.right = Node(15);    

tree.root.left.left = Node(20);    

tree.root.left.middle = Node(40);    

tree.root.left.right = Node(50);    

tree.root.middle.left = Node(24);    

tree.root.middle.middle = Node(36);    

tree.root.middle.right = Node(48);    

tree.root.right.left = Node(30);    

tree.root.right.middle = Node(45);    

tree.root.right.right = Node(60);    


tree.convertTernaryToDLL(tree.root);    

     

tree.displayDLL();

Program to write numbers 1 to 100 in a file

class File:

    def Create_file(self,fname):

        file = open(fname , 'w+')

        file.close()

        print("File Created as '{}'Successfully!\n".format(fname))


    def write_1__to_100(self,fname):

        try:

            fp = open(fname , 'w')

            for i in range(1,101):

                fp.write(str(i)+'\t')

            fp.close()

            print("\n Numbers written Successfully!")


        except FileNotFoundError:

            print('File not found')

if __name__ == '__main__':

    obj = File()

    k = 'C'

    while k=='C':

        ch = int(input("1.Create a File \n2.Write numbers 1 to 100 in file\nAny other key to Exit\n"))

        if ch==1:

            fname = input('Enter File name:')

            obj.Create_file(fname)

        elif ch==2:

            fname = input('Enter File name:')

            obj.write_1__to_100(fname)

        else:

            exit(0)

        k = input("Press 'C' to continue Other Key to exit:")

        print('\n')

else:

    pass

Heap Sort in Python

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))  

Bubble Sort in Python

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))  


Program to Find HCF(Highest Common Factor)

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))  

program to create and display a doubly linked list

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();  

Generate Captcha Using Python


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')


Program to find minimum cost path

R = 3

C = 3

def minCost(cost, m, n):

tc = [[0 for x in range(C)] for x in range(R)]


tc[0][0] = cost[0][0]

for i in range(1, m + 1):

tc[i][0] = tc[i-1][0] + cost[i][0]

for j in range(1, n + 1):

tc[0][j] = tc[0][j-1] + cost[0][j]

for i in range(1, m + 1):

for j in range(1, n + 1):

tc[i][j] = min(tc[i-1][j-1], tc[i-1][j],tc[i][j-1]) + cost[i][j]

return tc[m][n]

cost = [[1, 2, 3],

[4, 8, 2],

[1, 5, 3]]

print(minCost(cost, 2, 2))


Convert nested tuple to custom key dictionary

sample_tuple = ((4, 'Gfg', 10), (3, 'is', 8), (6, 'Best', 10))

print("The original tuple : " + str(sample_tuple))

res = [{'key': sub[0], 'value': sub[1], 'id': sub[2]} for sub in sample_tuple]

print("The converted dictionary : " + str(res))


Permutation of a given string using inbuilt function

from itertools import permutations

def allPermutations(str):

permList = permutations(str)

for perm in list(permList):

print (''.join(perm))

if __name__ == "__main__":

str = 'ABC'

allPermutations(str)