Skip to content

Instantly share code, notes, and snippets.

View yancya's full-sized avatar
💭
😂

Shinta Koyanagi yancya

💭
😂
View GitHub Profile
@yancya
yancya / diff_load_and_require.txt
Created November 27, 2013 08:57
load と require の違いだよ
panda:~ yancya$ echo 'puts :hoge' > hoge.rb
panda:~ yancya$ ruby -e 'load "./hoge.rb"'
hoge
panda:~ yancya$ ruby -e '10.times{load "./hoge.rb"}'
hoge
hoge
hoge
hoge
hoge
hoge
@yancya
yancya / count.rb
Created December 3, 2013 10:25
POH! Vol.1 ぼくとつ版
# ruby-1.9.3
n, d = gets.split(" ").map(&:to_i)
items = n.times.map{gets.to_i}
limits = d.times.map{gets.to_i}
limits.each do |limit|
indexes = (0..items.size-1).to_a
sums = indexes.product(indexes).select{|n, m| n != m}.map{|n, m| items[n] + items[m]}
puts sums.uniq.reduce(0){|max, sum| max = sum if max < sum && sum <= limit; max}
end
@yancya
yancya / count2.rb
Created December 3, 2013 10:26
POH! Vol.1 マルチスレッド版
n, d = gets.split(" ").map(&:to_i)
items = n.times.map{gets.to_i}
limits = d.times.map{gets.to_i}
def max_pattern(limit, items)
max = 0
items.each_with_index do |first, i|
items.each_with_index do |second, j|
sum = first + second
max = sum if i != j && max < sum && sum <= limit
@yancya
yancya / count3.rb
Last active December 30, 2015 03:09
POH! Vol.1 マルチプロセス版
data = ARGF.read.split("\n")
n, d = data.first.split(" ").map(&:to_i)
items = data[1..n].map(&:to_i)
limits = data[n+1..n+d].map(&:to_i)
patterns = []
until items.empty?
first = items.pop
items.each do |second|
patterns << (first+second)
@yancya
yancya / count4.rb
Last active December 30, 2015 05:08
POH! Vol.1 シンプル版
data = ARGF.read.split("\n")
n, d = data.first.split(" ").map(&:to_i)
items = data[1..n].map(&:to_i)
limits = data[n+1..n+d].map(&:to_i)
patterns = []
until items.empty?
first = items.pop
items.each do |second|
patterns << (first+second)
@yancya
yancya / count5.rb
Created December 5, 2013 01:20
POH! Vol.1 二分探索版
data = ARGF.read.split("\n")
n, d = data.first.split(" ").map(&:to_i)
items = data[1..n].map(&:to_i)
limits = data[n+1..n+d].map(&:to_i)
patterns = []
until items.empty?
first = items.pop
items.each do |second|
patterns << (first+second)
@yancya
yancya / count.sql
Last active January 2, 2016 11:09
"created_at降順でソートしてstatus=xである行が上から何個連続しているのか数えるやつをエスキューエルでやりたいんですが?!?!" https://twitter.com/todesking/status/420400521124409344
SELECT t2.count
FROM (SELECT t1.created_at
,lag(t1.status) over(order by t1.created_at DESC) AS status
,lag(t1.count) over(order by t1.created_at DESC) AS count
,lag(t1.status) over(order by t1.created_at DESC) <> t1.status AS judge
FROM (SELECT status
,created_at
,count(id) over(partition by status order by created_at DESC)
FROM items
) as t1
@yancya
yancya / json_server.sh
Created January 9, 2014 09:41
適当な JSON を返す WEB サーバーを立ち上げるワンライナー
ruby -rsinatra -rjson -e 'get "/" do; %w{hoge fuga moge}.to_json; end'
@yancya
yancya / binary_prefix.rb
Last active January 2, 2016 19:19
簡単な Refinements の例
module BinaryPrefix
refine Fixnum do
UNIT_TYPES = {
kib: 10, # Kibi Byte
mib: 20, # Mebi Byte
gib: 30, # Gibi Byte
tib: 40, # Tebi Byte
pib: 50, # Pebi Byte
eib: 60, # Exbi Byte
zib: 70, # Zebi Byte
@yancya
yancya / only.rb
Created January 22, 2014 09:43
"Ruby、{a: 1, b: 2, c: 3}.only(:a, :c) #=> {a: 1, c: 3} みたいなメソッドないの" https://twitter.com/todesking/status/425922930229248000
h = {a: 1, b: 2, c: 3}
h.define_singleton_method(:only, ->(*syms){self.select{|o| syms.member? o}})
h.only(:a, :c) #=> {:a=>1, :c=>3}
#作るしかないかも