Skip to content

Instantly share code, notes, and snippets.

View gchiam's full-sized avatar

Gordon Chiam gchiam

View GitHub Profile
import functools
def merge_or_append(ranges, element):
if ranges and ranges[-1][0] <= element[0] <= ranges[-1][1]:
ranges[-1] = (ranges[-1][0], max(ranges[-1][1], element[1]))
else:
ranges.append(element)
return ranges
"""Binary Tree: Longest path
https://link.medium.com/tHhd8C0KOU
"""
class Node(object):
def __init__(self, value):
self.value = value
self.left =
self.right = None
"""Maximum Subarray Problem
The maximum subarray problem is the task of finding the largest
possible sum of a contiguous subarray, within a given one-dimensional
array A[1…n] of numbers.
https://link.medium.com/MPXDJMtDMU
"""
def maxsubarray(array):
def fibonaci_recursive(n):
if n <= 2:
return 1
return fibonaci_recursive(n-1) + fibonaci_recursive(n-2)
def fibonaci_dp_1(n):
memo = {}
for i in range(1, n + 1):
@gchiam
gchiam / cm.py
Created May 2, 2017 06:11
context manager
import contextlib
@contextlib.contextmanager
def cm():
info = {}
print 'start'
yield info
print 'end', info
class Root(object):
def setup(self):
print 'Root.setup'
class Base(Root):
def setup(self):
print 'Base.setup'
super(Base, self).setup()
@gchiam
gchiam / keybase.md
Created March 1, 2017 04:32
keybase.md

Keybase proof

I hereby claim:

  • I am gchiam on github.
  • I am gchiam (https://keybase.io/gchiam) on keybase.
  • I have a public key whose fingerprint is 7628 D70D 2B68 1304 86DB B346 BAE1 0BD7 51A0 125E

To claim this, I am signing this object:

from itertools import izip_longest
def get_chunks(size, parts):
ranges = list(range(0, size, size // (parts - 1)))
return list(izip_longest(ranges, ranges[1:], fillvalue=size))
if __name__ == '__main__':
size = 500
@gchiam
gchiam / singleton.py
Last active February 9, 2017 03:01
Singleton metaclass
class Singleton(type):
"""A singleton metaclass.
Usage:
class Foo(object):
__metaclass__ = Singleton
assert Foo() is Foo()
"""
@gchiam
gchiam / item2_italics.sh
Last active February 7, 2017 04:26
Italics in Item2
infocmp xterm-256color > /tmp/xterm-256color.terminfo
cp /tmp/xterm-256color.terminfo /tmp/xterm-256color.terminfo.origial # backup
printf '\tsitm=\\E[3m, ritm=\\E[23m,\n' >> /tmp/xterm-256color.terminfo
tic /tmp/xterm-256color.terminfo
infocmp screen-256color > /tmp/screen-256color.terminfo
cp /tmp/screen-256color.terminfo /tmp/screen-256color.terminfo.origial # backup
printf '\tsitm=\\E[3m, ritm=\\E[23m,\n' >> /tmp/screen-256color.terminfo
tic /tmp/screen-256color.terminfo