Spiral Square Mania with Turtle

import turtle as t

pen = t.Turtle()

pen.color("cyan")

pen.speed(0)

colors = ["green","black","orange","navy","green","black","orange","navy"]

def draw_square(color):

        for side in range(4):

                pen.forward(100)

                pen.right(90)

                for side in range(4):

                        pen.forward(50)

                        pen.right(90)

pen.penup()

pen.back(40)

pen.pendown()

for color in colors:

        pen.color(color)

        draw_square(color)

        pen.forward(50)

        pen.left(45)

pen.hideturtle()

t.done()

Chess-Board using Turtle

import turtle

sc = turtle.Screen()

cb = turtle.Turtle()

def draw():

    for i in range(4):

        cb.forward(30)

        cb.left(90)

    cb.forward(30)

if __name__ == "__main__":

    sc.setup(400, 600)

    cb.speed(100)

    for i in range(8):

        cb.up()

        cb.setpos(-100, 30 * i)

        cb.down()

        for j in range(8):

            if (i + j) % 2 == 0:

                col = 'black'

            else:

                col = 'white'

            cb.fillcolor(col)

            cb.begin_fill()

            draw()

            cb.end_fill()

    cb.hideturtle()

    turtle.done()


The Story of Python, by Its Creator, Guido van Rossum

Guido van Rossum  is a Dutch programmer best known as the creator of the Python programming language, for which he was the "benevolent dictator for life" (BDFL) until he stepped down from the position on 12 July 2018. He remained a member of the Python Steering Council through 2019, and withdrew from nominations for the 2020 election.

Iterative Method to find Height of Binary Tree

class Node:

def __init__(self, data):

self.data = data

self.left = None

self.right = None

def treeHeight(root):

if root is None:

return 0

q = []

q.append(root)

height = 0

while(True):

nodeCount = len(q)

if nodeCount == 0 :

return height

height += 1

while(nodeCount > 0):

node = q[0]

q.pop(0)

if node.left is not None:

q.append(node.left)

if node.right is not None:

q.append(node.right)

nodeCount -= 1

root = Node(1)

root.left = Node(2)

root.right = Node(3)

root.left.left = Node(4)

root.left.right = Node(5)

print ("Height of tree is", treeHeight(root))




Split a Circular Linked List into two halves

class Node:

def __init__(self, data):

self.data = data

self.next = None

class CircularLinkedList:

def __init__(self):

self.head = None

def push(self, data):

ptr1 = Node(data)

temp = self.head

ptr1.next = self.head

if self.head is not None:

while(temp.next != self.head):

temp = temp.next

temp.next = ptr1

else:

ptr1.next = ptr1 

self.head = ptr1

def printList(self):

temp = self.head

if self.head is not None:

while(True):

print ("%d" %(temp.data),end=' ')

temp = temp.next

if (temp == self.head):

break

def splitList(self, head1, head2):

slow_ptr = self.head

fast_ptr = self.head

if self.head is None:

return

while(fast_ptr.next != self.head and

fast_ptr.next.next != self.head ):

fast_ptr = fast_ptr.next.next

slow_ptr = slow_ptr.next

if fast_ptr.next.next == self.head:

fast_ptr = fast_ptr.next

head1.head = self.head

if self.head.next != self.head:

head2.head = slow_ptr.next

fast_ptr.next = slow_ptr.next

slow_ptr.next = self.head

head = CircularLinkedList()

head1 = CircularLinkedList()

head2 = CircularLinkedList()

head.push(12)

head.push(56)

head.push(2)

head.push(11)

print ("Original Circular Linked List")

head.printList()

head.splitList(head1 , head2)

print ("\nFirst Circular Linked List")

head1.printList()

print ("\nSecond Circular Linked List")

head2.printList()