Skip to content

Instantly share code, notes, and snippets.

alias glog='git log --pretty=format:"%C(yellow)%h%C(yellow) %Cblue%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%Creset %C(cyan)%s%Creset - %Cred%an%Cred" --date=iso'
# The English alphabet has 26 characters.
# Each character is encoded according to their position in the alphabet.
# E.g. A = 1, B = 2, C = 3 and so on.
# Given a sequence of digits, determine how many valid character sequences can be made from it.
# E.g. 123 = 3: ABC (1, 2, 3) or LC (12, 3) or AW (1, 23)
# Another example, 271 only has 1: BGA (2, 7, 1) as 27 and 71 are not valid character encodings.
def num_char_encodings(digits):
dp = [1, 1]
for n, digit in enumerate(digits[1:], 1):
# https://www.careercup.com/question?id=5711620404674560
# From N stacks pick M numbers.
# Since we are picking from stacks, any number popped MUST also be taken.
# Maximise the sum of numbers of popped.
def stacks_pick_m(stacks, M):
dp = [[0 for _ in xrange(M + 1)] for _ in xrange(len(stacks) + 1)]
for n, stack in enumerate(stacks, 1):
@krone
krone / stochastic_dp.rb
Last active November 20, 2017 07:49
Stochastic Dynamic Programming
require 'ostruct'
require 'benchmark'
def cost(config, perm)
last_letter = perm.pop
current = config[last_letter].time
# TODO: Further perf improvements can be made by
# keeping a memoized table of costs for *parts* of the permutation
# e.g. Take two perms -> ABCDE and FABCDE, FABCDE = F(ABCDE),
merged_branches(){
local red=`tput setaf 1`
local green=`tput setaf 2`
local bold=`tput bold`
local reset=`tput sgr0`
local current_branch=$(git rev-parse --abbrev-ref HEAD)
for branch in $(git branch --merged | cut -c3-)
do
if [[ $branch = $current_branch ]]; then