Skip to content

Instantly share code, notes, and snippets.

Avatar
❤️
Playing With Life Since 2002

Abhishek Chaudhary theabbie

❤️
Playing With Life Since 2002
View GitHub Profile
@theabbie
theabbie / ascii_human.py
Last active Oct 22, 2022
Dancing ASCII Human in Python
View ascii_human.py
import random
from time import sleep
class Human:
def __init__(self):
self.human = [[" ", "o", " "], ["/", "|", "\\"], ["/", " ", "\\"]]
self.front = False
self.commandmap = {
"left hand to head": self.leftHandToHead,
"left hand to hip": self.leftHandToHip,
@theabbie
theabbie / rle.py
Created Oct 15, 2022
Run-length Encoding Imlementation in Python
View rle.py
def encode(s):
n = len(s)
res = []
i = 0
while i < n:
ctr = 1
while i < n - 1 and s[i] == s[i + 1]:
ctr += 1
i += 1
if ctr == 1:
@theabbie
theabbie / binexp.py
Created Oct 11, 2022
Iterative binary exponentiation
View binexp.py
def pow(a, b):
curr = a
res = 1
while b:
if b & 1:
res *= curr
b = b >> 1
curr *= curr
return res
@theabbie
theabbie / triemap.py
Last active Sep 22, 2022
TrieMap implementation in python
View triemap.py
class Node:
def __init__(self, val = None):
self.left = None
self.right = None
self.val = val
self.end = False
class TrieMap:
def __init__(self):
self.root = Node()
@theabbie
theabbie / trie.py
Created Sep 20, 2022
Trie (Prefix Tree) Implementation in Python
View trie.py
class Node:
def __init__(self):
self.child = {}
self.end = False
class Trie:
def __init__(self):
self.root = Node()
def insert(self, s):
@theabbie
theabbie / radical.py
Created Sep 3, 2022
Radical of a number
View radical.py
from collections import defaultdict
def radical(n):
primes = [True] * (n + 1)
primes[0] = primes[1] = False
for i in range(n + 1):
if primes[i]:
j = 2
while i * j <= n:
primes[i * j] = False
@theabbie
theabbie / inverse.py
Created Aug 29, 2022
Inverse of a function using binary search
View inverse.py
def inverse(x, f, k = 15):
beg = 0
end = 1
while f(end) < x:
beg += 1
end += 1
while k:
mid = (beg + end) / 2
if f(mid) > x:
end = mid
@theabbie
theabbie / sparse_table.py
Created Aug 16, 2022
Sparse Table Implementation in Python
View sparse_table.py
def genSparse(arr):
n = len(arr)
k = len("{:b}".format(n))
table = [[float('inf')] * k for _ in range(n)]
for l in range(k):
for i in range(n):
if l == 0:
table[i][l] = arr[i]
else:
a = table[i][l - 1]
@theabbie
theabbie / segtree.py
Last active Sep 4, 2022
Segment Tree Implementation in Python
View segtree.py
class Node:
def __init__(self, start, end, min = float('inf')):
self.start = start
self.end = end
self.left = None
self.right = None
self.min = min
class SegTree:
def __init__(self, nums):
@theabbie
theabbie / binary_search_tree.py
Created Jul 30, 2022
Binary Search Tree (BST) implementation in Python
View binary_search_tree.py
class Node:
def __init__(self, val = None, ctr = 1, left = None, right = None):
self.val = val
self.ctr = ctr
self.left = left
self.right = right
class BST:
def __init__(self):
self.root = None