Skip to content

Instantly share code, notes, and snippets.

class Decomposer
def initialize params
@uv_gen = params[:uv_gen]
@qu_gen = params[:qu_gen]
@qv_gen = params[:qv_gen]
end
def each
@uv_gen.each do |u, v|
ruby 1.8.7 (2009-09-11 patchlevel 202) [x86_64-linux]
Rehearsal -----------------------------------------------------------------
large hash empty 0.700000 0.000000 0.700000 ( 0.708355)
large hash ek empty 0.170000 0.000000 0.170000 ( 0.168563)
large set empty 1.090000 0.000000 1.090000 ( 1.099709)
large myset empty 0.170000 0.000000 0.170000 ( 0.172110)
large hash work 1.190000 0.000000 1.190000 ( 1.193358)
large hash ek work 0.630000 0.000000 0.630000 ( 0.631994)
large set work 1.610000 0.000000 1.610000 ( 1.610816)
words = Hash.new 0
Dir.glob("#{ARGV.first}/**/*").reject { |file| File.directory? file }.each do |file|
File.read(file).scan(/\w+/).each { |word| words[word] += 1 }
end
File.open('counts-descreasing-ruby', 'w') do |file|
file << words.sort_by { |word, count| count }.reverse.map { |word, count| "#{word}\t#{count}" }.join("\n")
end
require 'benchmark'
require './integer'
require 'prime'
Benchmark.bmbm do |bench|
bench.report 'simple:' do
(2..100_000).each { |i| i.simple_prime? }
end
bench.report 'stdlib:' do
(2..100_000).each { |i| i.prime? }
# Form all the arithmetic expressions that consist of the digits one
# through nine, in order, with a plus-sign, a times-sign, or a null
# character interpolated between each pair of digits; for instance,
# 12+34*56+7*89 is a valid expression that evaluates to 2539. What
# number is the most frequent result of evaluating all possible
# arithmetic expressions formed as described above? How many times
# does it occur? What are the expressions that evaluate to that result?
digits = ('1'..'9').to_a
opers = ['', '+', '*']
require 'ostruct'
require 'delegate'
os = OpenStruct.new
sd = SimpleDelegator.new os
os.foo # => nil
os.bar = :baz # => :baz
sd.qux # => NoMethodError
require 'benchmark'
require './integer'
require 'prime'
Benchmark.bmbm do |bench|
bench.report 'simple:' do
(2..100_000).each { |i| i.simple_prime? }
end
bench.report 'clever:' do
(2..100_000).each { |i| i.clever_prime? }
class Hash
def to_html
[
'<ul>',
map { |k, v| ["<li><strong>#{k}</strong>", v.respond_to?(:to_html) ? v.to_html : "<span>#{v}</span></li>"] },
'</ul>'
].join
end
end

Pressman

General Information

Pressman is a two-player game played with sixteen black and sixteen white stones on a square, eight by eight board. Each player controls all of the stones of a single colour, and they take alternating turns to move their stones; the goal

#!/usr/bin/env ruby
# encoding: UTF-8
require 'iconv'
blocks = <<end
░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░ ░░ ░░░ ░░░ ░░░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░░░ ░░░ ░░░ ░░░ ░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░
█▀█ █▀█ █▀▀ █▀█ █▀▀ █▀▀ █▀▀ █░█ █ ░█ █░█ █░░ █▀█▀█ █▀█ █▀█ █▀█ █▀█ █▀█ █▀▀ ▀█▀ █░█ █░█ █░█░█ ▀░▀ █░█ ▀▀█ ░ █▀█ ▀█░ ▀▀█ ▀▀█ █░█ █▀▀ █▀▀ ▀▀█ █▀█ █▀█
█▀█ █▀█ █░░ █░█ █▀▀ █▀▀ █░█ █▀█ █ ░█ ██░ █░░ █░█░█ █░█ █░█ █▀▀ ▀▀█ ██▀ ▀▀█ ░█░ █░█ █░█ █░█░█ ░▀░ ▀▀█ █▀▀ ░ █░█ ░█░ █▀▀ ▀▀█ ▀▀█ ▀▀█ █▀█ ░░█ █▀█ ▀▀█
▀░▀ ▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀ ▀░░ ▀▀▀ ▀░▀ ▀ ▀▀ ▀░▀ ▀▀▀ ▀░▀░▀ ▀░▀ ▀▀▀ ▀░░ ░░▀ ▀░▀ ▀▀▀ ░▀░ ▀▀▀ ░▀░ ▀▀▀▀▀ ▀░▀ ░▀▀ ▀▀▀ ░ ▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀ ░░▀ ▀▀▀ ▀▀▀ ░░▀ ▀▀▀ ▀▀▀