[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [ProgSoc] Combination Theory




gleNN! wrote:
> Hey Kiddies, 

> Ok ... I'd like to write a function that is passed an integer, and returns an
> array of strings (?), containing all possible unique combinations of the 
> numbers up to and including the argument. 

<snip>

> Oh .. efficiency is an issue ... but don't let that stop you from posting an 
> attempt. 

The following QuickBASIC (yes I know, it's the R-est RAD tool I know of :)
subroutine does this. It uses a recursive method; anyone want to
write the iterative version?

Oh yes, and it doesn't generate an array of strings, it prints the output. But
generating an array of strings from this should be straightforward, if you're
interested I'll amend this program to generate strings instead.

DECLARE SUB NumberCombo (prepend$, start!, finish!)
'NumberCombo by Nicholas FitzRoy-Dale
CLS

'Sample call, produces all number combinations between 1 and 4.
NumberCombo "", 1, 4

SUB NumberCombo (prepend$, start, finish)
FOR counter = start TO finish
    PRINT "[";
    IF prepend$ <> "" THEN PRINT prepend$;
    PRINT STR$(counter) + " ]  ";
    newprepend$ = prepend$ + STR$(counter)
    IF counter < finish THEN NumberCombo newprepend$, counter + 1, finish
NEXT counter
END SUB

- Nicholas

wzdd@nospam.tig.com.au, njfitzro@nospam.socs.uts.edu.au, wzdd@nospam.progsoc.uts.edu.au
--
You are subscribed to the progsoc mailing list. To unsubscribe, send a
message containing "unsubscribe" to progsoc-request@nospam.progsoc.uts.edu.au.
If you are having trouble, ask owner-progsoc@nospam.progsoc.uts.edu.au for help.

This list is archived at <http://www.progsoc.uts.edu.au/lists/progsoc/>