2007-11-28から1日間の記事一覧

Rubyにおける改行

オブジェクトとメソッドのあいだには改行があってはいけないようです。 ↓コンパイルエラー p(1 + 2)↓もちろんこれはOK p(1+ 2)

配列の順列の列挙

class Array def perm() if self.length <= 1 then [self] else a=self[1..-1].perm; c=[]; a.each { |x| 0.upto(x.length) { |y| c.push( (y==0 ? [] : x[0..y-1]) + [ self[0] ] + x[y..-1]) } } c.uniq end end end "123".split(//).perm.each{ |x| p x }…