Minimum number of cells after, before, above and below NxN squares: Difference between revisions
Minimum number of cells after, before, above and below NxN squares (view source)
Revision as of 22:41, 27 August 2022
, 1 year agosyntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 8:
{{trans|Python}}
<
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))</
{{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.
<
# cell in a suare matrix #
Line 117:
OD
END</
{{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.
<
% cell in a square matrix %
Line 205:
for n := 10, 9, 2, 1 do printMinCells( n )
end.</
{{out}}
<pre>
Line 263:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
n_by_n¨ 2 3 9 10</
{{out}}
<pre>┌───┬─────┬─────────────────┬───────────────────┐
Line 280:
=={{header|AutoHotkey}}==
<
grid := []
Line 296:
result .= "`n"
}
MsgBox % result</
{{out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 310:
=={{header|AWK}}==
<syntaxhighlight 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>
{{out}}
<pre>
Line 394:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<
if a<=b then return a else return b
end function
Line 408:
call minab(10)
end</
{{out}}
<pre>
Line 418:
{{works with|QuickBasic}}
{{trans|FreeBASIC}}
<
DECLARE SUB minab (n!)
Line 436:
PRINT
NEXT i
END SUB</
{{out}}
<pre>
Line 444:
==={{header|True BASIC}}===
{{trans|QBasic}}
<
IF a <= b THEN LET min = a ELSE LET min = b
END FUNCTION
Line 458:
CALL minab (10)
END</
{{out}}
<pre>
Line 466:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<
end
Line 476:
print
next i
end sub</
{{out}}
<pre>
Line 484:
=={{header|BCPL}}==
<
let min(a,b) = a<b -> a, b
Line 495:
$)
let start() be minNbyN(10, 3)</
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 509:
=={{header|BQN}}==
<
NByN¨ 2‿3‿9‿10</
{{out}}
<pre>┌─
Line 529:
=={{header|C}}==
{{trans|FreeBASIC}}
<
#include<stdlib.h>
Line 548:
minab(10);
return 0;
}</
{{out}}<pre>
0 0 0 0 0 0 0 0 0 0
Line 563:
=={{header|CLU}}==
<
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</
{{out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 612:
=={{header|COBOL}}==
<
PROGRAM-ID. MINIMUM-CELLS-N-BY-N.
Line 656:
CHECK-MINIMUM.
IF ITEM IS LESS THAN MIN, MOVE ITEM TO MIN.</
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 677:
{{Works with|Office 365 betas 2021}}
<
LET(
lastIndex, n - 1,
Line 696:
)(SEQUENCE(n, n, 0, 1))
)
)</
{{Out}}
Line 1,058:
=={{header|F_Sharp|F#}}==
<
// 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>
{{out}}
<pre>
Line 1,089:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
sequences ;
Line 1,096:
'[ dup sum _ > [ _ v-n vabs ] when infimum ] matrix-map ;
{ 10 9 2 1 } [ square simple-table. nl ] each</
{{out}}
<pre>
Line 1,127:
=={{header|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>
{{out}}<pre>
[[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, `
Line 1,148:
=={{header|FreeBASIC}}==
<
sub minab( n as uinteger )
Line 1,159:
end sub
minab(10)</
{{out}}<pre>
0 0 0 0 0 0 0 0 0 0
Line 1,175:
=={{header|Go}}==
{{trans|Wren}}
<
import "fmt"
Line 1,206:
fmt.Println()
}
}</
{{out}}
Line 1,267:
=={{header|GW-BASIC}}==
<
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</
=={{header|Haskell}}==
<
----------- 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)</
{{Out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 1,333:
or in terms of Data.Matrix:
<
distanceToEdge :: Int -> Matrix Int
Line 1,344:
main :: IO ()
main =
mapM_ print $ distanceToEdge <$> [10, 9, 2, 1]</
{{Out}}
<pre>┌ ┐
Line 1,378:
=={{header|J}}==
<
nByN each 2 3 9 10</
{{out}}
<pre>┌───┬─────┬─────────────────┬───────────────────┐
Line 1,395:
=={{header|Julia}}==
<
for N in sizes
mat = zeros(Int, N, N)
Line 1,408:
printNbyN([23, 10, 9, 2, 1])
</
<pre>
Minimum number of cells after, before, above and below 23 x 23 square:
Line 1,472:
=={{header|MiniZinc}}==
<syntaxhighlight 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>
{{out}}
<pre>
Line 1,495:
=={{header|Modula-2}}==
<
FROM InOut IMPORT WriteCard, WriteLn;
Line 1,518:
BEGIN
minNbyN(10, 3);
END MinNByN.</
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 1,533:
=={{header|Nim}}==
{{trans|Go}}
<
proc printMinCells(n: Positive) =
Line 1,546:
for n in [10, 9, 2, 1]:
printMinCells(n)
echo()</
{{out}}
Line 1,580:
=={{header|PARI/GP}}==
<
n=10
matrix(n,n,i,j,min(min(i-1,n-i),min(j-1,n-j)))
</syntaxhighlight>
{{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.
<
{$IFDEF FPC} //used fpc 3.2.1
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON}
Line 1,693:
Test(1);
end.
</syntaxhighlight>
{{out}}
<pre style="width: 800px; height: 480px>
Line 1,750:
=={{header|Perl}}==
<
use warnings;
use List::Util qw( max min );
Line 1,762:
}
print "\n";
}</
{{out}}
<pre>0 x 0 distance to nearest edge:
Line 1,818:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{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:
<!--<
<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>
<!--</
or maybe just (good for n<=200 whereas the above goes on and on to "%4d", etc.)
<!--<
<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>
<!--</
=={{header|PILOT}}==
<
:y=0
*line
Line 1,912:
C :y=y+1
J (y<size):*line
E :</
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 1,926:
=={{header|Python}}==
<
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()
</
<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:
<
from itertools import chain, product
Line 2,067:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 2,096:
and in terms of a generalized matrix function:
<
from itertools import chain
Line 2,161:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 2,190:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
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;
}</
{{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.
<
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. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,347:
=={{header|Ring}}==
<
see "working..." + nl
see "Minimum number of cells after, before, above and below NxN squares:" + nl
Line 2,375:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 2,394:
=={{header|Ruby}}==
<
width = (n/2).to_s.size+1
m = n-1
Line 2,402:
end
puts dist2edge(10)</
{{out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 2,418:
=={{header|Vlang}}==
{{trans|Go}}
<
println("Minimum number of cells after, before, above and below $n x $n square:")
for r in 0..n {
Line 2,441:
println('')
}
}</
{{out}}
Line 2,451:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 2,467:
printMinCells.call(n)
System.print()
}</
{{out}}
Line 2,528:
=={{header|XPL0}}==
<
int A, B;
return if A<B then A else B;
Line 2,542:
CrLf(0);
];
]</
{{out}}
|