webページのスクリーンショットを撮影

phantomjsでやろう http://d.hatena.ne.jp/Jxck/20111102/1320230249 と思って、インストールしたのだが

brew install phantomjs

一緒にwebkit2pngというpythonで書かれたツールがついてきた。


キャプチャしてみる

webkit2png http://shokai.org/blog/


いろいろなサイズでキャプチャできた。
phantomjsでキャプチャすると背景色が指定されていないページが灰色になってしまうが、webkit2pngは白くしてくれた。

ターミナルから印刷する

applescriptで印刷もできるのだが、地獄のような事になっているので原点に帰ってlpr


プリンタ一覧を取得

lpstat -s


USBやsambaで接続したプリンタもでてきた


プリンタを指定してファイルを印刷する

lpr -P プリンタ名 ファイル名

印刷の設定は、そのプリンタの保存されている設定(最後の設定かデフォルトか調べてないが)で適当に印刷されるらしく、とりあえずPDFとpngとgifはそのまま印刷できた。
htmlは印刷しても真っ白だった。いったんpdfにでもしなければならなそう。


印刷キューは

lpstat

で見れる。

diffをファイルに書き出し

commitのdiffをgrepしたいのだが、gitだけで検索する方法がわからなかったのでとりあえず~/diffに全commitのdiffを書きだした

mkdir ~/diff
git log | grep '^commit' | ruby -lane 'puts $F[1].strip' | ruby -lane 'arr=ARGF.to_a.map{|i|i.strip}; for i in 0...(arr.size-1) do a,b=arr[i,i+1]; puts cmd="git diff #{a} #{b} > ~/diff/#{a}"; system cmd end'


あとは

grep 'bug' * --color -A10 -B10

とかやって探す

ある日付のcommitのIDだけ抜き出す

git-id-date.rb

#!/usr/bin/env ruby
require 'date'

if ARGV.size < 1
  STDERR.puts "ruby #{$0} 2011-08-15"
  STDERR.puts "ruby #{$0} 2011-08-15 2011-09-30"
  exit 1
end

date_start = Date.parse(ARGV.shift)
date_end = ARGV.empty? ? Date.today : Date.parse(ARGV.shift)

lines = `git log`.split(/[\r\n]/)

id = nil
date = nil

lines.each do |line|
  if line =~ /^commit\s[a-zA-Z0-9]+$/
    id = line.split(/\s/)[1]
  elsif line =~ /^Date:\s/
    date = Date.parse line.scan(/^Date:\s+(.+)$/).first.first
    puts id if date_start <= date and date <= date_end
    id = nil
    date = nil
  end
end

gitリポジトリ内で、

ruby git-id-date.rb 20110815 20110830

とかすると8/15〜8/30の間のcommit idがでてくる。



ある期間のcommit logを全てファイルに書き出す

mkdir -p ~/tmp/gitdiff
git-id-date.rb 20110815 20110830 | ruby -lane 'puts cmd="git show #{$_} | > ~/tmp/gitdiff/#{$_}";system cmd'

camelcaseとsnakecaseの変換

いろいろ丁寧に変換するようにした

--to_snake
'ShoHashimoto' => 'sho_hashimoto'
'sho_hashimoto' => 'sho_hashimoto'
'shokai' => 'shokai'
'SHOKAI' => 'shokai'
'This is a  pen' => 'this_is_a_pen'
'EventMachine::HTTPRequest' => 'event_machine_httprequest'
'tEst' => 't_est'
--to_camel
'ShoHashimoto' => 'ShoHashimoto'
'sho_hashimoto' => 'ShoHashimoto'
'shokai' => 'Shokai'
'SHOKAI' => 'SHOKAI'
'This is a  pen' => 'ThisIsAPen'
'EventMachine::HTTPRequest' => 'EventMachine::HTTPRequest'
'tEst' => 'TEst'
#!/usr/bin/env ruby

class String
  def to_snake
    ptn = /[A-Z\s]*[^A-Z]*/
    self =~ ptn ? self.scan(ptn).map{|i|
      i.gsub(/[\s:]+/,'_').downcase
    }.join('_').gsub(/__+/,'_').sub(/_$/,'') : self
  end
  def to_camel
    self.split(/[_\s]+/).map{|i|
      a,b,c = i.split(/^(.)/)
      "#{b.upcase}#{c}"
    }.join('')
  end
end

# puts 'ShoHashimoto'.to_snake
# puts 'sho_hashimoto'.to_camel

[:to_snake, :to_camel].each do |mes|
  puts "--#{mes}"
  ['ShoHashimoto', 'sho_hashimoto', 'shokai', 'SHOKAI',
   'This is a  pen', 'EventMachine::HTTPRequest', 'tEst'].each do |s|
    puts "'#{s}' => '#{s.method(mes).call}'"
  end
end

8queen問題

http://ja.wikipedia.org/wiki/エイト・クイーン

すげー悩んだけどわりと綺麗にできた。
再帰+lambda渡しは再帰を巻き戻さないで良いので書きやすい。
変な書き方しないようにしたけど20行きった。

#!/usr/bin/env ruby
def put(size=8, queens=[], &block)
  block.call(queens) if block and queens.size >= size
  (queens.empty? ? 1 : queens.last[:x]).upto(size).each do |x|
    1.upto(size).each do |y|
      q = {:x => x, :y => y}
      next if queens.map{|i|
        q[:x] == i[:x] or q[:y] == i[:y] or q[:x]+q[:y] == i[:x]+i[:y] or q[:x]-q[:y] == i[:x]-i[:y] 
      }.include? true
      queens_ = queens.clone
      queens_.push q
      put(size, queens_, &block)
    end
  end
end

put(ARGV.empty? ? 8 : ARGV.shift.to_i){|queens|
  puts queens.map{|q| "[#{q[:x]},#{q[:y]}]"}.join('')
}


結果。queenを置くべき座標のリストがでてくる。全92通り。

[1,1][2,5][3,8][4,6][5,3][6,7][7,2][8,4]
[1,1][2,6][3,8][4,3][5,7][6,4][7,2][8,5]
[1,1][2,7][3,4][4,6][5,8][6,2][7,5][8,3]
[1,1][2,7][3,5][4,8][5,2][6,4][7,6][8,3]
[1,2][2,4][3,6][4,8][5,3][6,1][7,7][8,5]
[1,2][2,5][3,7][4,1][5,3][6,8][7,6][8,4]
[1,2][2,5][3,7][4,4][5,1][6,8][7,6][8,3]
[1,2][2,6][3,1][4,7][5,4][6,8][7,3][8,5]
[1,2][2,6][3,8][4,3][5,1][6,4][7,7][8,5]
[1,2][2,7][3,3][4,6][5,8][6,5][7,1][8,4]
[1,2][2,7][3,5][4,8][5,1][6,4][7,6][8,3]
[1,2][2,8][3,6][4,1][5,3][6,5][7,7][8,4]
[1,3][2,1][3,7][4,5][5,8][6,2][7,4][8,6]
[1,3][2,5][3,2][4,8][5,1][6,7][7,4][8,6]
[1,3][2,5][3,2][4,8][5,6][6,4][7,7][8,1]
[1,3][2,5][3,7][4,1][5,4][6,2][7,8][8,6]
[1,3][2,5][3,8][4,4][5,1][6,7][7,2][8,6]
[1,3][2,6][3,2][4,5][5,8][6,1][7,7][8,4]
[1,3][2,6][3,2][4,7][5,1][6,4][7,8][8,5]
[1,3][2,6][3,2][4,7][5,5][6,1][7,8][8,4]
[1,3][2,6][3,4][4,1][5,8][6,5][7,7][8,2]
[1,3][2,6][3,4][4,2][5,8][6,5][7,7][8,1]
[1,3][2,6][3,8][4,1][5,4][6,7][7,5][8,2]
[1,3][2,6][3,8][4,1][5,5][6,7][7,2][8,4]
[1,3][2,6][3,8][4,2][5,4][6,1][7,7][8,5]
[1,3][2,7][3,2][4,8][5,5][6,1][7,4][8,6]
[1,3][2,7][3,2][4,8][5,6][6,4][7,1][8,5]
[1,3][2,8][3,4][4,7][5,1][6,6][7,2][8,5]
[1,4][2,1][3,5][4,8][5,2][6,7][7,3][8,6]
[1,4][2,1][3,5][4,8][5,6][6,3][7,7][8,2]
[1,4][2,2][3,5][4,8][5,6][6,1][7,3][8,7]
[1,4][2,2][3,7][4,3][5,6][6,8][7,1][8,5]
[1,4][2,2][3,7][4,3][5,6][6,8][7,5][8,1]
[1,4][2,2][3,7][4,5][5,1][6,8][7,6][8,3]
[1,4][2,2][3,8][4,5][5,7][6,1][7,3][8,6]
[1,4][2,2][3,8][4,6][5,1][6,3][7,5][8,7]
[1,4][2,6][3,1][4,5][5,2][6,8][7,3][8,7]
[1,4][2,6][3,8][4,2][5,7][6,1][7,3][8,5]
[1,4][2,6][3,8][4,3][5,1][6,7][7,5][8,2]
[1,4][2,7][3,1][4,8][5,5][6,2][7,6][8,3]
[1,4][2,7][3,3][4,8][5,2][6,5][7,1][8,6]
[1,4][2,7][3,5][4,2][5,6][6,1][7,3][8,8]
[1,4][2,7][3,5][4,3][5,1][6,6][7,8][8,2]
[1,4][2,8][3,1][4,3][5,6][6,2][7,7][8,5]
[1,4][2,8][3,1][4,5][5,7][6,2][7,6][8,3]
[1,4][2,8][3,5][4,3][5,1][6,7][7,2][8,6]
[1,5][2,1][3,4][4,6][5,8][6,2][7,7][8,3]
[1,5][2,1][3,8][4,4][5,2][6,7][7,3][8,6]
[1,5][2,1][3,8][4,6][5,3][6,7][7,2][8,4]
[1,5][2,2][3,4][4,6][5,8][6,3][7,1][8,7]
[1,5][2,2][3,4][4,7][5,3][6,8][7,6][8,1]
[1,5][2,2][3,6][4,1][5,7][6,4][7,8][8,3]
[1,5][2,2][3,8][4,1][5,4][6,7][7,3][8,6]
[1,5][2,3][3,1][4,6][5,8][6,2][7,4][8,7]
[1,5][2,3][3,1][4,7][5,2][6,8][7,6][8,4]
[1,5][2,3][3,8][4,4][5,7][6,1][7,6][8,2]
[1,5][2,7][3,1][4,3][5,8][6,6][7,4][8,2]
[1,5][2,7][3,1][4,4][5,2][6,8][7,6][8,3]
[1,5][2,7][3,2][4,4][5,8][6,1][7,3][8,6]
[1,5][2,7][3,2][4,6][5,3][6,1][7,4][8,8]
[1,5][2,7][3,2][4,6][5,3][6,1][7,8][8,4]
[1,5][2,7][3,4][4,1][5,3][6,8][7,6][8,2]
[1,5][2,8][3,4][4,1][5,3][6,6][7,2][8,7]
[1,5][2,8][3,4][4,1][5,7][6,2][7,6][8,3]
[1,6][2,1][3,5][4,2][5,8][6,3][7,7][8,4]
[1,6][2,2][3,7][4,1][5,3][6,5][7,8][8,4]
[1,6][2,2][3,7][4,1][5,4][6,8][7,5][8,3]
[1,6][2,3][3,1][4,7][5,5][6,8][7,2][8,4]
[1,6][2,3][3,1][4,8][5,4][6,2][7,7][8,5]
[1,6][2,3][3,1][4,8][5,5][6,2][7,4][8,7]
[1,6][2,3][3,5][4,7][5,1][6,4][7,2][8,8]
[1,6][2,3][3,5][4,8][5,1][6,4][7,2][8,7]
[1,6][2,3][3,7][4,2][5,4][6,8][7,1][8,5]
[1,6][2,3][3,7][4,2][5,8][6,5][7,1][8,4]
[1,6][2,3][3,7][4,4][5,1][6,8][7,2][8,5]
[1,6][2,4][3,1][4,5][5,8][6,2][7,7][8,3]
[1,6][2,4][3,2][4,8][5,5][6,7][7,1][8,3]
[1,6][2,4][3,7][4,1][5,3][6,5][7,2][8,8]
[1,6][2,4][3,7][4,1][5,8][6,2][7,5][8,3]
[1,6][2,8][3,2][4,4][5,1][6,7][7,5][8,3]
[1,7][2,1][3,3][4,8][5,6][6,4][7,2][8,5]
[1,7][2,2][3,4][4,1][5,8][6,5][7,3][8,6]
[1,7][2,2][3,6][4,3][5,1][6,4][7,8][8,5]
[1,7][2,3][3,1][4,6][5,8][6,5][7,2][8,4]
[1,7][2,3][3,8][4,2][5,5][6,1][7,6][8,4]
[1,7][2,4][3,2][4,5][5,8][6,1][7,3][8,6]
[1,7][2,4][3,2][4,8][5,6][6,1][7,3][8,5]
[1,7][2,5][3,3][4,1][5,6][6,8][7,2][8,4]
[1,8][2,2][3,4][4,1][5,7][6,5][7,3][8,6]
[1,8][2,2][3,5][4,3][5,1][6,7][7,4][8,6]
[1,8][2,3][3,1][4,6][5,2][6,5][7,7][8,4]
[1,8][2,4][3,1][4,3][5,6][6,2][7,7][8,5]

清一色の配牌パターン数を調べる

mlab(masui lab)はmajan labになりました。
清一色の配牌パターン数を調べる。
1が0〜4枚、2が0〜4枚...9が0〜4枚、で合計14枚になるのが正しい配牌なので、ならば富豪的に5進数を使おう

chin-itsu.rb

for i in 0..(("4"*9).to_i(5))
  res = i.to_s(5)
  puts '0'*(9-res.size)+res if res.split('').map{|j| j.to_i}.inject{|a,b| a+b} == 14
end

実行すると、1から9まで4枚ずつある牌が合計14枚配られたパターンが出る

000002444
000003344
000003434
000003443
000004244
000004334
000004343
000004424
000004433
000004442
000011444
000012344
000012434
000012443
000013244
000013334
000013343
000013424
000013433
000013442

数える

ruby chin-itsu.rb | wc -l

118800通りだった。

数を数えるだけver

count = 0
last = (("4"*9).to_i(5))
for i in 0..last
  count += 1 if i.to_s(5).split('').map{|j| j.to_i}.inject{|a,b| a+b} == 14
end
puts count

やはり118800通りだった。