Minimum number of cells after, before, above and below NxN squares: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add Factor)
Line 39: Line 39:
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
</pre>
</pre>

=={{header|Julia}}==
Set up the upper left quadrant and then mirror it.
<lang julia>function printNbyN(N)
mat = zeros(Int, N, N)
for xy in CartesianIndices(mat)
mat[xy] = minimum(Tuple(xy)) - 1
end
for (rownum, row) in enumerate(eachrow(mat))
rownum > N ÷ 2 && (row .= mat[N - rownum + 1, :])
row[(N+2)÷2:end] .= reverse(row[begin:(N+1)÷2])
for n in row
print(rpad(n, 2))
end
println()
end
end

function testNbyN(sizes)
for n in sizes
println("\nSquare of size $n:")
printNbyN(n)
end
end

testNbyN([10, 9, 2, 1])
</lang>{{out}}
<pre>
Square of size 10:
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0

Square of size 9:
0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0

Square of size 2:
0 0
0 0

Square of size 1:
0
</pre>



=={{header|Ring}}==
=={{header|Ring}}==

Revision as of 10:54, 2 August 2021

Minimum number of cells after, before, above and below NxN squares is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task
Find and show on this page the minimum number of cells after, before, above and below NxN squares, where N = 10



Factor

Works with: Factor version 0.99 2021-06-02

<lang factor>USING: io kernel math math.matrices math.vectors prettyprint sequences ;

square ( n -- matrix )
   [ <cartesian-square-indices> ] keep 1 - dup
   '[ dup sum _ > [ _ v-n vabs ] when infimum ] matrix-map ;

10 square simple-table. nl 9 square simple-table.</lang>

Output:
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0

Julia

Set up the upper left quadrant and then mirror it. <lang julia>function printNbyN(N)

   mat = zeros(Int, N, N)
   for xy in CartesianIndices(mat)
       mat[xy] = minimum(Tuple(xy)) - 1
   end
   for (rownum, row) in enumerate(eachrow(mat))
       rownum > N ÷ 2 && (row .= mat[N - rownum + 1, :])
       row[(N+2)÷2:end] .= reverse(row[begin:(N+1)÷2])
       for n in row
           print(rpad(n, 2))
       end
       println()
   end

end

function testNbyN(sizes)

   for n in sizes
       println("\nSquare of size $n:")
       printNbyN(n)
   end

end

testNbyN([10, 9, 2, 1])

</lang>

Output:
        
Square of size 10:
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0

Square of size 9:
0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0

Square of size 2:
0 0
0 0

Square of size 1:
0


Ring

<lang ring> see "working..." + nl see "Minimum number of cells after, before, above and below NxN squares:" + nl row = 0 cellsMin = []

for n = 1 to 10

   for m = 1 to 10
       cells = []
       add(cells,m-1)
       add(cells,10-m)
       add(cells,n-1)
       add(cells,10-n)
       min = min(cells)
       add(cellsMin,min)
   next

next

ind = 100 for n = 1 to ind

   row++
   see "" + cellsMin[n] + " "
   if row%10 = 0
      see nl
   ok

next

see "done..." + nl </lang>

Output:
working...
Minimum number of cells after, before, above and below NxN squares:
0 0 0 0 0 0 0 0 0 0 
0 1 1 1 1 1 1 1 1 0 
0 1 2 2 2 2 2 2 1 0 
0 1 2 3 3 3 3 2 1 0 
0 1 2 3 4 4 3 2 1 0 
0 1 2 3 4 4 3 2 1 0 
0 1 2 3 3 3 3 2 1 0 
0 1 2 2 2 2 2 2 1 0 
0 1 1 1 1 1 1 1 1 0 
0 0 0 0 0 0 0 0 0 0 
done...