Skip to content

Instantly share code, notes, and snippets.

@pknowledge
pknowledge / GraphRepresentation.py
Created Jul 4, 2020
Competitive Programming with Python | Graph Representation | ADJACENCY LIST VS. ADJACENCY MATRIX https://youtu.be/CGOox3yLLYU
View GraphRepresentation.py
# UNDIRECTED GRAPH
'''
ADJ MATRIX
ADJ LIST
'''
'''
V E
FOR EVERY EDGE
@pknowledge
pknowledge / BitMagic.py
Created Jun 23, 2020
Competitive Programming with Python | Bit Magic : Interconversion, Kth Bit, Single Number https://youtu.be/4-rgHbeqjTU
View BitMagic.py
# n convert it into binary
def intToBin(n):
return str(bin(n))[2:]
# bin to int
def binToInt(s):
return int(s,2)
# kth bit set from right
def kthbit(n,k):
@pknowledge
pknowledge / BitMagic.py
Created Jun 22, 2020
Competitive Programming with Python-Bit Magic- Power of 2 + One's in Binary Representation of Number https://youtu.be/SdP4j-n8S0g
View BitMagic.py
# ispowerof 2
# n -> input
# True/False -> output
# check if n is a powerof 2
# 512 -> True 512 = 2**9
# 1024 -> True 1024 = 2**10
def ispowerof2(n):
# T.C = O(1)
if n <= 0:
View bitwise.py
# bitwise operator and - &
# bitwise operator or - |
# bitwise operator not - ~
# bitwise operator xor - ^
# bitwise operator right shift - >>
# bitwise operator left shift <<
# rightshift is divide in power of 2
# leftshift is multiply in power of 2
def evenodd(n):
if n&1 == 1:
@pknowledge
pknowledge / Sieve.py
Created Jun 18, 2020
Sieve of Eratosthenes | Competitive Programming with Python https://youtu.be/-wIYdmPiHcA
View Sieve.py
from math import *
def genprimes(n):
primes = [True]*(n+1)
primes[0] = False
primes[1] = False
for p in range(2,int(sqrt(n))+1):
if primes[p] == True:
for i in range(p*p,n+1,p):
primes[i] = False
@pknowledge
pknowledge / primalitytest.py
Created Jun 9, 2020
Number Theory for Competitive Programming with Python - Compare Primality Test in O(n) vs O(root(n)) https://youtu.be/4UZufg54dFc
View primalitytest.py
# Prime numbers two factors 1 , Number Itself
# Approach One O(n)
# Aprroach Two: Base Case + Hint: O(1) -> O(root(N))
from math import *
def approach1(n):
divcnt = 0
for i in range(1,n+1): # [1,n]
if n%i == 0:
divcnt+=1
@pknowledge
pknowledge / divisors.py
Created Jun 9, 2020
Number Theory for Competitive Programming Using Python - Find Divisors Of A Number O(sqrt(n))
View divisors.py
# 24 [1,2,3,4,6,8,12,24]
from math import *
def fun1(n):
# T.C = O(n)
div1 = []
for i in range(1,n+1): # [1,n]
if n%i == 0:
div1.append(i)
return div1
@pknowledge
pknowledge / main.c
Created Jun 7, 2020
create a car is controlled from Bluetooth via Android SmartPhone
View main.c
#include <SoftwareSerial.h> // Import Soft Uart library
#include <Servo.h> // Import Servo library
// Bluetooth communication pins
#define TX 2
#define RX 4
// L298N communication pins
#define IN1 3
#define IN2 5
@pknowledge
pknowledge / Clean_PC.ps1
Created Mar 28, 2020
Clean PC using powershell
View Clean_PC.ps1
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace(0xA)
$WinTemp = "c:\Windows\Temp\*"
#1# Empty Recycle Bin #
write-Host "Emptying Recycle Bin." -ForegroundColor Cyan
$objFolder.items() | %{ remove-item $_.path -Recurse -Confirm:$false}
#2# Remove Temp
View FolderLock.bat
@ECHO OFF
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST Private goto MDPrivate
:CONFIRM
echo Are you sure to lock this folder? (Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
You can’t perform that action at this time.