rustedの日記 このページをアンテナに追加 RSSフィード

ruby勉強するよ。

2006-12-20

[][]Amazing Mazes (#31) 08:16 Amazing Mazes (#31) - rustedの日記 を含むブックマーク はてなブックマーク - Amazing Mazes (#31) - rustedの日記

今度は迷路の自動生成(とその解き方)(http://www.rubyquiz.com/quiz31.html)。

All nodes of the maze must be reachable from any point.

まず全てのノードに到達できるようにすること。これは生成後に移動可能な場所をチェックして、いっていないところがあったら穴をあけることで解決した。

Furthermore, let us enforce that only 1 viable solution for the maze exists for any given start/stop (you cannot reach the same destination from 2 different routes).

もう1つ注意点。ただ1つの解になるようにすること、ということなので環ができていたら埋めてみました。

生成した迷路を解くほうは再帰でサクっと書いてみた。

続きを読む

2006-10-19

[]csv 22:45 csv - rustedの日記 を含むブックマーク はてなブックマーク - csv - rustedの日記

Comma Separated Valueのほう。CSVクラスを使って操作してみる。サクっとできた。

require 'CSV'

CSV.open(ARGV[0], "r") do |row|
  row.each_index { |x| print "X", x, "=", row[x], "\n" }
end

GregGreg2007/02/05 03:59If you wanna stop guestbook spammers just confirm url of this page to anti.spam.police@gmail.com with subject:ANTISPAM. Thx.

Greg NilsonGreg Nilson2007/02/05 16:55If you wanna stop guestbook spammers just confirm url of this page to anti.spam.police@gmail.com with subject:ANTISPAM. Thx.

2006-10-07

[]Array.each_index { |i| ... } 10:56 Array.each_index { |i| ... } - rustedの日記 を含むブックマーク はてなブックマーク - Array.each_index { |i| ... } - rustedの日記

while文を延々と使ってきたけど、c言語のfor文のように使えるものも当然あったのでした。

a.each_index { |i| a[i].no = i}

2006-09-27

[]Array.delete_if 20:55 Array.delete_if - rustedの日記 を含むブックマーク はてなブックマーク - Array.delete_if - rustedの日記

ブロックの結果が真になった要素を全て削除する。

@grid.delete_if { |g| g.size != size }

ここではgが@gridの各要素になる。

2006-09-23

[][]Grid Folding (#63) 22:29 Grid Folding (#63) - rustedの日記 を含むブックマーク はてなブックマーク - Grid Folding (#63) - rustedの日記

16×16サイズの紙を指定されたコマンドで半分ずつ折っていく問題(http://www.rubyquiz.com/quiz63.html)。最終的に1×1サイズになったところで、グリッドについていた番号を上から並べて出力する。

この例がわかりやすい。

12

34

fold("RB") => [3, 4, 2, 1]

fold("TL") => [3, 1, 2, 4]

fold("LR") => raise exception for invalid input

コマンドは"T"(上辺を下辺に合わせるように折る。折った後、上辺側が上になる)、"B"(下辺を上辺に~)、"L"(左辺を右辺に~)、"R"(右辺を左辺に~)の4つ。折ったときに上になるほうは並びが反転するけど、下になるほうはそのままなことに注意する必要があるかな。

Extra credit: Make your fold function take an additional input parameter(s), the dimensions of the paper; dimensions should be power-of-2 in order to fold down to one cell.

サイズを指定できるようにするといいらしい。あと最終的に1つのマスにたたみこむために、1辺の長さを2の累乗にするべきとのこと。

続きを読む