Comment detail

あみだくじ (Nested Flatten)

最初の行の状態を1行読む毎に書き換えてます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def amida(prob)
  prob.split("\n").inject do |r,l|
    l.split(//).each_with_index do |e,i|
      next unless e=='-'
      r[i-1..i+1]=r[i-1..i+1].reverse
    end
    r
  end
end


prob=<<-EOD
A B C D E
| | |-| |
|-| | |-|
| |-| |-|
|-| |-| |
|-| | | |
EOD

puts prob
puts amida(prob)

ありゃ、ログインしてなかった・・。

Index

Feed

Other

Link

Pathtraq

loading...