Minimum number of cells after, before, above and below NxN squares: Difference between revisions
Line 440: | Line 440: | ||
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 |
||
</pre> |
|||
=={{header|Phix}}== |
|||
<!--<lang 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> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Minimum number of cells after, before, above and below %d x %d square:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">({</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">r</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">c</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}))</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<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> |
|||
<!--</lang>--> |
|||
{{out}} |
|||
You could argue there should be more spacing on the 23x23, but I rather like it the way it is. |
|||
<pre> |
|||
Minimum number of cells after, before, above and below 23 x 23 square: |
|||
0 0 0 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 1 1 1 1 1 1 1 1 1 1 1 1 1 0 |
|||
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0 |
|||
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0 |
|||
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0 |
|||
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0 |
|||
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0 |
|||
0 1 2 3 4 5 6 7 7 7 7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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 |
|||
</pre> |
</pre> |
||
Revision as of 02:38, 3 August 2021
- 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
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
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
<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 9 2 1 } [ square simple-table. nl ] each</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 0 0 0 0 0
Go
<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]
Haskell
<lang haskell>import Data.List.Split (chunksOf)
SHORTEST DISTANCES TO EDGE OF MATRIX ---------
distancesToEdge :: Int -> Int distancesToEdge n =
( \i -> chunksOf n $ (\(x, y) -> minimum [x, y, i - x, i - y]) <$> (fmap (,) >>= (<*>)) [0 .. i] ) $ pred n
TEST -------------------------
main :: IO () main =
mapM_ putStrLn $ showMatrix . distancesToEdge <$> [10, 9, 2, 1]
DISPLAY ------------------------
showMatrix :: Show a => a -> String showMatrix m =
let w = (succ . maximum) $ fmap (length . show) =<< m rjust n c = (drop . length) <*> (replicate n c <>) in unlines (unwords . fmap (rjust w ' ' . show) <$> m)</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 0 0 0 0 0
Julia
<lang julia>function printNbyN(sizes)
for N in sizes mat = zeros(Int, N, N) println("\n\nMinimum number of cells after, before, above and below $N x $N square:") for r in 1:N, c in 1:N mat[r, c] = min(r - 1, c - 1, N - r, N - c) end display(mat) end
end
printNbyN([23, 10, 9, 2, 1])
</lang>
- Output:
Minimum number of cells after, before, above and below 23 x 23 square: 23×23 Matrix{Int64}: 0 0 0 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 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0 0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0 0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0 0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0 0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 9 9 9 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 10 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 10 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 9 9 9 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0 0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0 0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0 0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 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 0 0 0 Minimum number of cells after, before, above and below 10 x 10 square: 10×10 Matrix{Int64}: 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: 9×9 Matrix{Int64}: 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: 2×2 Matrix{Int64}: 0 0 0 0 Minimum number of cells after, before, above and below 1 x 1 square: 1×1 Matrix{Int64}: 0
Nim
<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
Perl
<lang perl>#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Minimum_number_of_cells_after,_before,_above_and_below_NxN_squares use warnings; use List::Util qw( max min );
my $N = 10;
for my $row ( 0 .. $N - 1 )
{ my @cols = map { min $_, $row, $N-1 - max $_, $row } 0 .. $N-1; print "@cols\n"; }</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
Phix
with javascript_semantics procedure distance_to_edge(integer n) printf(1,"Minimum number of cells after, before, above and below %d x %d square:\n",n) for r=0 to n-1 do for c=0 to n-1 do printf(1,"%2d",min({r,c,n-r-1,n-c-1})) end for printf(1,"\n") end for end procedure papply({23,10,9,2,1},distance_to_edge)
- Output:
You could argue there should be more spacing on the 23x23, but I rather like it the way it is.
Minimum number of cells after, before, above and below 23 x 23 square: 0 0 0 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 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 2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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
Python
<lang python>def printMinCells(siz):
print(f"\nMinimum number of cells after, before, above and below {siz} x {siz} square:") for row in range(siz): cells = [min(row, cel, siz - row - 1, row, siz - cel - 1) for cel in range(siz)] print(" ".join([str(n) for n in cells]))
for gridsize in [10, 9, 2, 1]:
printMinCells(gridsize)
</lang>
- Output:
Same as Wren version.
Or, disentangling computation from IO (separating model from display), and composing from generics:
<lang python>Distance to edge of matrix
from itertools import chain, product
- distancesToEdge :: Int -> Int
def distancesToEdge(n):
A square matrix of dimension n, in which each value is the minimum distance from the matrix position to the edge of the matrix. axis = range(0, n) i = n - 1 return chunksOf(n)([ min(x, y, i - x, i - y) for (x, y) in product(axis, axis) ])
- ------------------------- TEST -------------------------
- main :: IO ()
def main():
Square matrices of distances to the matrix edge. Sample matrices of dimensions [10, 9, 2, 1]. print('\n\n'.join([ showMatrix(distancesToEdge(n)) for n in [10, 9, 2, 1] ]))
- ----------------------- DISPLAY ------------------------
- showMatrix :: Int -> String
def showMatrix(xs):
String representation of xs as a matrix. def go(): rows = [[str(x) for x in row] for row in xs] w = max(map(len, chain.from_iterable(rows))) return "\n".join( " ".join(k.rjust(w, ' ') for k in row) for row in rows ) return go() if xs else
- ----------------------- GENERIC ------------------------
- chunksOf :: Int -> [a] -> a
def chunksOf(n):
A series of lists of length n, subdividing the contents of xs. Where the length of xs is not evenly divisible, the final list will be shorter than n. def go(xs): return [ xs[i:n + i] for i in range(0, len(xs), n) ] if 0 < n else None return go
- MAIN ---
if __name__ == '__main__':
main()</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 0 0 0 0 0
Raku
<lang perl6>sub distance-to-edge (\N) {
my $c = ceiling N / 2; my $f = floor N / 2; my @ul = ^$c .map: -> $x { [ ^$c .map: { min($x, $_) } ] } @ul[$_].append: reverse @ul[$_; ^$f] for ^$c; @ul.push: [ reverse @ul[$_] ] for reverse ^$f; @ul
}
for 0, 1, 2, 6, 9, 23 {
my @dte = .&distance-to-edge; my $max = chars max flat @dte».Slip; say "\n$_ x $_ distance to nearest edge:"; .fmt("%{$max}d").say for @dte;
}</lang>
- Output:
0 x 0 distance to nearest edge: 1 x 1 distance to nearest edge: 0 2 x 2 distance to nearest edge: 0 0 0 0 6 x 6 distance to nearest edge: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 2 2 1 0 0 1 2 2 1 0 0 1 1 1 1 0 0 0 0 0 0 0 9 x 9 distance to nearest edge: 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 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2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 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 0 0 0
REXX
This REXX version automatically adjusts the width of each (cell) number displayed so that all displayed 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 $= 21 10 9 2 1 /*Not specified? Then use the default.*/
@title= ' the minimum 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(0, 85%(w+1) -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 minimum number of cells after, before, above, and below a 21x21 square matrix ────────────────────────────────────────────────────────────────────────────────────── 0 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 1 1 1 1 1 1 1 1 1 1 1 0 0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0 0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0 0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0 0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0 0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 7 7 7 7 7 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 8 8 8 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 9 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 9 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 8 8 8 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 7 7 7 7 7 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0 0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0 0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0 0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0 0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0 0 1 1 1 1 1 1 1 1 1 1 1 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 0 the minimum 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 minimum 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 minimum number of cells after, before, above, and below a 2x2 square matrix ────────────────────────────────────────────────────────────────────────────────────── 0 0 0 0 the minimum 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
<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