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)