Skip to content

Instantly share code, notes, and snippets.

View masui's full-sized avatar

Toshiyuki Masui masui

View GitHub Profile
@masui
masui / raspi_mouse.rb
Last active August 29, 2015 14:13
Raspberry PiのRubyでマウスホイール信号を取得する
File.open("/dev/input/event0","rb"){ |f|
while true do
s = f.read 16
(time, type, code, value) = s.unpack "qssi"
if type == 2 and code == 8 then
puts value
end
end
}
@masui
masui / problem9.rb
Created May 25, 2015 10:14
a^2 + b^2 == c^2 かつ a+b+c=1000, a < b < c となる a, b, c を求める
(1..1000).each { |i|
(1..1000).each { |j|
k = 1000 - i - j
next if j <= i || k <= j
puts "#{i}, #{j}, #{k}" if i*i + j*j == k*k
}
}
@masui
masui / problem9-slow.rb
Created May 25, 2015 10:48
a^2 + b^2 == c^2 かつ a+b+c=1000, a < b < c となる a, b, c を求める (流石にナイーブすぎて遅いバージョン)
(1..1000).each { |i|
(1..1000).each { |j|
(1..1000).each { |k|
next if j <= i
next if k <= j
next if i+j+k != 1000
puts "#{i}, #{j}, #{k}" if i*i + j*j == k*k
}
}
}
@masui
masui / gist:b688b4275f5d59fea6ec
Created May 25, 2015 15:44
1時間以内に解けなければプログラマ失格となってしまう5つの問題
#
# https://blog.svpino.com/2015/05/08/solution-to-problem-5-and-some-other-thoughts-about-this-type-of-questions
#
def power(level, a, &block)
if level > 0 then
['+', '-', ''].each { |op|
a[level-1] = op
power level-1, a, &block
}
else
@masui
masui / gist:89fa67d47c1925f89329
Last active August 29, 2015 14:21
1時間以内に解けなければプログラマ失格となってしまう5つの問題(2)
#
# https://blog.svpino.com/2015/05/08/solution-to-problem-5-and-some-other-thoughts-about-this-type-of-questions
#
def power(n, alts, &block)
_power(n, 0, [], alts, &block)
end
def _power(total, level, res, alts, &block)
if level < total then
alts.each { |op|
@masui
masui / slow.rb
Created May 26, 2015 06:43
a^2 + b^2 == c^2 かつ a+b+c=1000, a < b < c となる a, b, c を求める
1.upto(1000) do |c|
1.upto(c-1) do |b|
1.upto(b-1) do |a|
next if a + b + c != 1000
next if a * a + b * b != c * c
puts "#{a}^2 == #{b}^2 + #{c}^2"
end
end
end
@masui
masui / gist:8a5a6ae7e5bb7acf83ee
Last active August 29, 2015 14:22
1時間以内に解けなければプログラマ失格となってしまう5つの問題(3)
#
# https://blog.svpino.com/2015/05/08/solution-to-problem-5-and-some-other-thoughts-about-this-type-of-questions
#
def power(len, sels, arr=[], &block)
if len == 0 then
yield arr
else
sels.to_a.each { |sel|
power len-1, sels, arr + [sel], &block
}
@masui
masui / promise.coffee
Last active August 29, 2015 14:27
Promiseの例
fs = require 'fs'
readFile = (filename, enc) ->
new Promise (fulfill, reject) ->
fs.readFile filename, enc, (err, res) -> # readFile()は非同期関数
if err
reject err
else
fulfill res
@masui
masui / register.rb
Created August 25, 2015 12:00
Gyazo1
id = ARGV[0]
time = ARGV[1]
url = ARGV[2]
exit unless id && time && url
time = time.gsub(/_/,' ')
d = Image.find_by(image_id: id)
d.date = Time.zone.parse(time).utc
d.metadata = { 'url' => url }