それでもRubyはお気楽だ
もう10年以上はRubyにお世話になっている。いや20年になるのか?
最初に威力を知ったのは,分割数を数え上げるプログラムを書いた時だ。
例えば,
のようにを分ける場合の数を数え上げるプログラムを書いたのだが,
整数の桁数を全く気にしなくて良いし,配列というよりは集合のようにも使えるArrayが素晴らしく便利であったからだ。もうゾッコンになってしまった。
ただ,職場のお仕着せのWindowsだともうメンテされてないけどRubyのインタプリタがあってそれを入れておけばサクサクだけど,MacOSX上だと困っていてずっとmiというエディタのrubyモードで動かしている。
お昼には
# 巡回置換群の数え上げ szen=10 glst=(1..szen).to_a.permutation(szen).to_a p glst.size # def analyst(spl) gpl=[] dspl=[] begin sgpl=[] we=(spl-dspl)[0] begin sgpl << we we=spl[we-1] end while sgpl.index(we)==nil gpl << sgpl dspl=dspl+sgpl end while dspl.size<spl.size [spl,gpl] end # def chka(lst,n) if lst.max<=n/2 true else false end end # cnt=0 acnt=10 for i in (1..acnt) spl=glst.sample ans=analyst(spl) ans << ans[1].map{|x| x.size}.sort cans=chka(ans[2],szen) if cans==true cnt=cnt+1 end p [ans[0],ans[2],cans] end p cnt.quo(acnt) #
こんなプログラムを書いて遊んでいた。
# 〜------------------------------------ results 12:17:53 u 3628800 [[5, 9, 2, 7, 8, 1, 3, 10, 6, 4], [10], false] [[8, 7, 2, 4, 6, 10, 3, 5, 1, 9], [1, 3, 6], false] [[8, 1, 3, 4, 5, 9, 7, 10, 6, 2], [1, 1, 1, 1, 2, 4], true] [[3, 5, 10, 6, 8, 7, 4, 2, 9, 1], [1, 3, 3, 3], true] [[4, 5, 6, 2, 9, 10, 8, 3, 7, 1], [10], false] [[9, 6, 10, 1, 2, 5, 7, 3, 8, 4], [1, 3, 6], false] [[1, 9, 5, 3, 10, 6, 8, 7, 2, 4], [1, 1, 2, 2, 4], true] [[6, 10, 8, 4, 9, 1, 2, 7, 3, 5], [1, 2, 7], false] [[1, 6, 7, 10, 8, 9, 3, 2, 5, 4], [1, 2, 2, 5], true] [[2, 8, 10, 9, 6, 3, 1, 7, 4, 5], [2, 4, 4], true] (1/2)
所謂,置換の分解だけど,日曜プログラマでもサクサクと仕上がる。
ソースも簡潔で可読性が高いから,暫く経ってから見ても大抵分かるのだ。
でまぁ,そういうのもJuliaでできれば少しお気楽さが増えるか否か。
そこを今年は見極めようかと思う。