This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import urllib2 | |
import simplejson | |
def get_info(adress): | |
print "************************************************" | |
api = "http://freegeoip.net/json/" + adress | |
try: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tree | |
attr_accessor :children, :node_name | |
def initialize(name, children={}) | |
@children = children | |
@node_name = name | |
end | |
def visit_all(&block) | |
visit &block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> '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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |