Mosaic matrix: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Add Factor)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(9 intermediate revisions by 3 users not shown)
Line 1:
{{Draft task|Matrices}}
;Task:
Draw a 'mosaic' matrix which, for the purposes of this task, is a square matrix which has 1's in alternate cells (both horizontally and vertically) starting with a 1 in the top-left hand cell.
Line 17:
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
V size = 9
Line 45 ⟶ 44:
<syntaxhighlight lang="action!">
;;; draw a "mosaic matrix" - one with a 1 in the top-left and then
;;; alternating with another character vertically and horiontallyhorizontally
 
;;; draws a mosaic matrix with height and width = n
Line 230 ⟶ 229:
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
Line 342 ⟶ 340:
1 0 1 0 1 0 1
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Mosaic ← =⌜˜2|↕
 
Mosaic 6</syntaxhighlight>
{{out}}
<pre>┌─
╵ 1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
┘</pre>
 
=={{header|C}}==
Line 366 ⟶ 378:
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Line 759 ⟶ 770:
 
=={{header|J}}==
 
Implementation:
<syntaxhighlight lang="j">mosq=:{{0 [: =2|+/~ 2 | i.y}}</syntaxhighlight>
 
Examples:
 
<syntaxhighlight lang="j"> mosq 4
1 0 1 0
Line 917 ⟶ 925:
1 0 1 0 1
</syntaxhighlight>
 
=={{header|K}}==
K6
<syntaxhighlight lang="k">mosaic: {x=/:x}2!!:
 
mosaic 6</syntaxhighlight>
{{out}}
<pre>(1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1)</pre>
 
=={{header|Maxima}}==
Line 922 ⟶ 943:
/* Function that returns a mosaic matrix */
mosaic(n):=genmatrix(lambda([i,j],charfun(evenp(abs(i-j)))),n,n)$
 
/* Alternative fumction */
altern_mosaic(n):=genmatrix(lambda([i,j],if evenp(abs(i-j)) then 1 else 0),n,n)$
 
/* Examples */
Line 1,082 ⟶ 1,106:
0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1</pre>
 
 
=={{header|PL/M}}==
Line 1,259 ⟶ 1,282:
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ 1 & not ] is even ( n --> b )
 
Line 1,426 ⟶ 1,448:
 
=={{header|RPL}}==
'''Filling a matrix'''
≪ → n
≪ n DUP 2 →LIST 0 CON
1 n '''FOR''' r
1 n '''FOR''' c
r c 2 →LIST r c + 2 MOD NOT PUT '''NEXT NEXT'''
'''NEXT NEXT'''
≫ ≫ '<span style="color:blue">'''MOZTX'''</span>' STO
 
4 <span style="color:blue">'''MOZTX'''</span>
Line 1,441 ⟶ 1,465:
[ 0 1 0 1 ]]
</pre>
'''Filling the stack'''
This stack-only one-liner can run only on HP-48G and newer models, due to the presence of the <code>∑LIST</code> instruction:
« → n
≪ DUP 2 →LIST DUP 0 CON SWAP SIGN '''DO''' DUP ∑LIST 2 MOD NOT PUTI '''UNTIL''' -64 FS? '''END''' DROP ≫ ‘<span style="color:blue">'''MOZTX'''</span>’ STO
« 1 n '''FOR''' j
j 2 MOD
2 n '''START''' DUP NOT '''NEXT'''
'''NEXT'''
n DUP 2 →LIST →ARRY
» » '<span style="color:blue">'''MOZTX'''</span>' STO
'''Direct approach'''
 
Latest RPL versions have a specific instruction to generate a matrix ruled by a formula.
≪ DUP 2 →LIST DUP 0 CON SWAP SIGN '''DO'''NOT(I+J) DUP ∑LISTMOD 2 MOD NOT PUTI '''UNTIL''' -64LCXM FS? '''END''' DROP→NUM'<span style="color:blue">'''MOZTX'''</span>' STO
 
=={{header|Sidef}}==
Line 1,472 ⟶ 1,506:
=={{header|Wren}}==
===Text based===
<syntaxhighlight lang="ecmascriptwren">var mosaicMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {
Line 1,498 ⟶ 1,532:
{{libheader|DOME}}
{{libheader|Go-fonts}}
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color, Font, ImageData
 
9,476

edits