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

Added Easylang
(Added Easylang)
 
(7 intermediate revisions by 5 users not shown)
Line 278:
│ │ │ │0 0 0 0 0 0 0 0 0 0│
└───┴─────┴─────────────────┴───────────────────┘</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use framework "Foundation"
 
------------------- DISTANCES FROM EDGE ------------------
 
-- distancesFromEdge :: Int -> [[Int]]
on distancesFromEdge(n)
-- A matrix of minimum distances to the edge.
script f
on |λ|(row, col)
minimum({row - 1, col - 1, n - row, n - col})
end |λ|
end script
matrix(n, n, f)
end distancesFromEdge
 
 
--------------------------- TEST -------------------------
on run
script test
on |λ|(n)
showMatrix(my distancesFromEdge(n)) & ¬
linefeed
end |λ|
end script
unlines(map(test, {25, 10, 9, 2, 1}))
end run
 
 
------------------------- GENERIC ------------------------
 
-- matrix :: Int -> Int -> ((Int, Int) -> a) -> [[a]]
on matrix(nRows, nCols, f)
-- A matrix of a given number of columns and rows,
-- in which each value is a given function of its
-- (zero-based) column and row indices.
script go
property g : mReturn(f)'s |λ|
on |λ|(iRow)
set xs to {}
repeat with iCol from 1 to nCols
set end of xs to g(iRow, iCol)
end repeat
xs
end |λ|
end script
map(go, enumFromTo(1, nRows))
end matrix
 
 
-- minimum :: Ord a => [a] -> a
on minimum(xs)
set ca to current application
unwrap((ca's NSArray's arrayWithArray:xs)'s ¬
valueForKeyPath:"@min.self")
end minimum
 
 
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m ≤ n then
set xs to {}
repeat with i from m to n
set end of xs to i
end repeat
xs
else
{}
end if
end enumFromTo
 
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
 
-- unwrap :: NSValue -> a
on unwrap(nsValue)
if nsValue is missing value then
missing value
else
set ca to current application
item 1 of ((ca's NSArray's arrayWithObject:nsValue) as list)
end if
end unwrap
 
 
------------------------ FORMATTING ----------------------
 
-- showMatrix :: [[Maybe a]] -> String
on showMatrix(rows)
-- String representation of rows
-- as a matrix.
script showRow
on |λ|(a, row)
set {maxWidth, prevRows} to a
script showCell
on |λ|(acc, cell)
set {w, xs} to acc
if missing value is cell then
{w, xs & ""}
else
set s to cell as string
{max(w, length of s), xs & s}
end if
end |λ|
end script
set {rowMax, cells} to foldl(showCell, {0, {}}, row)
{max(maxWidth, rowMax), prevRows & {cells}}
end |λ|
end script
set {w, stringRows} to foldl(showRow, {0, {}}, rows)
script go
on |λ|(row)
unwords(map(justifyRight(w, space), row))
end |λ|
end script
unlines(map(go, stringRows))
end showMatrix
 
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
 
 
-- justifyRight :: Int -> Char -> String -> String
on justifyRight(n, cFiller)
script
on |λ|(txt)
if n > length of txt then
text -n thru -1 of ((replicate(n, cFiller) as text) & txt)
else
txt
end if
end |λ|
end script
end justifyRight
 
 
-- max :: Ord a => a -> a -> a
on max(x, y)
if x > y then
x
else
y
end if
end max
 
 
-- Egyptian multiplication - progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for binary
-- assembly of a target length
-- replicate :: Int -> String -> String
on replicate(n, s)
-- Egyptian multiplication - progressively doubling a list,
-- appending stages of doubling to an accumulator where needed
-- for binary assembly of a target length
script p
on |λ|({n})
n ≤ 1
end |λ|
end script
script f
on |λ|({n, dbl, out})
if (n mod 2) > 0 then
set d to out & dbl
else
set d to out
end if
{n div 2, dbl & dbl, d}
end |λ|
end script
set xs to |until|(p, f, {n, s, ""})
item 2 of xs & item 3 of xs
end replicate
 
 
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines
 
 
-- until :: (a -> Bool) -> (a -> a) -> a -> a
on |until|(p, f, x)
set v to x
set mp to mReturn(p)
set mf to mReturn(f)
repeat until mp's |λ|(v)
set v to mf's |λ|(v)
end repeat
v
end |until|
 
 
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords</syntaxhighlight>
{{Out}}
<pre> 0 0 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 1 1 0
0 1 2 2 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 3 3 2 1 0
0 1 2 3 4 4 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 5 5 4 3 2 1 0
0 1 2 3 4 5 6 6 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 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 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 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 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 11 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4 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arturo">printMinCells: function [n][
cells: array.of:n 0
loop 0..dec n 'r [
loop 0..dec n 'c ->
cells\[c]: min @[dec n-r, r, c, dec n-c]
print cells
]
]
 
loop [10 9 2 1] 'n [
print ["Minimum number of cells after, before, above and below" n "x" n "square:"]
printMinCells n
print ""
]</syntaxhighlight>
 
{{out}}
 
<pre>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</pre>
 
=={{header|AutoHotkey}}==
Line 391 ⟶ 746:
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
<syntaxhighlight lang="qbasic">100 HOME : REM 100 CLS for Chipmunk Basic
110 n = 10
120 FOR i = 1 TO n
130 FOR j = 1 TO n
140 IF i-1 <= n-i THEN a = i-1 : GOTO 160
150 IF i-1 > n-i THEN a = n-i
160 IF j-1 <= n-j THEN b = j-1 : GOTO 180
170 IF j-1 > n-j THEN b = n-j
180 IF a <= b THEN r = a : GOTO 200
190 IF a > b THEN r = b
200 PRINT r " ";
210 NEXT j
220 PRINT
230 NEXT i
240 END</syntaxhighlight>
{{out}}
<pre>Same as Chipmunk Basic entry.</pre>
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
Line 412 ⟶ 787:
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|BASIC256}}
<syntaxhighlight lang="qbasic">10 cls
20 call minab(10)
30 end
40 sub min(a,b)
50 if a <= b then min = a else min = b
60 end sub
70 sub minab(n)
80 for i = 1 to n
90 for j = 1 to n
100 print using "##"; min(min(i-1,n-i),min(j-1,n-j));
110 next j
120 print
130 next i
140 end sub</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
 
==={{header|FreeBASIC}}===
Line 821 ⟶ 1,216:
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0</pre>
 
=={{header|EasyLang}}==
{{trans|Yabasic}}
<syntaxhighlight>
proc minab n . .
for i = 1 to n
for j = 1 to n
write lower lower (i - 1) (n - i) lower (j - 1) (n - j)
.
print ""
.
.
numfmt 0 2
minab 10
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Excel}}==
Line 1,209 ⟶ 1,632:
|
|}
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
The problem is not well described. You have to look at other people's code to understand the problem, which turns out to be something quite different from the description seems to say.
 
<syntaxhighlight lang="Delphi">
 
function EdgeDistance(P: TPoint; Size: integer): integer;
{Find the distance to the nearest edge}
begin
Result:=Min(Min(P.X,(Size-1)-P.X),Min(P.Y,(Size-1)-P.Y));
end;
 
 
procedure MapMatrix(Memo: TMemo; Size: integer);
{Map each cell in Size X Size matrix}
{with the distance to nearest edge}
var X,Y,E: integer;
var S: string;
begin
Memo.Lines.Add(Format('Map for %d X %d Matrix',[Size,Size]));
S:='';
for Y:=0 to Size-1 do
begin
for X:=0 to Size-1 do
begin
E:=EdgeDistance(Point(X,Y),Size);
S:=S+Format('%3d',[E]);
end;
S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
 
 
procedure ShowEdgeMaps(Memo: TMemo);
{Show a series of maps for matrices of different sizes}
var I: integer;
begin
for I:=3 to 12 do MapMatrix(Memo,I);
end;
 
</syntaxhighlight>
{{out}}
<pre>
Map for 3 X 3 Matrix
0 0 0
0 1 0
0 0 0
 
Map for 4 X 4 Matrix
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
 
Map for 5 X 5 Matrix
0 0 0 0 0
0 1 1 1 0
0 1 2 1 0
0 1 1 1 0
0 0 0 0 0
 
Map for 6 X 6 Matrix
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
 
Map for 7 X 7 Matrix
0 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 2 2 2 1 0
0 1 2 3 2 1 0
0 1 2 2 2 1 0
0 1 1 1 1 1 0
0 0 0 0 0 0 0
 
Map for 8 X 8 Matrix
0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 0
0 1 2 2 2 2 1 0
0 1 2 3 3 2 1 0
0 1 2 3 3 2 1 0
0 1 2 2 2 2 1 0
0 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0
 
Map for 9 X 9 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
 
Map for 10 X 10 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
 
Map for 11 X 11 Matrix
0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 3 2 1 0
0 1 2 3 4 4 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 4 4 4 3 2 1 0
0 1 2 3 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0
 
Map for 12 X 12 Matrix
0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 3 3 2 1 0
0 1 2 3 4 4 4 4 3 2 1 0
0 1 2 3 4 5 5 4 3 2 1 0
0 1 2 3 4 5 5 4 3 2 1 0
0 1 2 3 4 4 4 4 3 2 1 0
0 1 2 3 3 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 2,606 ⟶ 3,171:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Nums
import "./fmt" for Fmt
 
var printMinCells = Fn.new { |n|
1,983

edits