How to resolve the algorithm Combinations step by step in the Action! programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Combinations step by step in the Action! programming language
Table of Contents
Problem Statement
Given non-negative integers m and n, generate all size m combinations of the integers from 0 (zero) to n-1 in sorted order (each combination is sorted and the entire table is sorted).
3 comb 5 is: If it is more "natural" in your language to start counting from 1 (unity) instead of 0 (zero), the combinations can be of the integers from 1 to n.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Combinations step by step in the Action! programming language
Source code in the action! programming language
PROC PrintComb(BYTE ARRAY c BYTE len)
BYTE i
Put('()
FOR i=0 TO len-1
DO
IF i>0 THEN Put(',) FI
PrintB(c(i))
OD
Put(')) PutE()
RETURN
BYTE FUNC Increasing(BYTE ARRAY c BYTE len)
BYTE i
IF len<2 THEN RETURN (1) FI
FOR i=0 TO len-2
DO
IF c(i)>=c(i+1) THEN
RETURN (0)
FI
OD
RETURN (1)
BYTE FUNC NextComb(BYTE ARRAY c BYTE n,k)
INT pos,i
DO
pos=k-1
DO
c(pos)==+1
IF c(pos)
EXIT
ELSE
pos==-1
IF pos<0 THEN RETURN (0) FI
FI
FOR i=pos+1 TO k-1
DO
c(i)=c(pos)
OD
OD
UNTIL Increasing(c,k)
OD
RETURN (1)
PROC Comb(BYTE n,k)
BYTE ARRAY c(10)
BYTE i
IF k>n THEN
Print("Error! k is greater than n.")
Break()
FI
FOR i=0 TO k-1
DO
c(i)=i
OD
DO
PrintComb(c,k)
UNTIL NextComb(c,n,k)=0
OD
RETURN
PROC Main()
Comb(5,3)
RETURN
You may also check:How to resolve the algorithm Bulls and cows/Player step by step in the Fortran programming language
You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the Delphi programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Burlesque programming language
You may also check:How to resolve the algorithm Determine if a string has all unique characters step by step in the F# programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the Swift programming language