Google Drive Logo Using Turtle

import turtle as t

t.hideturtle()

t.Screen().bgcolor("Black")

t.pencolor("white")

t.pensize(0)

t.begin_fill()

t.fillcolor('#4688F4')

t.forward(170)

t.left(60)

t.forward(50)

t.left(120)

t.forward(220)

t.left(120)

t.forward(50)

t.end_fill()

t.begin_fill()

t.fillcolor('#1FA463')

t.left(120)

t.forward(200)

t.left(120)

t.forward(50)

t.left(60)

t.forward(150)

t.left(60)

t.forward(50)

t.end_fill()

t.penup()

t.left(120)

t.forward(200)

t.left(120)

t.forward(50)

t.pendown()

t.begin_fill()

t.fillcolor('#FFD048')

t.left(125)

t.forward(160)

t.left(55)

t.forward(53)

t.left(126)

t.forward(163)

t.end_fill()

t.done()


Check whether a string starts and ends with the same character or not (using Regular Expression)

import re

regex = r'^[a-z]$|^([a-z]).*\1$'

def check(string):

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

print("Valid")

else:

print("Invalid")

if __name__ == '__main__' :


sample1 = "abba"

sample2 = "a"

sample3 = "abcd"


check(sample1)

check(sample2)

check(sample3)


Javascript logo using turtle

import turtle

t=turtle.Turtle()

t.penup()

t.goto(-20,-70)

t.color("#F0DB4F","#F0DB4F")

t.begin_fill()

t.pendown()

t.left(165)

t.forward(100)

t.right(70)

t.forward(220)

t.setheading(0)

t.forward(229)

t.penup()

t.goto(-20,-70)

t.setheading(0)

t.pendown()

t.left(15)

t.forward(100)

t.left(70)

t.forward(229)

t.end_fill()

t.penup()

t.goto(-35,-20)

t.setheading(90)

t.color("white","white")

t.pendown()

t.begin_fill()

t.forward(150)

t.left(90)

t.forward(20)

t.left(90)

t.forward(130)

t.setheading(90)

t.left(75)

t.forward(40)

t.left(110)

t.forward(20)


t.penup()

t.goto(-35,-20)

t.setheading(90)

t.left(75)

t.pendown()

t.forward(60)

t.end_fill()

t.penup()

t.color("yellow","yellow")

t.goto(-20,-55)

t.setheading(90)

t.pendown()

t.begin_fill()

t.forward(215)

t.right(90)

t.forward(100)

t.right(95)

t.forward(195)

t.left(90)

t.end_fill()

t.penup()

t.color("white","white")

t.pensize(2)

t.goto(-10,-20)

t.begin_fill()

t.setheading(0)

t.left(15)

t.pendown()

t.forward(65)

t.left(70)

t.forward(70)

t.left(90)

t.left(15)

t.forward(50)

t.right(100)

t.forward(50)

t.right(90)

t.forward(50)

t.left(85)

t.forward(20)

t.left(95)

t.forward(70)

t.left(90)

t.forward(90)

t.left(100)

t.forward(50)

t.right(105)

t.forward(37)

t.right(75)

t.forward(50)

t.left(85)

t.forward(20)

t.end_fill()

t.hideturtle()

turtle.done()


Replace multiple words

test_str = 'Python for engineers,The complete solution for python programs'

print("The original string is : " + str(test_str))

word_list = ["for", 'The', 'solution']

repl_wrd = 'hai'

res = ' '.join([repl_wrd if idx in word_list else idx for idx in test_str.split()])

print("String after multiple replace : " + str(res))


Program for Counting Sort

def countSort(arr):

output = [0 for i in range(256)]

count = [0 for i in range(256)]

ans = ["" for _ in arr]

for i in arr:

count[ord(i)] += 1

for i in range(256):

count[i] += count[i-1]

for i in range(len(arr)):

output[count[ord(arr[i])]-1] = arr[i]

count[ord(arr[i])] -= 1

for i in range(len(arr)):

ans[i] = output[i]

return ans

arr = "Pythonforengineers"

ans = countSort(arr)

print ("Sorted character array is %s" %("".join(ans)))


Program for Radix Sort

def countingSort(arr, exp1):

n = len(arr)

output = [0] * (n)

count = [0] * (10)

for i in range(0, n):

index = (arr[i]/exp1)

count[int((index)%10)] += 1

for i in range(1,10):

count[i] += count[i-1]

i = n-1

while i>=0:

index = (arr[i]/exp1)

output[ count[ int((index)%10) ] - 1] = arr[i]

count[int((index)%10)] -= 1

i -= 1

i = 0

for i in range(0,len(arr)):

arr[i] = output[i]

def radixSort(arr):

max1 = max(arr)

exp = 1

while max1/exp > 0:

countingSort(arr,exp)

exp *= 10

arr = [ 170, 45, 75, 90, 802, 24, 2, 66]

radixSort(arr)

for i in range(len(arr)):

print(arr[i],end=" ")


Maximum profit by buying and selling a share at most k times

def maxProfit(prices, n, k):

profit = [[0 for i in range(k + 1)]

for j in range(n)]

for i in range(1, n):

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

max_so_far = 0

for l in range(i):

max_so_far = max(max_so_far, prices[i] -

prices[l] + profit[l][j - 1])

profit[i][j] = max(profit[i - 1][j], max_so_far)

return profit[n - 1][k]

k = 2

prices = [10, 22, 5, 75, 65, 80]

n = len(prices)

print("Maximum profit is:",

maxProfit(prices, n, k))


Balloon Shooter Game

import pygame

import sys

import random

from math import *

pygame.init()

width = 700

height = 600

display = pygame.display.set_mode((width, height))

pygame.display.set_caption("CopyAssignment - Balloon Shooter Game")

clock = pygame.time.Clock()

margin = 100

lowerBound = 100

score = 0

white = (230, 230, 230)

lightBlue = (4, 27, 96)

red = (231, 76, 60)

lightGreen = (25, 111, 61)

darkGray = (40, 55, 71)

darkBlue = (64, 178, 239)

green = (35, 155, 86)

yellow = (244, 208, 63)

blue = (46, 134, 193)

purple = (155, 89, 182)

orange = (243, 156, 18)

font = pygame.font.SysFont("Arial", 25)

class Balloon:

    def __init__(self, speed):

        self.a = random.randint(30, 40)

        self.b = self.a + random.randint(0, 10)

        self.x = random.randrange(margin, width - self.a - margin)

        self.y = height - lowerBound

        self.angle = 90

        self.speed = -speed

        self.proPool= [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]

        self.length = random.randint(50, 100)

        self.color = random.choice([red, green, purple, orange, yellow, blue])

    def move(self):

        direct = random.choice(self.proPool)

        if direct == -1:

            self.angle += -10

        elif direct == 0:

            self.angle += 0

        else:

            self.angle += 10

        self.y += self.speed*sin(radians(self.angle))

        self.x += self.speed*cos(radians(self.angle))

        if (self.x + self.a > width) or (self.x < 0):

            if self.y > height/5:

                self.x -= self.speed*cos(radians(self.angle)) 

            else:

                self.reset()

        if self.y + self.b < 0 or self.y > height + 30:

            self.reset()         

    def show(self):

        pygame.draw.line(display, darkBlue, (self.x + self.a/2, self.y + self.b), (self.x + self.a/2, self.y + self.b + self.length))

        pygame.draw.ellipse(display, self.color, (self.x, self.y, self.a, self.b))

        pygame.draw.ellipse(display, self.color, (self.x + self.a/2 - 5, self.y + self.b - 3, 10, 10))   

    def burst(self):

        global score

        pos = pygame.mouse.get_pos()

        if isonBalloon(self.x, self.y, self.a, self.b, pos):

            score += 1

            self.reset()                

    def reset(self):

        self.a = random.randint(30, 40)

        self.b = self.a + random.randint(0, 10)

        self.x = random.randrange(margin, width - self.a - margin)

        self.y = height - lowerBound 

        self.angle = 90

        self.speed -= 0.002

        self.proPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]

        self.length = random.randint(50, 100)

        self.color = random.choice([red, green, purple, orange, yellow, blue])   

balloons = []

noBalloon = 10

for i in range(noBalloon):

    obj = Balloon(random.choice([1, 1, 2, 2, 2, 2, 3, 3, 3, 4]))

    balloons.append(obj)

def isonBalloon(x, y, a, b, pos):

    if (x < pos[0] < x + a) and (y < pos[1] < y + b):

        return True

    else:

        return False   

def pointer():

    pos = pygame.mouse.get_pos()

    r = 25

    l = 20

    color = lightGreen

    for i in range(noBalloon):

        if isonBalloon(balloons[i].x, balloons[i].y, balloons[i].a, balloons[i].b, pos):

            color = red

    pygame.draw.ellipse(display, color, (pos[0] - r/2, pos[1] - r/2, r, r), 4)

    pygame.draw.line(display, color, (pos[0], pos[1] - l/2), (pos[0], pos[1] - l), 4)

    pygame.draw.line(display, color, (pos[0] + l/2, pos[1]), (pos[0] + l, pos[1]), 4)

    pygame.draw.line(display, color, (pos[0], pos[1] + l/2), (pos[0], pos[1] + l), 4)

    pygame.draw.line(display, color, (pos[0] - l/2, pos[1]), (pos[0] - l, pos[1]), 4)

def lowerPlatform():

    pygame.draw.rect(display, darkGray, (0, height - lowerBound, width, lowerBound))

def showScore():

    scoreText = font.render("Balloons Bursted : " + str(score), True, white)

    display.blit(scoreText, (150, height - lowerBound + 50))

def close():

    pygame.quit()

    sys.exit()    

def game():

    global score

    loop = True

    while loop:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                close()

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_q:

                    close()

                if event.key == pygame.K_r:

                    score = 0

                    game()

            if event.type == pygame.MOUSEBUTTONDOWN:

                for i in range(noBalloon):

                    balloons[i].burst()

        display.fill(lightBlue)        

        for i in range(noBalloon):

            balloons[i].show()

        pointer()        

        for i in range(noBalloon):

            balloons[i].move()        

        lowerPlatform()

        showScore()

        pygame.display.update()

        clock.tick(60)

game()


Google Drive Logo Using Python Turtle

import turtle as t

t.hideturtle()

t.Screen().bgcolor("Black")

t.pencolor("white")

t.pensize(0)

t.begin_fill()

t.fillcolor('#4688F4')

t.forward(170)

t.left(60)

t.forward(50)

t.left(120)

t.forward(220)

t.left(120)

t.forward(50)

t.end_fill()

t.begin_fill()

t.fillcolor('#1FA463')

t.left(120)

t.forward(200)

t.left(120)

t.forward(50)

t.left(60)

t.forward(150)

t.left(60)

t.forward(50)

t.end_fill()

t.penup()

t.left(120)

t.forward(200)

t.left(120)

t.forward(50)

t.pendown()

t.begin_fill()

t.fillcolor('#FFD048')

t.left(125)

t.forward(160)

t.left(55)

t.forward(53)

t.left(126)

t.forward(163)

t.end_fill()

t.done()


Program to generate CAPTCHA and verify user

import random

def checkCaptcha(captcha, user_captcha):

if captcha == user_captcha:

return True

return False

def generateCaptcha(n):

chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

captcha = ""

while (n):

captcha += chrs[random.randint(1, 1000) % 62]

n -= 1

return captcha

captcha = generateCaptcha(9)

print(captcha)

print("Enter above CAPTCHA:")

usr_captcha = input()

if (checkCaptcha(captcha, usr_captcha)):

print("CAPTCHA Matched")

else:

print("CAPTCHA Not Matched")