Skip to content

Instantly share code, notes, and snippets.

NAND = lambda { |i, j| !(i && j) }
NOT = lambda { |i| NAND[i, i] }
AND = lambda { |i, j| NOT[NAND[i, j]] }
OR = lambda { |i, j| NAND[NAND[i, i], NAND[j, j]] }
NOR = lambda { |i, j| NOT[OR[i, j]] }
@404pnf
404pnf / 00040.rb
Created December 2, 2013 02:12 — forked from plexus/00040.rb
# pnodes => path
# ppath => parent
class Zipper
attr_reader :parent, :path, :node, :lefts, :rights, :at_end
def initialize(branch, children, make_node, node, lefts = nil, path = nil, parent = nil, rights = nil, changed = false, at_end = false)
@branch = branch
@children = children
@make_node = make_node
@404pnf
404pnf / naive implementation of alphanumeric sort in ruby
Last active December 28, 2015 20:48
naive implementation of alphanumeric sort in ruby
>> 'a11'.split('').map { |e| e[/\d/] ? e : e.ord }.join
=> "9711"
>> def conv str
>> str.split('').map { |e| e[/\d/] ? e : e.ord }.join
>> end
=> nil
>> a
=> ["a", "a1", "a12", "b", "a2"]
>> a.sort_by { |e| conv e}
=> ["a", "a1", "a12", "a2", "b"]
@404pnf
404pnf / tree.rb
Created November 14, 2013 00:49 — forked from hanachin/tree.rb
class Tree
attr_accessor :children, :node_name
def initialize(name, children={})
@children = children
@node_name = name
end
def visit_all(&block)
visit &block
@404pnf
404pnf / goof.rb
Created November 14, 2013 00:49 — forked from aasmith/goof.rb
# Generate all possible combinations for a 46-point win in the game of
# Goofspiel.
#
cards = [*1..13]
res = [*4..13].map do |n|
cards.combination(n).
select { |cards| cards.reduce(:+) == 46 }.
map(&:sort).
map(&:reverse)
@404pnf
404pnf / geoip
Created November 14, 2013 00:38 — forked from Sulter/geoip
#!/usr/bin/env python
import sys
import urllib2
import simplejson
def get_info(adress):
print "************************************************"
api = "http://freegeoip.net/json/" + adress
try:
# run using ```rvm jruby-1.6.7 do jruby "-J-Xmx2000m" "--1.9" tej.rb```
require 'rubygems'
require 'nokogiri'
require 'csv'
f = File.open("/tmp/preview.html")
doc = Nokogiri::HTML(f)
csv = CSV.open("/tmp/output.csv", 'w',{:col_sep => ",", :quote_char => '\'', :force_quotes => true})
@404pnf
404pnf / piglatin.rb
Last active December 24, 2015 01:38
piglatin and rotation implemented in ruby
class String
def vowel?
match(/[aeiou]/i)
end
def constant?
!match(/[aeiou]/i)
end
def rotate(n = 1)
# Get Sublime to use your rvm ruby... Change your ~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.sublime-build to this, replacing YOURUSERNAME:
{
"cmd": [
"bundle", "exec", "/Users/YOURUSERNAME/.rvm/bin/rvm-auto-ruby", "$file"
],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.ruby"
}
$stack, $draws = [], {}
def method_missing *args
return if args[0][/^to_/]
$stack << args.map { |a| a or $stack.pop }
$draws[$stack.pop(2)[0][0]] = args[1] if args[0] == :<
end
class Array
def +@