Skip to content

Instantly share code, notes, and snippets.

@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:
@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 / 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 / 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 / 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
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 / timer
Last active January 4, 2016 02:19
ruby function to time a function
# found at <https://github.com/jdantonio/functional-ruby/blob/master/lib/functional/utilities.rb>
# Run the given block and time how long it takes in seconds. All arguments
# will be passed to the block. The function will return two values. The
# first value will be the duration of the timer in seconds. The second
# return value will be the result of the block.
#
# @param args [Array] zero or more arguments to pass to the block
#
# @return [Integer, Object] the duration of the operation in seconds and
@404pnf
404pnf / gist:10964044
Created April 17, 2014 08:19
for the sake of another way doing fizbuzz
# for the sake of another way doing fizbuzz
# must call fiz last since since it returns n by default
>> fiz = -> n { n % 3 == 0 ? 'fiz' : n }
=> #<Proc:0x00000101be66d8@(irb):11 (lambda)>
>> buzz = -> n { n % 5 == 0 ? 'buzz' : nil }
=> #<Proc:0x00000101bb7a68@(irb):12 (lambda)>
>> fizbuzz = -> n { n % 5 == 0 && n % 3 == 0 ? 'fizbuzz' : nil }
=> #<Proc:0x00000102065970@(irb):13 (lambda)>
>> (1..40).map { |e| fizbuzz[e] or buzz[e] or fiz[e] }
@404pnf
404pnf / gist:19b4ef978be621144253
Created May 26, 2014 06:59
ruby coll, ascending? and friends
# my proposal to functional-ruby gem
def in_order?(compare_fn)
-> col, &blk do
if blk
col.map {|e| blk[e] }
.each_cons(2)
.all? { |e1, e2| e1.send(compare_fn, e2) }
else
col.each_cons(2).all? { |e1, e2| e1.send(compare_fn, e2) }
# require 'benchmark'
# # ==> true
# def ascending?(data, opts={})
# return false if data.nil?
# (data.size-1).times do |i|
# if block_given?
# return false if yield(data[i]) > yield(data[i+1])
# else