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

From Rosetta Code
Content added Content deleted
m (combine 2 functions)
m (→‎{{header|REXX}}: changed title wording and a variable name.)
Line 332: Line 332:
=={{header|REXX}}==
=={{header|REXX}}==
This REXX version automatically adjusts the width of each (cell) number displayed so that all numbers are aligned.
This REXX version automatically adjusts the width of each (cell) number displayed so that all numbers are aligned.
<lang rexx>/*REXX pgm finds/shows the minimum # of cells after, before, above, & below a NxN square*/
<lang rexx>/*REXX pgm finds the minimum# of cells after, before, above, & below a NxN square matrix*/
parse arg $ /*obtain optional arguments from the CL*/
parse arg $ /*obtain optional arguments from the CL*/
if $='' | $="," then $= 10 9 2 1 /*Not specified? Then use the default.*/
if $='' | $="," then $= 10 9 2 1 /*Not specified? Then use the default.*/
@title= ' the mimimum number of cells after, before, above, and below a '
do j=1 for words($); g= word($, j) /*process each of the squares specified*/
w= length( (g-1) % 2) /*width of largest number to be shown. */
say center(@title g"x"g ' square matrix ', 86) /*center title of output to be shown. */
say center('', 86, '─') /*display a separator line below title.*/


do j=1 for words($) /*process each of the squares specified*/
do r=0 for g /*process output for a NxN sq. matrix*/
x= word($, j) /*obtain the next square to process. */
_= left('', max(1, 85%2 ) - g ) /*compute indentation output centering.*/
do c=0 for g
w= length( (x-1) % 2) /*calculate the width of the largest #.*/
_= _ right( min(r, c, g-r-1, g-c-1), w) /*construct a row of the output matrix.*/
title= 'the minimum number of cells after, before, above, and below a ' x "x" x 'square'
say center(' 'title" ", 79) /*center title of output to be shown. */
say center('', 79, '─') /*display a separator line below title.*/

do r=0 for x /*process output for a N x N square. */
_= left('', max(1, 77%2 ) - x ) /*compute indentation output centering.*/
do c=0 for x
_= _ right( min(r, c, x-r-1, x-c-1), w) /*construct a row of the output square.*/
end /*c*/
end /*c*/
say _ /*display a row of the output square. */
say _ /*display a row of the output square. */
Line 355: Line 353:
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
the mimimum number of cells after, before, above, and below a 10 x 10 square
the mimimum number of cells after, before, above, and below a 10x10 square matrix
──────────────────────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────────────────
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 1 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 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 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 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 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 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




the mimimum number of cells after, before, above, and below a 9 x 9 square
the mimimum number of cells after, before, above, and below a 9x9 square matrix
──────────────────────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────────────────
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 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 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 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 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




the mimimum number of cells after, before, above, and below a 2 x 2 square
the mimimum number of cells after, before, above, and below a 2x2 square matrix
──────────────────────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────────────────
0 0
0 0
0 0
0 0




the mimimum number of cells after, before, above, and below a 1 x 1 square
the mimimum number of cells after, before, above, and below a 1x1 square matrix
──────────────────────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────────────────
0
0
</pre>
</pre>



Revision as of 17:32, 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   N×N   squares,   where   N = 10.

ALGOL 68

Translation of: Wren

As with the Algol W version, the cells are printed as they are calculated. <lang algol68>BEGIN # show the minimum number of cells above, below, before and after each #

     # cell in a suare matrix                                               #
   PROC min = ( INT a, b )INT: IF a < b THEN a ELSE b FI;
   PROC print min cells = ( INT n )VOID: 
        BEGIN
           print( ( "Minimum number of cells after, before, above and below "
                  , whole( n, 0 )
                  , " x "
                  , whole( n, 0 )
                  , " square:"
                  , newline
                  )
                );
           FOR r FROM 0 TO n - 1 DO
               FOR c FROM 0 TO n - 1 DO print( ( whole( min( n-r-1, min( r, min( c, n-c-1 ) ) ), 0 ), " " ) ) OD;
               print( ( newline ) )
           OD
        END # print min cells # ;

   []INT tests = ( 10, 9, 2, 1 );
   FOR i FROM LWB tests TO UPB tests DO
       print min cells( tests[ i ] );
       print( ( newline ) )
   OD

END</lang>

Output:
Minimum number of cells after, before, above and below 10 x 10 square:
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

Minimum number of cells after, before, above and below 9 x 9 square:
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

Minimum number of cells after, before, above and below 2 x 2 square:
0 0
0 0

Minimum number of cells after, before, above and below 1 x 1 square:
0

ALGOL W

Translation of: Wren

This version avoids generating an explicit list of elements for each row in the matrix and just prints the elements as they are calculated. <lang algolw>begin % show the minimum number of cells above, below, before and after each %

     % cell in a square matrix                                              %
   integer procedure min4( integer value a, b, c, d ) ;
   begin
       integer m;
       m := a;
       if b < m then m := b;
       if c < m then m := c;
       if d < m then m := d;
       m
   end min4 ;
   procedure printMinCells ( integer value n ) ; 
   begin
       write( i_w := 1, s_w := 0, "Minimum number of cells after, before, above and below ", n, " x ", n, " square:" );
       write();
       for r := 0 until n - 1 do begin
           for c := 0 until n - 1 do writeon( i_w := 1, s_w := 1, min4( n-r-1, r, c, n-c-1 ) );
           write()
       end for_r
   end printMinCells ;

   for n := 10, 9, 2, 1 do printMinCells( n )

end.</lang>

Output:
Minimum number of cells after, before, above and below 10 x 10 square:
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

Minimum number of cells after, before, above and below 9 x 9 square:
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

Minimum number of cells after, before, above and below 2 x 2 square:
0 0
0 0

Minimum number of cells after, before, above and below 1 x 1 square:
0

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

Go

Translation of: Wren

<lang go>package main

import "fmt"

func printMinCells(n int) {

   fmt.Printf("Minimum number of cells after, before, above and below %d x %d square:\n", n, n)
   for r := 0; r < n; r++ {
       cells := make([]int, n)
       for c := 0; c < n; c++ {
           nums := []int{n - r - 1, r, c, n - c - 1}
           min := n
           for _, num := range nums {
               if num < min {
                   min = num
               }
           }
           cells[c] = min
       }
       fmt.Printf("%d \n", cells)
   }

}

func main() {

   for _, n := range []int{10, 9, 2, 1} {
       printMinCells(n)
       fmt.Println()
   }

}</lang>

Output:
Minimum number of cells after, before, above and below 10 x 10 square:
[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] 

Minimum number of cells after, before, above and below 9 x 9 square:
[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] 

Minimum number of cells after, before, above and below 2 x 2 square:
[0 0] 
[0 0] 

Minimum number of cells after, before, above and below 1 x 1 square:
[0] 

Julia

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

   for N in sizes
       println("\nSquare of size $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])
           println(join(row, " "))
       end
   end

end

printNbyN([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

Nim

Translation of: Go

<lang Nim>import strutils

proc printMinCells(n: Positive) =

 echo "Minimum number of cells after, before, above and below $1 x $1 square:".format(n)
 var cells = newSeq[int](n)
 for r in 0..<n:
   for c in 0..<n:
     cells[c] = min([n - r - 1, r, c, n - c - 1])
   echo cells.join(" ")

when isMainModule:

 for n in [10, 9, 2, 1]:
   printMinCells(n)
   echo()</lang>
Output:
Minimum number of cells after, before, above and below 10 x 10 square:
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

Minimum number of cells after, before, above and below 9 x 9 square:
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

Minimum number of cells after, before, above and below 2 x 2 square:
0 0
0 0

Minimum number of cells after, before, above and below 1 x 1 square:
0

REXX

This REXX version automatically adjusts the width of each (cell) number displayed so that all numbers are aligned. <lang rexx>/*REXX pgm finds the minimum# of cells after, before, above, & below a NxN square matrix*/ parse arg $ /*obtain optional arguments from the CL*/ if $= | $="," then $= 10 9 2 1 /*Not specified? Then use the default.*/

            @title= ' the mimimum number of cells after, before, above, and below a '
 do j=1  for words($);     g= word($, j)        /*process each of the squares specified*/
 w= length( (g-1) % 2)                          /*width of largest number to be shown. */
 say center(@title g"x"g ' square matrix ', 86) /*center title of output to be shown.  */
 say center(,    86, '─')                     /*display a separator line below title.*/
    do     r=0  for g                           /*process output for a  NxN  sq. matrix*/
    _= left(, max(1, 85%2 ) - g )             /*compute indentation output centering.*/
        do c=0  for g
        _= _ right( min(r, c, g-r-1, g-c-1), w) /*construct a row of the output matrix.*/
        end   /*c*/
    say _                                       /*display a row of the output square.  */
    end       /*r*/
  say;  say                                     /*display 2 blank lines between outputs*/
  end         /*j*/                             /*stick a fork in it,  we're all done. */</lang>
output   when using the default inputs:
 the mimimum number of cells after, before, above, and below a  10x10  square matrix
──────────────────────────────────────────────────────────────────────────────────────
                                 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


  the mimimum number of cells after, before, above, and below a  9x9  square matrix
──────────────────────────────────────────────────────────────────────────────────────
                                  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


  the mimimum number of cells after, before, above, and below a  2x2  square matrix
──────────────────────────────────────────────────────────────────────────────────────
                                         0 0
                                         0 0


  the mimimum number of cells after, before, above, and below a  1x1  square matrix
──────────────────────────────────────────────────────────────────────────────────────
                                          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...

Wren

Library: Wren-math
Library: Wren-fmt

<lang ecmascript>import "/math" for Nums import "/fmt" for Fmt

var printMinCells = Fn.new { |n|

   System.print("Minimum number of cells after, before, above and below %(n) x %(n) square:")
   for (r in 0...n) {
       var cells = List.filled(n, 0)
       for (c in 0...n) cells[c] = Nums.min([n-r-1, r, c, n-c-1])
       Fmt.print("$d", cells)
   }

}

for (n in [10, 9, 2, 1]) {

   printMinCells.call(n)
   System.print()

}</lang>

Output:
Minimum number of cells after, before, above and below 10 x 10 square:
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

Minimum number of cells after, before, above and below 9 x 9 square:
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

Minimum number of cells after, before, above and below 2 x 2 square:
0 0
0 0

Minimum number of cells after, before, above and below 1 x 1 square:
0