Skip to content

Instantly share code, notes, and snippets.

View neotrinity's full-sized avatar

Gokulnath Haribabu neotrinity

View GitHub Profile
@neotrinity
neotrinity / problem_solution_1.py
Created November 11, 2014 18:09
O(1) insert, remove, contains and get_random
import random
class MyDataStructure(object):
def __init__(self):
self.d = {} # hash table
self.li = [] # dynamic resizing array
def insert(self, key):
if key in self.d:
@neotrinity
neotrinity / trie_tokenize.py
Last active August 29, 2015 14:09
Split string to words problem using a trie. Trie.tokenize method takes in a string and returns the tokenized string if we can successfully match else it returns back an empty string. This is a recursive solution and goes through all possible approaches. It greedily looks for the maximum length words to tokenize on and if it fails, then it takes …
class TrieNode(object):
"""
Trie Node
"""
def __init__(self,key):
self._key = key
self._value = None
self._children = {}
@neotrinity
neotrinity / simple_run_length_encoding.py
Created November 21, 2014 14:10
Simple run length encoding problem
def compress(s):
"""
s -> string to be compressed
retuns -> compressed string
"""
if not s:
return ""
prev = None
count = 0