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

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 8:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F min_cells_matrix(siz)
R (0 .< siz).map(row -> (0 .< @siz).map(col -> min(@row, col, @@siz - @row - 1, @@siz - col - 1)))
 
Line 19:
 
L(siz) [23, 10, 9, 2, 1]
display_matrix(min_cells_matrix(siz))</langsyntaxhighlight>
 
{{out}}
Line 83:
{{Trans|Wren}}
As with the Algol W version, the cells are printed as they are calculated. Ensures the counts are shown in the same width.
<langsyntaxhighlight lang="algol68">BEGIN # show the minimum number of cells above, below, before and after each #
# cell in a suare matrix #
 
Line 117:
OD
 
END</langsyntaxhighlight>
{{out}}
<pre>
Line 177:
{{Trans|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. The elements are all shown in the same field width.
<langsyntaxhighlight lang="algolw">begin % show the minimum number of cells above, below, before and after each %
% cell in a square matrix %
 
Line 205:
for n := 10, 9, 2, 1 do printMinCells( n )
 
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 263:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight lang="apl">n_by_n ← (⌽⌊⊖)∘(∘.⌊⍨¯1+⍳)
n_by_n¨ 2 3 9 10</langsyntaxhighlight>
{{out}}
<pre>┌───┬─────┬─────────────────┬───────────────────┐
Line 280:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">gridSize := 10
 
grid := []
Line 296:
result .= "`n"
}
MsgBox % result</langsyntaxhighlight>
{{out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 310:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MINIMUM_NUMBER_OF_CELLS_AFTER_BEFORE_ABOVE_AND_BELOW_NXN_SQUARES.AWK
BEGIN {
Line 327:
}
function min(x,y) { return((x < y) ? x : y) }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 394:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="basic256">function min(a, b)
if a<=b then return a else return b
end function
Line 408:
 
call minab(10)
end</langsyntaxhighlight>
{{out}}
<pre>
Line 418:
{{works with|QuickBasic}}
{{trans|FreeBASIC}}
<langsyntaxhighlight QBasiclang="qbasic">DECLARE FUNCTION min! (a!, b!)
DECLARE SUB minab (n!)
 
Line 436:
PRINT
NEXT i
END SUB</langsyntaxhighlight>
{{out}}
<pre>
Line 444:
==={{header|True BASIC}}===
{{trans|QBasic}}
<langsyntaxhighlight lang="qbasic">FUNCTION min (a, b)
IF a <= b THEN LET min = a ELSE LET min = b
END FUNCTION
Line 458:
 
CALL minab (10)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 466:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">minab(10)
end
 
Line 476:
print
next i
end sub</langsyntaxhighlight>
{{out}}
<pre>
Line 484:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let min(a,b) = a<b -> a, b
Line 495:
$)
let start() be minNbyN(10, 3)</langsyntaxhighlight>
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 509:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">NByN ← ⌊⌜˜ ⌽⊸⌊∘↕
NByN¨ 2‿3‿9‿10</langsyntaxhighlight>
{{out}}
<pre>┌─
Line 529:
=={{header|C}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
 
Line 548:
minab(10);
return 0;
}</langsyntaxhighlight>
{{out}}<pre>
0 0 0 0 0 0 0 0 0 0
Line 563:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">min = proc [T: type] (a, b: T) returns (T)
where T has lt: proctype (T,T) returns (bool)
if a<b
Line 598:
start_up = proc ()
print_table(stream$primary_output(), min_n_by_n(10))
end start_up</langsyntaxhighlight>
{{out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 612:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. MINIMUM-CELLS-N-BY-N.
Line 656:
CHECK-MINIMUM.
IF ITEM IS LESS THAN MIN, MOVE ITEM TO MIN.</langsyntaxhighlight>
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 677:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">=LAMBDA(n,
LET(
lastIndex, n - 1,
Line 696:
)(SEQUENCE(n, n, 0, 1))
)
)</langsyntaxhighlight>
 
{{Out}}
Line 1,058:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Minimum number of cells after, before, above and below NxN squares. Nigel Galloway: August 1st., 2021
printfn "%A" (Array2D.init 10 10 (fun n g->List.min [n;g;9-n;9-g]))
printfn "\n%A" (Array2D.init 9 9 (fun n g->List.min [n;g;8-n;8-g]))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,089:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: io kernel math math.matrices math.vectors prettyprint
sequences ;
 
Line 1,096:
'[ dup sum _ > [ _ v-n vabs ] when infimum ] matrix-map ;
 
{ 10 9 2 1 } [ square simple-table. nl ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 1,127:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">
Func Min(a, b) = if a<=b then a else b fi.;
n:=10;
Line 1,133:
[x]:= [<i=1,n> <j=1,n> Min(Min(i-1,n-i),Min(j-1,n-j))];
[x];
</syntaxhighlight>
</lang>
{{out}}<pre>
[[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, `
Line 1,148:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define min(a, b) Iif(a<=b,a,b)
 
sub minab( n as uinteger )
Line 1,159:
end sub
 
minab(10)</langsyntaxhighlight>
{{out}}<pre>
0 0 0 0 0 0 0 0 0 0
Line 1,175:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,206:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,267:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 N = 10
20 FOR I = 0 TO N - 1
30 IF I < N - 1 - I THEN DI = I ELSE DI = N - 1 - I
Line 1,276:
80 NEXT J
90 PRINT
100 NEXT I</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List.Split (chunksOf)
 
----------- SHORTEST DISTANCES TO EDGE OF MATRIX ---------
Line 1,304:
let w = (succ . maximum) $ fmap (length . show) =<< m
rjust n c = (drop . length) <*> (replicate n c <>)
in unlines (unwords . fmap (rjust w ' ' . show) <$> m)</langsyntaxhighlight>
{{Out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 1,333:
 
or in terms of Data.Matrix:
<langsyntaxhighlight lang="haskell">import Data.Matrix ( matrix, Matrix )
 
distanceToEdge :: Int -> Matrix Int
Line 1,344:
main :: IO ()
main =
mapM_ print $ distanceToEdge <$> [10, 9, 2, 1]</langsyntaxhighlight>
{{Out}}
<pre>┌ ┐
Line 1,378:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">nByN=: (|."1<.|.)@(<./~@i.)
nByN each 2 3 9 10</langsyntaxhighlight>
{{out}}
<pre>┌───┬─────┬─────────────────┬───────────────────┐
Line 1,395:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function printNbyN(sizes)
for N in sizes
mat = zeros(Int, N, N)
Line 1,408:
printNbyN([23, 10, 9, 2, 1])
 
</langsyntaxhighlight>{{out}}
<pre>
Minimum number of cells after, before, above and below 23 x 23 square:
Line 1,472:
 
=={{header|MiniZinc}}==
<syntaxhighlight lang="minizinc">
<lang MiniZinc>
%Minimum number of cells after, before, above and below NxN squares. Nigel Galloway, August 3rd., 2021
int: Size=10; int: S=Size-1; set of int: N=0..S;
array[N,N] of var int: G = array2d(N,N,[min([n,g,S-n,S-g])|n,g in N]);
output([show2d(G)])
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,495:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE MinNByN;
FROM InOut IMPORT WriteCard, WriteLn;
 
Line 1,518:
BEGIN
minNbyN(10, 3);
END MinNByN.</langsyntaxhighlight>
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 1,533:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import strutils
 
proc printMinCells(n: Positive) =
Line 1,546:
for n in [10, 9, 2, 1]:
printMinCells(n)
echo()</langsyntaxhighlight>
 
{{out}}
Line 1,580:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">
n=10
matrix(n,n,i,j,min(min(i-1,n-i),min(j-1,n-j)))
</syntaxhighlight>
</lang>
{{out}}<pre>
[0 0 0 0 0 0 0 0 0 0]
Line 1,608:
=={{header|Pascal}}==
Using symmetry within row and col.Fill only the middle and let the values before in place.
<langsyntaxhighlight lang="pascal">program mindistance;
{$IFDEF FPC} //used fpc 3.2.1
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON}
Line 1,693:
Test(1);
end.
</syntaxhighlight>
</lang>
{{out}}
<pre style="width: 800px; height: 480px>
Line 1,750:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util qw( max min );
Line 1,762:
}
print "\n";
}</langsyntaxhighlight>
{{out}}
<pre>0 x 0 distance to nearest edge:
Line 1,818:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">distance_to_edge</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 1,830:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">23</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span><span style="color: #000000;">distance_to_edge</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,885:
</pre>
Although I rather like it the way it is, you could argue there should be more spacing on the 23x23, if you insist do this before the loops and use fmt on the innermost line:
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%%%dd"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)))+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
or maybe just (good for n<=200 whereas the above goes on and on to "%4d", etc.)
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">20</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PILOT}}==
<langsyntaxhighlight lang="pilot">C :size=10
:y=0
*line
Line 1,912:
C :y=y+1
J (y<size):*line
E :</langsyntaxhighlight>
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 1,926:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def min_cells_matrix(siz):
return [[min(row, col, siz - row - 1, siz - col - 1) for col in range(siz)] for row in range(siz)]
 
Line 1,942:
if __name__ == "__main__":
test_min_mat()
</langsyntaxhighlight>{{out}}
<pre>
Minimum number of cells after, before, above and below 23 x 23 square:
Line 2,002:
Or, disentangling computation from IO (separating model from display), and composing from generics:
 
<langsyntaxhighlight lang="python">'''Distance to edge of matrix'''
 
from itertools import chain, product
Line 2,067:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 2,096:
 
and in terms of a generalized matrix function:
<langsyntaxhighlight lang="python">'''Minimum distances to edge of matrix'''
 
from itertools import chain
Line 2,161:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 2,190:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub distance-to-edge (\N) {
my $c = ceiling N / 2;
my $f = floor N / 2;
Line 2,205:
say "\n$_ x $_ distance to nearest edge:";
.fmt("%{$max}d").say for @dte;
}</langsyntaxhighlight>
{{out}}
<pre>0 x 0 distance to nearest edge:
Line 2,262:
=={{header|REXX}}==
This REXX version automatically adjusts the width of each (cell) number displayed so that all displayed numbers are aligned.
<langsyntaxhighlight 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 $= 21 10 9 2 1 /*Not specified? Then use the default.*/
Line 2,280:
 
say; say /*display 2 blank lines between outputs*/
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,347:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Minimum number of cells after, before, above and below NxN squares:" + nl
Line 2,375:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,394:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def dist2edge(n)
width = (n/2).to_s.size+1
m = n-1
Line 2,402:
end
puts dist2edge(10)</langsyntaxhighlight>
{{out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 2,418:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">fn print_min_cells(n int) {
println("Minimum number of cells after, before, above and below $n x $n square:")
for r in 0..n {
Line 2,441:
println('')
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,451:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Nums
import "/fmt" for Fmt
 
Line 2,467:
printMinCells.call(n)
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 2,528:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func Min(A, B);
int A, B;
return if A<B then A else B;
Line 2,542:
CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits