Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 10482

Re: Program for finding combinations of products.

$
0
0

I have put together this code to print permutations of integer series.

You can use this as a starting point.

TYPES: tty TYPE TABLE OF i.

 

* store superset

DATA gt TYPE TABLE OF i.

* temporarily store permutation set

DATA gt_set TYPE TABLE OF i.

 

* notation for permutation is:

* n

*   P

*     k

 

* n is number of elements in superset

* k is maximum number of elements in permutation set, minimum being 1

 

PARAMETERS: p_n TYPE i DEFAULT 5,

            p_k TYPE i DEFAULT 3.

 

CHECK p_n GT 0 AND

      p_k GT 0 AND

      p_n GE p_k.

 

* populate superset

DO p_n TIMES.

  APPEND sy-index TO gt.

ENDDO.

 

* print permutation sets from size 1 till size p_k

DO p_k TIMES.

  PERFORM permute USING gt sy-index.

ENDDO.

 

*&---------------------------------------------------------------------*

*&      Form  PERMUTE

*&---------------------------------------------------------------------*

FORM permute  USING    pt TYPE tty

                       p_setsize TYPE i.

  DATA lv TYPE i.

  DATA lv_lines TYPE i.

  DATA lt TYPE tty.

  DATA lv_tabix TYPE i.

 

  LOOP AT pt INTO lv.

    lv_tabix = sy-tabix.

* PUSH element to stack and pass remaining to recursive call

    APPEND lv TO gt_set.

 

* print result if permutation set size is achieved

    DESCRIBE TABLE gt_set LINES lv_lines.

    IF lv_lines EQ p_setsize.

      LOOP AT gt_set INTO lv.

        WRITE: lv, '  '.

      ENDLOOP.

      SKIP.

      DELETE gt_set INDEX lv_lines.

      CONTINUE.

    ENDIF.

 

* recursive call for permutation of subset

    lt = pt.

    DELETE lt INDEX lv_tabix.

    IF lt IS NOT INITIAL.

      PERFORM permute USING lt p_setsize.

    ENDIF.

 

* POP the element from stack once all combinations are tried

    DESCRIBE TABLE gt_set LINES lv_lines.

    DELETE gt_set INDEX lv_lines.

  ENDLOOP.

 

ENDFORM.                    " PERMUTE


Viewing all articles
Browse latest Browse all 10482

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>