Post Reply 
HP49-HP50 Lists of combination
12-19-2022, 01:50 AM
Post: #2
RE: HP49-HP50 Lists of combination
This works for the HP-48GX:
Code:
«
  « → m s 
    « IF m 1 ==
      THEN s 1 « 1 →LIST » DOLIST
      ELSE IF m s SIZE ==
        THEN s 1 →LIST
        ELSE m 1 - s TAIL ←C EVAL
          1 « s HEAD SWAP + » DOLIST
          m s TAIL ←C EVAL +
        END
      END
    »
  » → ←C
  « ←C EVAL
  »
»

It is mostly a verbatim translation of the following Python program:
Code:
def comb(m, s):
    if m == 1: return [[x] for x in s]
    if m == len(s): return [s]
    return [s[:1] + a for a in comb(m-1, s[1:])] + comb(m, s[1:])

The use of a compiled local variable is inspired by Gerald's program.

Example

Assuming that the program is saved as comb:

4 ENTER
{ x2 x3 x4 x5 x6 x7 }
comb

{
{ x2 x3 x4 x5 }
{ x2 x3 x4 x6 }
{ x2 x3 x4 x7 }
{ x2 x3 x5 x6 }
{ x2 x3 x5 x7 }
{ x2 x3 x6 x7 }
{ x2 x4 x5 x6 }
{ x2 x4 x5 x7 }
{ x2 x4 x6 x7 }
{ x2 x5 x6 x7 }
{ x3 x4 x5 x6 }
{ x3 x4 x5 x7 }
{ x3 x4 x6 x7 }
{ x3 x5 x6 x7 }
{ x4 x5 x6 x7 }
}

I'm sure that there's room for improvements.

References
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
HP49-HP50 Lists of combination - Gil - 12-18-2022, 11:01 PM
RE: HP49-HP50 Lists of combination - Thomas Klemm - 12-19-2022 01:50 AM
RE: HP49-HP50 Lists of combination - Gil - 12-19-2022, 10:38 AM
RE: HP49-HP50 Lists of combination - Gil - 12-19-2022, 01:42 PM
RE: HP49-HP50 Lists of combination - Gil - 12-19-2022, 03:35 PM



User(s) browsing this thread: 2 Guest(s)