How to resolve the algorithm Sierpinski triangle step by step in the NetRexx programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sierpinski triangle step by step in the NetRexx programming language
Table of Contents
Problem Statement
Produce an ASCII representation of a Sierpinski triangle of order N.
The Sierpinski triangle of order 4 should look like this:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sierpinski triangle step by step in the NetRexx programming language
Source code in the netrexx programming language
/* NetRexx */
options replace format comments java crossref symbols nobinary
numeric digits 1000
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) public static
BLACK_UPPOINTING_TRIANGLE = '\u25b2'
parse arg ordr filr .
if ordr = '' | ordr = '.' then ordr = 4
if filr = '' | filr = '.' then filler = BLACK_UPPOINTING_TRIANGLE
else filler = filr
drawSierpinskiTriangle(ordr, filler)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method drawSierpinskiTriangle(ordr, filler = Rexx '^') public static
n = 1 * (2 ** ordr)
line = ' '.copies(2 * n)
line = line.overlay(filler, n + 1) -- set the top point of the triangle
loop row = 1 to n -- NetRexx arrays, lists etc. index from 1
say line.strip('t')
u = filler
loop col = 2 + n - row to n + row
cl = line.substr(col - 1, 1)
cr = line.substr(col + 1, 1)
if cl == cr then t = ' '
else t = filler
line = line.overlay(u, col - 1)
u = t
end col
j2 = n + row - 1
j3 = n + row
line = line.overlay(t, j2 + 1)
line = line.overlay(filler, j3 + 1)
end row
return
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the Lua programming language
You may also check:How to resolve the algorithm Shell one-liner step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Named parameters step by step in the Elixir programming language
You may also check:How to resolve the algorithm O'Halloran numbers step by step in the Phix programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Groovy programming language