Skip to content

Instantly share code, notes, and snippets.

View theabbie's full-sized avatar
❤️
Playing With Life Since 2002

Abhishek Choudhary theabbie

❤️
Playing With Life Since 2002
View GitHub Profile
@theabbie
theabbie / binexp.py
Created October 11, 2022 14:27
Iterative binary exponentiation
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 September 22, 2022 04:51
TrieMap implementation in python
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 September 20, 2022 05:46
Trie (Prefix Tree) Implementation in Python
class Node:
def __init__(self):
self.child = {}
self.end = False
class Trie:
def __init__(self):
self.root = Node()
def insert(self, s):
@theabbie
theabbie / segtree.py
Last active September 4, 2022 11:51
Segment Tree Implementation in Python
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 / radical.py
Created September 3, 2022 14:20
Radical of a number
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 August 29, 2022 06:13
Inverse of a function using binary search
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 August 16, 2022 13:57
Sparse Table Implementation in Python
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 / binary_search_tree.py
Created July 30, 2022 04:23
Binary Search Tree (BST) implementation in Python
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
@theabbie
theabbie / mobius.py
Created July 26, 2022 03:40
Mobius Function
import math
def mobius(n):
ctr = 0
k = int(math.sqrt(n)) + 1
primes = [True] * k
primes[0], primes[1] = False, False
for i in range(k):
if primes[i]:
j = 2
@theabbie
theabbie / minimum_score_triangulation_of_polygon.py
Created June 19, 2022 10:58
Minimum Score Triangulation of Polygon Using Backtracking
class Solution:
def minScore(self, values: List[int], n, used, usedctr) -> int:
if usedctr == n - 2:
return 0
if used in self.cache:
return self.cache[used]
minScore = float('inf')
for i in range(n):
if not used & (1 << i + 1):
l = (n + i - 1) % n