Skip to content

Instantly share code, notes, and snippets.

View SiestaMadokaist's full-sized avatar

Rama Patria Himawan SiestaMadokaist

View GitHub Profile
@SiestaMadokaist
SiestaMadokaist / SummedAreaTable.py
Last active November 17, 2020 10:46
Implementation of summed area table / integral image in python.
class SummedAreaTable(object):
def __init__(self, size, data):
"""
Just because I dislike a 2d array / list.
data should be a List of Integer.
"""
width, height = size
assert width * height == len(data), "invalid data length and or data size"
self.size = size
self.data = data
@SiestaMadokaist
SiestaMadokaist / FibMatrix.py
Last active August 29, 2015 14:14
O(log(n)) Fibbonaci via matrix multiplication.
class Matrix(list):
def __init__(self, *args):
super(Matrix, self).__init__(args)
def __mul__(self, other):
def dotproduct(As, Bs):
return sum(a * b for a, b in zip(As, Bs))
otherT = zip(*other)
return Matrix(*[[dotproduct(As, Bs) for Bs in otherT] for As in self])
@SiestaMadokaist
SiestaMadokaist / 14Matrix.py
Last active August 29, 2015 14:14
DP + Divide and Conquer to solve how many numbers are there within a certain 10 ^ n, which contains 14.
class Matrix(list):
def __init__(self, *args):
super(Matrix, self).__init__(args)
def __mul__(self, other):
def dotproduct(As, Bs):
return sum(a * b for a, b in zip(As, Bs))
otherT = zip(*other)
return Matrix(*[[dotproduct(As, Bs) for Bs in otherT] for As in self])
@SiestaMadokaist
SiestaMadokaist / Finite Automata.py
Last active August 29, 2015 14:19
Finite Automata
import re
class State():
class NotValidTrigger(BaseException): pass
def __init__(self, name, isEndState):
self.triggers = []
self.name = name
self.isEndState = isEndState
def addTrigger(self, trigger, nextState):
class Field:
"""
custom-version of array-2d.
when instatiated, each element inside will be initialized with `initial` value.
e.g:
>> Field(2)
[[None, None],
[None, None]]
"""
@classmethod
@SiestaMadokaist
SiestaMadokaist / RubyRefactor.rb
Last active August 29, 2015 14:22
Refactor your ruby from camelCased into underscored
class CamelCase
# @param original [String]
def initialize(original)
@original = original
end
# @return [String]
def refactored
regex = /(?:(?:\.)|(?:\:)|(?:\()|(?:\{)|(?:\[)| )[a-z_]+(?:[A-Z][a-z]+)+/
@SiestaMadokaist
SiestaMadokaist / bfsing.py
Last active August 29, 2015 14:23
searching the most troublesome (biggest) fail (file) in your directory.
import os
import operator as op
from memoized_property import memoized_property
import re
# credit to:
# http://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
def sizeof_fmt(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
@SiestaMadokaist
SiestaMadokaist / hopefully-will-refactor-later.py
Last active August 29, 2015 14:24
Refactor wrapper method.
import operator as op
import re
class PatternLocation:
def __init__(self, pattern, start):
self._pattern = pattern
self._start = start
@property
def stop(self):
@SiestaMadokaist
SiestaMadokaist / wao.hs
Created July 11, 2015 10:55
first-time doing something with haskell. #haskell version of https://gist.github.com/SiestaMadokaist/b5a8116d552123b41a8e
module DNAEncoder where
import qualified Data.Algorithms.KMP as KMP
import qualified Data.List as DL
import qualified Control.Monad as CM
data PLoc a = PLoc {
pattern :: [a],
start :: Int
} deriving (Show)
@SiestaMadokaist
SiestaMadokaist / flask-server.py
Created August 20, 2015 09:22
python-simple-flask-server
from flask import Flask, request, send_from_directory
import json
import os
app = Flask(__name__)
@app.route('/<path:path>')
def send_result(path):
print(path)
return send_from_directory("./", path)