How to resolve the algorithm List comprehensions step by step in the REXX programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm List comprehensions step by step in the REXX programming language
Table of Contents
Problem Statement
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name. Some attributes of a list comprehension are:
Write a list comprehension that builds the list of all Pythagorean triples with elements between 1 and n. If the language has multiple ways for expressing such a construct (for example, direct list comprehensions and generators), write one example for each.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm List comprehensions step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program displays a vertical list of Pythagorean triples up to a specified number.*/
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/
say 'Pythagorean triples (a² + b² = c², c ≤' n"):" /*display the list's title. */
$= /*assign a null to the triples list. */
do a=1 for n-2; aa=a*a
do b=a+1 to n-1; ab=aa + b*b
do c=b+1 to n ; cc= c*c
if ab
if ab==cc then do; $=$ '{'a"," || b','c"}"; leave; end
end /*c*/
end /*b*/
end /*a*/
#= words($); sat
do j=1 for #
say left('', 20) word($, j) /*display a member of the list, */
end /*j*/ /* [↑] list the members vertically. */
say
say # ' members listed.' /*stick a fork in it, we're all done. */
/*REXX program shows a horizontal list of Pythagorean triples up to a specified number. */
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/
do k=1 for n; @.k= k*k /*precompute the squares of usable #'s.*/
end /*k*/
sw= linesize() - 1 /*obtain the terminal width (less one).*/
say 'Pythagorean triples (a² + b² = c², c ≤' n"):" /*display the list's title. */
$= /*assign a null to the triples list. */
do a=1 for n-2; bump= a//2 /*Note: A*A is faster than A**2. */
do b=a+1 to n-1 by 1+bump
ab= @.a + @.b /*AB: a shortcut for the sum of A² & B²*/
if bump==0 & b//2==0 then cump= 2
else cump= 1
do c=b+cump to n by cump
if ab<@.c then leave /*Too small? Then try the next B. */
if ab==@.c then do; $=$ '{'a"," || b','c"}"; leave; end
end /*c*/
end /*b*/
end /*a*/
#= words($); say
do j=1 until p==0; p= lastPos('}', $, sw) /*find the last } */
if p\==0 then do; _= left($, p)
say strip(_)
$= substr($, p+1)
end
end /*j*/
say strip($); say
say # ' members listed.' /*stick a fork in it, we're all done. */
You may also check:How to resolve the algorithm Detect division by zero step by step in the Ol programming language
You may also check:How to resolve the algorithm Sorensen–Dice coefficient step by step in the Perl programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Crystal programming language
You may also check:How to resolve the algorithm Generic swap step by step in the 11l programming language
You may also check:How to resolve the algorithm Loops/Wrong ranges step by step in the jq programming language