Nonoblock: Difference between revisions

→‎{{header|Ruby}}: It moves a change method to the cell form to the String class.
(→‎{{header|Ruby}}: add Class version)
(→‎{{header|Ruby}}: It moves a change method to the cell form to the String class.)
Line 353:
<lang ruby>def nonoblocks(cell, blocks)
raise 'Those blocks will not fit in those cells' if cell < blocks.inject(0,:+) + blocks.size - 1
nblock(cell, blocks, '', result[])
result = []
nblock(cell, blocks, '', result)
result
end
 
Line 366 ⟶ 364:
rest = cell - blocks.inject(:+) - blocks.size + 2
bl, brest = blocks[0], blocks.drop(1)
rest.times.inject(result) do |res, i|
nblock(cell-i-bl-1, brest, position + '.'*i + '#'*bl + '.', resultres)
end
end
Line 427 ⟶ 425:
</pre>
 
'''===Class version:'''===
The output form consulted the one of the python.
<lang ruby>class NonoBlock
def self.cell(cell)
"|#{(['_']*cell).join('|')}|"
end
def initialize(cell, blocks)
raise 'Those blocks will not fit in those cells' if cell < blocks.inject(0,:+) + blocks.size - 1
Line 440 ⟶ 435:
def result(correct=true)
correct ? @result.map{|str| str2cell(str&:nonocell)} : @result
end
Line 457 ⟶ 452:
end
end
end
 
def str2cell(str) # "##.###..##" -> "|A|A|_|B|B|B|_|_|C|C|"
class String
def str2cell(str)nonocell # "##.###..##" -> "|A|A|_|B|B|B|_|_|C|C|"
chr = ('A'..'Z').each
s = str.tr('.','_').gsub(/#+/){|sharp| chr.next * sharp.size}
"|#{s.chars.join('|')}|"
end
Line 470 ⟶ 467:
[10, [8]],
[15, [2, 3, 2, 3]],
[ 5, [2, 3]], ]
conf.each do |cell, blocks|
begin
puts "Configuration:",
"#{NonoBlock('.cell('*cell).nonocell} # #{cell} cells and #{blocks} blocks",
"Possibilities:"
result = NonoBlock.new(cell, blocks).result
Line 484 ⟶ 481:
end
end</lang>
The output form consulted the one of the python.
 
{{out}}
<pre style="height: 64ex; overflow: scroll">
<pre>
Configuration:
|_|_|_|_|_| # 5 cells and [2, 1] blocks
Anonymous user