Pattern matching with Regex

import re

phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')

mo = phoneNumRegex.search('My number is 415-555-4242.')

print('Phone number found: ' + mo.group())



Draw Among Us Character with Turtle

import turtle

color1 =  'yellow'

color2 = ''

color3 = 'skyblue'

color4 = ''

s = turtle.getscreen()

t = turtle.Turtle()

def x():

    t.pensize(20)

    t.fillcolor(color1)

    t.begin_fill()

    t.right(90)

    t.forward(50)

    t.right(180)

    t.circle(40, -180)

    t.right(180)

    t.forward(200)

    t.right(180)

    t.circle(100, -180)

    t.backward(20)

    t.left(15)

    t.circle(500, -20)

    t.backward(20)

    t.circle(40, -180)

    t.left(7)

    t.backward(50)

    t.up()

    t.left(90)

    t.forward(10)

    t.right(90)

    t.down()

    t.right(240)

    t.circle(50, -70)

    t.end_fill()

def glass():

    t.up()

    t.right(230)

    t.forward(100)

    t.left(90)

    t.forward(20)

    t.right(90)

    t.down()

    t.fillcolor(color3)

    t.begin_fill()

    t.right(150)

    t.circle(90, -55)

    t.right(180)

    t.forward(1)

    t.right(180)

    t.circle(10, -65)

    t.right(180)

    t.forward(110)

    t.right(180)

    t.circle(50, -190)

    t.right(170)

    t.forward(80)

    t.right(180)

    t.circle(45, -30)

    t.end_fill()

def y():

    t.up()

    t.right(60)

    t.forward(100)

    t.right(90)

    t.forward(75)

    t.fillcolor(color1)

    t.begin_fill()

    t.down()

    t.forward(30)

    t.right(255)

    t.circle(300, -30)

    t.right(260)

    t.forward(30)

    t.end_fill()

x()

glass()

y()

turtle.done()

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)


Finding sum of digits of a number until sum becomes single digit

import math

def digSum( n):

sum = 0

while(n > 0 or sum > 9):

if(n == 0):

n = sum

sum = 0

sum += n % 10

n /= 10

return sum

n = 1234

print (digSum(n))


Check whether a number has consecutive 0’s in the given base or not

def hasConsecutiveZeroes(N, K):

z = toK(N, K)

if (check(z)):

print("Yes")

else:

print("No")

def toK(N, K):

w = 1

s = 0

while (N != 0):

r = N % K

N = N//K

s = r * w + s

w = 10

return s

def check(N):

fl = False

while (N != 0):

r = N % 10

N = N//10

if (fl == True and r == 0):

return False

if (r > 0):

fl = False

continue

fl = True

return True

N, K = 15, 8

hasConsecutiveZeroes(N, K)


Program for Number of solutions to Modular Equations

import math

def calculateDivisors (A, B):

N = A - B

noOfDivisors = 0

a = math.sqrt(N)

for i in range(1, int(a + 1)):

if ((N % i == 0)):

if (i > B):

noOfDivisors +=1

if ((N / i) != i and (N / i) > B):

noOfDivisors += 1;

return noOfDivisors

def numberOfPossibleWaysUtil (A, B):

if (A == B):

return -1

if (A < B):

return 0

noOfDivisors = 0

noOfDivisors = calculateDivisors;

return noOfDivisors

def numberOfPossibleWays(A, B):

noOfSolutions = numberOfPossibleWaysUtil(A, B)

if (noOfSolutions == -1):

print ("For A = " , A , " and B = " , B

, ", X can take Infinitely many values"

, " greater than " , A)

else:

print ("For A = " , A , " and B = " , B

, ", X can take " , noOfSolutions

, " values")

A = 26

B = 2

numberOfPossibleWays(A, B)

A = 21

B = 5

numberOfPossibleWays(A, B)