Skip to content

Instantly share code, notes, and snippets.

@rameshrvr
rameshrvr / python_trie.py
Last active September 18, 2019 11:52
Trie implementation in python. Supported operations (Adding string, searching string, prefix based searching, deleting string)
class Trie():
"""
"""
def __init__(self, char=None):
self.char = char
self.children = {}
self.count = 1
@rameshrvr
rameshrvr / calculate_power_of_two.py
Last active January 6, 2019 07:49
Calculate 2^N where N upto 10^20 in O(1) complexity. (Fastest way to compute large power of 2 modulo a number)
MODULO = 1000000007
def calc_power_of_two(number):
# i have taken 60 as base number for 64 bit processor
if number <= 60:
# Fastest way to calculate 2^N is to left shift binary 1 to N times
return (1 << number)
quotient = int(number / 60) % MODULO
@rameshrvr
rameshrvr / get_all_keys.py
Last active September 23, 2018 12:54
Get all keys of a nested dictionary python
from six import iteritems
def get_all_keys(dictionary):
"""
Method to get all keys from a nested dictionary as a List
Args:
dictionary: Nested dictionary
Returns:
List of keys in the dictionary
"""