This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'thread' | |
class Workers | |
def initialize(count) | |
@q = Queue.new | |
@count = count | |
@jobs = (0...@count).map.with_index {|id| | |
Thread.new do | |
while (job = @q.pop) | |
total = job.(id, total) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TimeoutError < StandardError; end | |
def timeout(sec) | |
x = Thread.current | |
t = Thread.new do | |
begin | |
sleep sec | |
x.raise TimeoutError.new "Exceed maximum timeout value #{sec}s" | |
rescue e | |
x.raise e | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'strscan' | |
BEGIN_TAGS = %w(<%= <%# <%) | |
END_TAGS = %w(%>) | |
class MiniErb | |
def scan(content) | |
scanner = ::StringScanner.new(content) | |
state = :text | |
until scanner.eos? | |
case state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Forwardable | |
def delegate_methods(obj, *methods) | |
methods.each {|method| delegate_method(obj, method)} | |
end | |
def delegate_method1(obj, method) | |
self.module_eval %Q{ | |
def #{method}(*args, &block) | |
_#{obj}.#{method}(*args, &block) | |
end | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 問題: https://twitter.com/nada_mathclub/status/1256418924737880069 | |
# : vivid * vive = brillante の覆面算を解く | |
# 解答: 62621 * 6267 = 392445807 | |
def solve(v, i, d, e) | |
return nil if v == 0 | |
vivid = [v, i, v, i, d].inject(0) {|sum, x| sum * 10 + x} | |
vive = [v, i, v, e].inject(0) {|sum, x| sum * 10 + x} | |
brillante = Enumerator.new do |e| | |
t = (vivid * vive) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 4,6,6,5,6 を1つずつと四則演算、累乗のみを使って 777を作る | |
def solve(nums) | |
if nums.length === 1 | |
yield({val: nums[0]}) | |
return | |
end | |
len = nums.length | |
len.times do |i| | |
solve(nums[0...i]) do |l| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rec(num, values) | |
if num == 0 | |
values.reverse! | |
puts "#{values.join("+")}=#{values.inject(&:+)}" | |
values.reverse! | |
return | |
end | |
k = 1 | |
begin | |
d = 10 ** k |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
M = 1 << 63 | |
class PQueue | |
def initialize(n) | |
size = 1 | |
size += 1 while (n = n/2) != 0 | |
size = 1 << size | |
@arr = Array.new(size, M) | |
@i = 0 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# 日本語重複問題対応スクリプト | |
find . -type f | | |
while read src; | |
do | |
dist="$(echo $src | nkf -w --ic=utf8-mac -)" | |
if [[ "$dist" != "$src" ]]; then | |
echo mv "$src" "$dist" | |
fi | |
done | sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [l, r) | |
def init | |
@sorted_bucket_items = @items.each_slice(BUCKET_SIZE).to_a.map {|x| x.sort {|a, b| b <=> a}} | |
end | |
# [l, r) | |
def query(l, r, k) | |
lb, ub = [0, 1 << 63] | |
while ub - lb > 1 | |
mid = (ub+lb)/2 |