Colored Hexagons Turtle

import turtle

from random import randint

x = 20

y = 20

turtle.speed(300)

turtle.colormode(255)

def move(l, a):

                turtle.right(a)

                turtle.forward(l)

def hex():

        turtle.pendown()

        turtle.color( randint(0,255),randint(0,255),randint(0,255) )

        turtle.begin_fill()

        for i in range(6):

                move(x,-60)

        turtle.end_fill()

        turtle.penup()

turtle.penup()

for color in range (y):

        if color == 0:

                hex()

                move(x,-60)

                move(x,-60)

                move(x,-60)

                move(0,180)

        for i in range (6):

                move(0,60)

                for j in range (color+1):

                        hex()

                        move(x,-60)

                        move(x,60)

                move(-x,0)

        move(-x,60)

        move(x,-120)

        move(0,60)

turtle.exitonclick()

Generating random strings until a given string is generated

import string

import random

import time

possibleCharacters = (string.ascii_lowercase + string.digits +

string.ascii_uppercase + ' ., !?;:')

t = "hi"

attemptThis = ''.join(random.choice(possibleCharacters)

for i in range(len(t)))

attemptNext = ''

completed = False

iteration = 0

while completed == False:

print(attemptThis)

attemptNext = ''

completed = True

for i in range(len(t)):

if attemptThis[i] != t[i]:

completed = False

attemptNext += random.choice(possibleCharacters)

else:

attemptNext += t[i]

iteration += 1

attemptThis = attemptNext

time.sleep(0.1)

print("Target matched after " +

str(iteration) + " iterations")


Check if Binary representation is Palindrome

def binaryPallindrome(num):

binary = bin(num)

binary = binary[2:]

return binary == binary[-1::-1]

if __name__ == "__main__":

num = 9

print (binaryPallindrome(num))


Program to accept string ending with alphanumeric character

import re

regex = '[a-zA-z0-9]$'

def check(string):

if(re.search(regex, string)):

print("Accept")

else:

print("Discard")

if __name__ == '__main__' :

string = "python@"

check(string)

string = "python326"

check(string)

string = "python."

check(string)

string = "python"

check(string)


Program for Recursive Insertion Sort

def insertionSortRecursive(arr, n):

if n <= 1:

return

insertionSortRecursive(arr, n - 1)

last = arr[n - 1]

j = n - 2

while (j >= 0 and arr[j] > last):

arr[j + 1] = arr[j]

j = j - 1

arr[j + 1] = last

if __name__ == '__main__':

A = [-7, 11, 6, 0, -3, 5, 10, 2]

n = len(A)

insertionSortRecursive(A, n)

print(A)