Counting Fingers

import cv2

import mediapipe as mp

cap = cv2.VideoCapture(0)

mp_Hands = mp.solutions.hands

hands = mp_Hands.Hands()

mpdraw = mp.solutions.drawing_utils

finger_Coord = [(8, 6), (12, 10), (16, 14), (20, 18)]

thumb_Coord = (4,2)

while True:

    success, image = cap.read()

    RGB_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    results = hands.process(RGB_image)

    multiLandMarks = results.multi_hand_landmarks

    if multiLandMarks:

        handList = []

        for handLms in multiLandMarks:

            mpdraw.draw_landmarks(image, handLms, mp_Hands.HAND_CONNECTIONS)

            for idx, lm in enumerate(handLms.landmark):

                h, w, c = image.shape

                cx, cy = int(lm.x * w), int(lm.y * h)

                handList.append((cx, cy))

            for point in handList:

                cv2.circle(image, point, 10, (255, 255, 0), cv2.FILLED)

                upCount = 0

        for coordinate in finger_Coord:

            if handList[coordinate[0]][1] < handList[coordinate[1]][1]:

                upCount += 1

        if handList[thumb_Coord[0]][0] > handList[thumb_Coord[1]][0]:

            upCount += 1

            cv2.putText(image, str(upCount), (150,150), cv2.FONT_HERSHEY_PLAIN, 12, (0,255,0), 12)


    cv2.imshow("Counting number of fingers", image)

    cv2.waitKey(1)

sudoku

board=[

    [2,3,0,0,7,0,0,0,0],

    [6,0,0,1,9,5,0,0,0],

    [0,9,8,0,0,0,0,6,0],

    [8,0,0,0,6,0,0,0,3],

    [4,0,0,8,0,3,0,0,1],

    [5,0,0,0,2,0,0,0,6],

    [0,6,0,0,0,0,2,8,0],

    [0,0,0,4,1,9,0,0,5],

    [0,0,0,0,9,0,0,8,9]]

def print_board(bo):

    for i in range(len(bo)):

        if i%3 == 0 and i!=0:

            print("---------------------")

        for j in range(len(bo[0])):

            if j%3 == 0 and j!=0:

                print(" | ", end ="")

            if j==8:

                print(bo[i][j])

            else:

                print(str(bo[i][j]) + " ",end="")

def find_empty(bo):

    for i in range(len(bo)):

        for j in range(len(bo[0])):

            if bo[i][j] == 0:

                return (i, j)

    return None

def valid(bo, num, pos):

    for i in range(len(bo[0])):

        if bo[pos[0]][i] == num and pos[1]!= i:

            return False

    for j in range(len(bo)):

        if bo[i][pos[1]] == num and pos[0]!= i:

            return False

    box_x = pos[1]//3

    box_y = pos[0]//3

    for i in range(box_y*3, box_y*3 + 3):

        for j in range(box_x*3, box_x*3 + 3):

            if bo[i][j] == num and  (i,j) != pos:

                return False

    return True

def solve(bo):

    find = find_empty(bo)

    if not find:

        return True

    else:

        row, col = find

    for i in range(1,10):

        if valid(bo, i , (row,col)):

            bo[row][col] = i

            if solve(bo):

                return True

            bo[row][col]=0

    return False

print_board(board)

solve(board)

print("Solving")

print_board(board)

print("Solved")

snowman

import turtle

turtle.speed(2)

turtle.setup(800, 700)

turtle.bgcolor("black")

turtle.penup()

turtle.goto(0, -320)

turtle.pendown()

turtle.color("#c1f8b5")

turtle.begin_fill()

turtle.circle(320)

turtle.end_fill()

turtle.penup()

turtle.goto(0, -280)

turtle.pendown()

turtle.color("white")

turtle.begin_fill()

turtle.circle(110)

turtle.end_fill()

turtle.penup()

turtle.goto(0, -110)

turtle.pendown()

turtle.begin_fill()

turtle.circle(90)

turtle.end_fill()

turtle.penup()

turtle.goto(0, 20)

turtle.pendown()

turtle.begin_fill()

turtle.circle(70)

turtle.end_fill()

def black_circle():

turtle.color("black")

turtle.begin_fill()

turtle.circle(10)

turtle.end_fill()

x = -20

for i in range(2):

turtle.penup()

turtle.goto(x, 110)

turtle.pendown()

black_circle()

x = x + 40

y = 0

for i in range(5):

turtle.penup()

turtle.goto(0, y)

turtle.pendown()

black_circle()

y = y - 55

turtle.penup()

turtle.goto(0,70)

turtle.pendown()

turtle.color("red")

turtle.begin_fill()

turtle.circle(17)

turtle.end_fill()

turtle.penup()

turtle.goto(0,75)

turtle.pendown()

turtle.color("white")

turtle.begin_fill()

turtle.circle(17)

turtle.end_fill()

turtle.penup()

turtle.goto(75, 0)

turtle.pendown()

turtle.color("brown")

turtle.begin_fill()

turtle.left(40)

for i in range(2):

turtle.forward(75)

turtle.left(90)

turtle.forward(7)

turtle.left(90)

turtle.end_fill()

turtle.penup()

turtle.goto(115, 38)

turtle.pendown()

turtle.begin_fill()

turtle.left(40)

for i in range(2):

turtle.forward(25)

turtle.left(90)

turtle.forward(5)

turtle.left(90)

turtle.end_fill()

turtle.begin_fill()

turtle.right(100)

for i in range(2):

turtle.forward(25)

turtle.left(90)

turtle.forward(5)

turtle.left(90)

turtle.end_fill()

turtle.penup()

turtle.goto(-130, 50)

turtle.pendown()

turtle.begin_fill()

turtle.right(10)

for i in range(2):

turtle.forward(75)

turtle.right(90)

turtle.forward(7)

turtle.right(90)

turtle.end_fill()

turtle.penup()

turtle.goto(-112, 58)

turtle.pendown()

turtle.begin_fill()

turtle.right(40)

for i in range(2):

turtle.forward(25)

turtle.right(90)

turtle.forward(5)

turtle.right(90)

turtle.end_fill()

turtle.begin_fill()

turtle.right(100)

turtle.penup()

turtle.goto(-108, 31)

turtle.pendown()

for i in range(2):

turtle.forward(25)

turtle.right(90)

turtle.forward(5)

turtle.right(90)

turtle.end_fill()

turtle.penup()

turtle.goto(50, 150)

turtle.pendown()

turtle.color("black")

turtle.begin_fill()

turtle.right(10)

turtle.forward(100)

turtle.right(90)

turtle.forward(10)

turtle.right(90)

turtle.forward(20)

turtle.left(90)

turtle.forward(45)

turtle.right(90)

turtle.forward(60)

turtle.right(90)

turtle.forward(45)

turtle.left(90)

turtle.forward(20)

turtle.right(90)

turtle.end_fill()

turtle.hideturtle()


Extract Links

import requests

from bs4 import BeautifulSoup

url = 'https://www.pythonforengineers.in/'

reqs = requests.get(url)

soup = BeautifulSoup(reqs.text, 'html.parser')

urls = []

for link in soup.find_all('a'):

print(link.get('href'))


Scraping TV Rating

import requests

from bs4 import BeautifulSoup

def getdata(url):

r = requests.get(url)

return r.text

htmldata = getdata("https://barcindia.co.in/data-insights")

soup = BeautifulSoup(htmldata, 'html.parser')

data = ''

for i in soup.find_all('tbody'):

data = data + (i.get_text())

data

data = ''.join((filter(lambda i: i not in ['\t'], data)))

print(data)