Four sides of square: Difference between revisions

m
m (syntax highlighting fixup automation)
 
(21 intermediate revisions by 11 users not shown)
Line 1:
{{Draft task|Matrices}}
 
;Task:
Line 6:
<br>
[http://keptarhely.eu/view.php?file=20220218v00x6hugz.jpeg Four sides of square - image]
 
===See also===
* [[Matrix with two diagonals]]
* [[Mosaic matrix]]
 
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">
V size = 9
L(row) 0 .< size
L(col) 0 .< size
I row == 0 | row == size - 1 | col == 0 | col == size - 1
print(‘1’, end' ‘ ’)
E
print(‘0’, end' ‘ ’)
print()
</syntaxhighlight>
 
{{out}}
<pre>
1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">
;;; draw a matrix with 1s on the edges and 0s elsewhere
 
;;; draws a matrix with height and width = n with 1s on the edges
PROC drawSquare( INT n )
CARD i, j
FOR i = 1 TO n DO
FOR j = 1 TO n DO
Put(' )
IF i = 1 OR i = n OR j = 1 OR j = n THEN Put('1) ELSE Put('0) FI
OD
PutE()
OD
RETURN
 
PROC Main()
drawSquare( 6 )
PutE()
drawSquare( 7 )
RETURN
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|Ada}}==
Line 61 ⟶ 131:
Usage: ./four_sides <length>
</pre>
 
 
=={{header|ALGOL 68}}==
Line 96 ⟶ 165:
1 1 1 1 1 1 1
</pre>
=={{header|Arturo}}==
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % draw a matrix with 1s on the edges and 0s elsewhere %
% draws a matrix with height and width = n with 1s on the edges %
procedure drawSquare ( integer value n ) ;
for i := 1 until n do begin
for j := 1 until n do begin
writeon( s_w := 0, if i = 1 or i = n or j = 1 or j = n then " 1" else " 0" )
end for_j ;
write()
end drawSquare ;
% test the draw square procedure %
drawSquare( 6 );
write();
drawSquare( 7 )
end.
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|AppleScript}}==
Defined in terms of a generic matrix function:
<syntaxhighlight lang="applescript">------------------- FOUR SIDES OF SQUARE -----------------
 
 
-- fourSides :: Int -> [[Int]]
on fourSides(n)
-- A matrix of dimension n in which edge values are 1,
-- and other values are zero.
script go
on |λ|(i, j)
if {1, n} contains i or {1, n} contains j then
1
else
0
end if
end |λ|
end script
matrix(n, n, go)
end fourSides
 
 
--------------------------- TEST -------------------------
on run
-- Matrices of dimension 1 .. 6
script test
on |λ|(n)
showMatrix(fourSides(n)) & linefeed & linefeed
end |λ|
end script
unlines(map(test, enumFromTo(1, 6)))
end run
 
 
------------------------- MATRICES -----------------------
 
-- 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
 
 
-- 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
 
 
------------------------- GENERIC ------------------------
 
-- 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
 
 
-- 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 |λ|(s)
if n > length of s then
text -n thru -1 of ((replicate(n, cFiller) as text) & s)
else
s
end if
end |λ|
end script
end justifyRight
 
 
-- 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
 
 
-- max :: Ord a => a -> a -> a
on max(x, y)
if x > y then
x
else
y
end if
end max
 
 
-- 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
 
 
-- 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>1
 
 
1 1
1 1
 
 
1 1 1
1 0 1
1 1 1
 
 
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1
 
 
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
 
 
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
Line 157 ⟶ 492:
1 1 1 1 1 1 1
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Square ← ∨⌜˜1⌽↑⟜1‿1
 
Square 5</syntaxhighlight>
{{out}}
<pre>┌─
╵ 1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
┘</pre>
 
=={{header|C}}==
Line 181 ⟶ 529:
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Line 260 ⟶ 607:
1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
procedure FillSquare(Memo: TMemo; Size: integer);
var X,Y: integer;
var S: string;
begin
S:='';
for Y:=1 to Size do
begin
for X:=1 to Size do
begin
if (X=1) or (X=Size) or
(Y=1) or (Y=Size) then S:=S+' 1'
else S:=S+' 0';
end;
S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
 
procedure ShowFillSquare(Memo: TMemo);
begin
FillSquare(Memo, 6);
FillSquare(Memo, 7);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc holmat n . .
for i to n
for j to n
if i = 1 or i = n or j = 1 or j = n
write "1 "
else
write "0 "
.
.
print ""
.
.
holmat 10
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|F_Sharp|F#}}==
Line 282 ⟶ 709:
[1; 1; 1; 1; 1; 1]]
</pre>
 
 
=={{header|FreeBASIC}}==
Line 333 ⟶ 759:
{{out}}
https://www.dropbox.com/s/9g5ahfzw1muuzgm/hollowMatrix.bmp?dl=0
 
 
=={{header|Go}}==
Line 399 ⟶ 824:
putStrLn $ intercalate "\n\n" $ map square sizes</syntaxhighlight>
{{out}}
<pre>$ four_sides 0 1 2 3 4 5
<pre>
$ four_sides 0 1 2 3 4 5
 
 
Line 421 ⟶ 845:
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1</pre>
</pre>
 
=={{header|J}}==
 
Or, expressed in terms of Data.Matrix:
<syntaxhighlight lang="haskell">import Data.Matrix
 
------------------ FOUR SIDES OF A SQUARE ----------------
 
fourSides :: Int -> Matrix Int
fourSides n = matrix n n
(\(i, j) -> (fromEnum . or) ((==) <$> [1, n] <*> [i, j]))
 
 
--------------------------- TEST -------------------------
main :: IO ()
main = mapM_ print $ fourSides <$> [0 .. 5]</syntaxhighlight>
{{Out}}
<pre>┌ ┐
└ ┘
┌ ┐
│ 1 │
└ ┘
┌ ┐
│ 1 1 │
│ 1 1 │
└ ┘
┌ ┐
│ 1 1 1 │
│ 1 0 1 │
│ 1 1 1 │
└ ┘
┌ ┐
│ 1 1 1 1 │
│ 1 0 0 1 │
│ 1 0 0 1 │
│ 1 1 1 1 │
└ ┘
┌ ┐
│ 1 1 1 1 1 │
│ 1 0 0 0 1 │
│ 1 0 0 0 1 │
│ 1 0 0 0 1 │
│ 1 1 1 1 1 │
└ ┘</pre>
 
=={{header|J}}==
Implementation:
<syntaxhighlight lang="j">fsosq=: {{+./~(+.|.)y{.1}}</syntaxhighlight>
Line 519 ⟶ 984:
squareonesapp(8)
</syntaxhighlight>
 
=={{header|K}}==
<syntaxhighlight lang="k">square: {x|/:x}{x||x}@~-':!:
 
square 5</syntaxhighlight>
{{out}}
<pre>(1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1)</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Manipulate[ArrayPad[ConstantArray[0, {1, 1} n - 1], 1, 1] // Grid, {n, 2, 20, 1}]</syntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a square matrix with square pattern in their entries */
square(n):=genmatrix(lambda([x,y],if x=1 or y=1 or x=n or y=n then 1 else 0),n,n)$
 
/* Example */
square(6);
</syntaxhighlight>
{{out}}
<pre>
matrix(
[1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1]
)
</pre>
 
=={{header|Nim}}==
There are several ways to draw the square. Here is one of them:
<syntaxhighlight lang="Nim">import std/[sequtils, strutils]
 
proc drawSquare(n: Positive) =
let s1 = repeat(1, n).join(" ")
let s2 = (1 & repeat(0, n - 2) & 1).join(" ")
echo s1
for i in 2..<n: echo s2
echo s1
 
drawSquare(7)
</syntaxhighlight>
 
{{out}}
<pre>1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|Perl}}==
Line 539 ⟶ 1,059:
=={{header|Phix}}==
See [[Matrix_with_two_diagonals#GUI.2Fonline|Matrix_with_two_diagonals#Phix]] and press 'O'.
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* DRAW SOME SQUARES WITH 1S ON THE EDGES and 0S ELSEWHERE */
 
/* CP/M SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
 
/* TASK */
 
DRAW$SQUARE: PROCEDURE( N );
DECLARE N BYTE;
DECLARE ( I, J ) BYTE;
DO I = 1 TO N;
DO J = 1 TO N;
CALL PR$CHAR( ' ' );
IF I = 1 OR I = N OR J = 1 OR J = N THEN CALL PR$CHAR( '1' );
ELSE CALL PR$CHAR( '0' );
END;
CALL PR$NL;
END;
END DRAW$SQUARE ;
 
CALL DRAW$SQUARE( 6 );
CALL PR$NL;
CALL DRAW$SQUARE( 7 );
 
EOF
</syntaxhighlight>
{{out}}
<pre>
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
 
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
</pre>
 
=={{header|Processing}}==
Line 671 ⟶ 1,240:
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ 0 over 2 - of
1 tuck join join nested
Line 836 ⟶ 1,404:
<br>
[http://keptarhely.eu/view.php?file=20220218v00x6hugz.jpeg Four sides of square]
 
=={{header|RPL}}==
≪ → n
≪ "" 1 n '''START''' "1 " + '''NEXT''' DUP
2 n 1 - '''START'''
"" 1 n '''FOR''' j j 1 == j n == OR →STR + " " + '''NEXT NEXT'''
n ROLL 1 n '''FOR''' j j DISP '''NEXT'''
≫ ≫ ''''TASK'''' STO
 
4 '''TASK'''
{{out}}
<pre>
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">def square_sides(size = 9)
Array.new(size){|n| n==0 || n==size-1 ? [1]*size : [1]+[0]*(size-2)+[1]}
end
 
puts square_sides.map{|line| line.join (" ") }
</syntaxhighlight>
{{out}}
<pre>1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1
</pre>
 
=={{header|Sidef}}==
Line 854 ⟶ 1,458:
=={{header|Wren}}==
===Text based===
<syntaxhighlight lang="ecmascriptwren">var hollowMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {
Line 881 ⟶ 1,485:
{{libheader|Go-fonts}}
This is designed to look as close as possible to the Red entry's image so that we don't have to fill up Wikimedia Commons with similar looking images.
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color, Font
class Main {
2,023

edits