Skip to content

Instantly share code, notes, and snippets.

# solves the puzzle from https://gist.github.com/1925593
# unpolished version - i'm tired
input = (<<EOF).split("\n").map{|row| row.split(//).map(&:intern)}
-------------
| |
| U
|* * * * *|
| |
| # |
@simonkro
simonkro / roman.rb
Created December 2, 2012 22:15
Ruby Quiz Solution
def roman_to_arabic
{'M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4,
'I' => 1}
end
def to_roman n
roman_to_arabic.map do |roman, arabic|
i, n = n.divmod(arabic)
@simonkro
simonkro / one_thing.rb
Created December 3, 2012 20:09
Roman Numerals
p "Hi, what is your name ?"
@name = gets.chomp
p "Welcome to the quiz, #{@name}! " + "Please enter a Roman numeral with value between 1 and 4 here:"
a = gets.to_i
if a == 1
p "I"
end
if a == 2
require 'number_converter'
ARGV.each do |input_number|
result = NumberConverter.new(input_number).convert
puts "#{input_number} = #{result}"
end
#Maybe it's not the way you would do it in ruby and code is not that
#beautiful and everyone knows how to read file line by line and how to make
#validation of inputs and how to use methods, global and local variables
#but I am sure my idea is genuis. Engoy
class Test
str=ARGV[0]
@simonkro
simonkro / roman.rb
Created December 4, 2012 07:56
Another Version
def digit_to_roman digit, one, five, ten
case d = digit % 10
when 0..3 then one * d
when 4 then one + five
when 5..8 then five + one * (d - 5)
when 9 then one + ten
end
end
def to_roman n
def make_change amount, coins = [1,2,5,10,20,50,100,200]
tree = Hash[coins.zip(coins)]
while tree[amount].nil? do
tree.keys.each do |value|
coins.each {|coin| tree[value + coin] ||= coin}
end
end
deep = lambda{|a| (coin = tree[a]) ? [coin] + deep[a - coin] : []}
deep[amount].sort.reverse
@simonkro
simonkro / application.rb
Created January 13, 2013 00:32
Toying around with Gtk bindings
require 'gir_ffi-gtk3'
Gtk.init
app = Gtk::Application.new('org.gtk.example', :flags_none)
app.signal_connect('startup') do
window = Gtk::ApplicationWindow.new(app)
menu = Gtk::Menu.new
@simonkro
simonkro / threadsafe.rb
Created May 28, 2013 10:11
Untested ...
class ThreadSafeWrapper < Delegator
def initialize(obj)
super
@mutex = Mutex.new
end
def method_missing(m, *args, &block)
@mutex.synchronize { super }
end
class GildedRose < Struct.new(:name, :days_remaining, :quality)
def tick
return if name == 'Sulfuras, Hand of Ragnaros'
self.days_remaining -= 1
case name
when 'Aged Brie' then
increase_quality