Skip to content

Instantly share code, notes, and snippets.

View tlehman's full-sized avatar

Tobi Lehman tlehman

View GitHub Profile
@tlehman
tlehman / backtracking_mkp.rb
Created April 15, 2016 23:07
Multiple Knapsack Problem solution using backtracking
ITEMS = [6,2,2,4].map { |q| {quantity: q} }
KNAPSACKS = [10,4,4].map { |c| {capacity: c} }
N = ITEMS.length
M = KNAPSACKS.length
A = Array.new(N*M, 0)
# [0 1 1 1 [3
# 1 0 0 0 ----> 1
# 0 1 0 1] 2]
def sum_rows(x, w)
@tlehman
tlehman / README.md
Created February 10, 2016 19:18 — forked from quiver/README.md
Who says PostgreSQL can't Pub/Sub like Redis?

Pub/Sub pattern with PostgreSQL's LISTEN/NOTIFY command

This is a simple chat-like program using pub-sub pattern, backed by PostgreSQL's LISTEN/NOTIFY command.

Publish

publish message to foo channel from user nickname.

$ python pub.py foo nickname
PUBLISH to channel #foo
@tlehman
tlehman / generics.swift
Created October 14, 2015 17:06
generics in Swift 2.0
//: Playground - noun: a place where people can play
import UIKit
// generic function using pass by reference
func swap<T>(inout a: T, inout b: T) {
let tempA: T = a
a = b
b = tempA
}
@tlehman
tlehman / boolfail.py
Created September 21, 2015 23:11
Redefine true and false in python
# Python boolean fail
# Or how to redefine True and False and win any argument on the internet!
# Love, @tlehman
from unittest import TestCase
class BoolFail(TestCase):
def test_false(self):
True = False
assert True == False
@tlehman
tlehman / lan_ip.rb
Created August 27, 2015 18:03
LAN IP
puts UDPSocket.open { |s| s.connect('www.example.com', 1); s.addr.last }
@tlehman
tlehman / normal_form.md
Last active August 29, 2015 14:21
normalized data

Normalization in data modeling

Say you have a service where users can add products they like to a wish list, representing this with JSON, you might expect something like this:

[
  {
    "user_id": 3,
    "email": "foo@example.com",
 "wishlist": [
@tlehman
tlehman / gem_desc.txt
Created February 3, 2015 17:52
look up gem description from command line
$ curl -s https://rubygems.org/gems/faraday | ruby -rnokogiri -e "puts Nokogiri::HTML(STDIN.read).css('.gem__desc p').children.first"
HTTP/REST API client library.
$ curl -s https://rubygems.org/gems/nokogiri | ruby -rnokogiri -e "puts Nokogiri::HTML(STDIN.read).css('.gem__desc p').children.first"
Nokogiri (鋸) is an HTML, XML, SAX, and Reader parser. Among Nokogiri's
many features is the ability to search documents via XPath or CSS3 selectors.
XML is like violence - if it doesn’t solve your problems, you are not using
enough of it.
@tlehman
tlehman / balance.rb
Created January 16, 2015 01:02
paren balancing
# Balanced paren strings have the property that each opening ( has a closing ).
# This proceeds by scanning left to right, using a stack to keep track of open
# parens.
def balanced?(string)
characters = string.split(//)
stack = []
characters.each do |c|
stack.push(c) if c == "("
return false if stack.empty? and c == ")"
@tlehman
tlehman / keybase.md
Created January 8, 2015 19:47
Keybase proof

Keybase proof

I hereby claim:

  • I am tlehman on github.
  • I am tlehman (https://keybase.io/tlehman) on keybase.
  • I have a public key whose fingerprint is 702A 8535 CE1D D733 D885 D431 FD0E 4D25 7D77 5AEB

To claim this, I am signing this object:

@tlehman
tlehman / arraysEqual.js
Last active August 29, 2015 14:10
Check whether two JS arrays are equal
// clever trick from http://stackoverflow.com/a/8618383/46871
var arraysEqual = function(a,b) { return !!a && !!b && !(a<b || b<a); }