Skip to content

Instantly share code, notes, and snippets.

View neotrinity's full-sized avatar

Gokulnath Haribabu neotrinity

View GitHub Profile
@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
@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 / 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 / abc_tester.py
Created November 5, 2014 12:57
getting my head around abc
import abc
class AbstractV(object):
__metaclass__ = abc.ABCMeta
def __init__(self, **kwargs):
for k, v in kwargs.iteritems():
import sys
# needed for casting PyCObject to void pointer
from ctypes import pythonapi, c_void_p, py_object
from PySide.QtCore import *
from PySide.QtGui import *
import gobject
import pygst
import simplegui
import random
class Direction(object):
UP, DOWN, LEFT, RIGHT = range(4)
class GameState(object):
RUNNING, FINISHED = range(2)
@neotrinity
neotrinity / elm_clock_tutorial.elm
Last active August 29, 2015 14:06
elm clock tutorial
main = lift clock (every second)
number n = let angle = degrees (90 - 6 * (n * 5))
in move (100 * cos angle, 100 * sin angle) (toForm (asText n))
displayNumbers = group (map number [1..12])
clock t = collage 400 400 [ outlined (solid blue) (circle 110)
@neotrinity
neotrinity / hash_folds.hs
Last active August 29, 2015 14:04
Decryption problem in haskell and python
-- Using folds version
import Data.List
import Data.Maybe
letters = "acdegilmnoprstuw"
getIndexOf :: Char -> String -> Int
getIndexOf x xs = fromMaybe (-1) (elemIndex x xs)
hashacc :: Int -> Char -> Int
import sys
## Typical Usage:
## python parse_transalations.py 'path/to/messages.po'
## This prints the strings and their respective translations
def clean(s):
""" Quick and dirty scrubbing of the data """
return s.replace('"', '').replace('msgid ', '').replace('msgstr ', '')
@neotrinity
neotrinity / fancy_sort.py
Created May 12, 2014 22:58
Fancy sort - longest to smallest and then to longest
def fancy_sort(countries):
""" countries -> list of countries """
sorted_countries = [(-1 * len(x[1]) if x[0] % 2 else len(x[1]), x[1])
for x in enumerate(sorted(countries, key=len))]
return [x for x in sorted(sorted_countries, key=lambda x: x[0])]
countries = ['Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola', 'Antigua & Deps', 'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bhutan', 'Bolivia', 'Bosnia Herzegovina', 'Botswana', 'Brazil', 'Brunei', 'Bulgaria', 'Burkina', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Central African Rep', 'Chad', 'Chile', 'China', 'Colombia', 'Comoros', 'Congo', 'Congo {Democratic Rep}', 'Costa Rica', 'Croatia', 'Cuba', 'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'East Timor', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Fiji