Skip to content

Instantly share code, notes, and snippets.

Toshiyuki Masui masui

View GitHub Profile
@masui
masui / raspi_mouse.rb
Last active Aug 29, 2015
Raspberry PiのRubyでマウスホイール信号を取得する
View raspi_mouse.rb
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
a^2 + b^2 == c^2 かつ a+b+c=1000, a < b < c となる a, b, c を求める
View problem9.rb
(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
a^2 + b^2 == c^2 かつ a+b+c=1000, a < b < c となる a, b, c を求める (流石にナイーブすぎて遅いバージョン)
View problem9-slow.rb
(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
1時間以内に解けなければプログラマ失格となってしまう5つの問題
View gist:b688b4275f5d59fea6ec
#
# 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 / slow.rb
Created May 26, 2015
a^2 + b^2 == c^2 かつ a+b+c=1000, a < b < c となる a, b, c を求める
View slow.rb
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:89fa67d47c1925f89329
Last active Aug 29, 2015
1時間以内に解けなければプログラマ失格となってしまう5つの問題(2)
View gist:89fa67d47c1925f89329
#
# 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 / gist:8a5a6ae7e5bb7acf83ee
Last active Aug 29, 2015
1時間以内に解けなければプログラマ失格となってしまう5つの問題(3)
View gist:8a5a6ae7e5bb7acf83ee
#
# 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 Aug 29, 2015
Promiseの例
View promise.coffee
fs = require 'fs'
readFile = (filename, enc) ->
new Promise (fulfill, reject) ->
fs.readFile filename, enc, (err, res) -> # readFile()は非同期関数
if err
reject err
else
fulfill res
View register.rb
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 }
You can’t perform that action at this time.