2006 Oct 10 8:37 AM
Dear ALL,
I have a string suppose for example 1234 and i have to make permutation combination for the given string.
1 ) By taking all four word
2 ) By taking any three of them,
3 ) By taking any two of them,
is there any function module to provide permutation combination of the given string or please provide any logic related to calculate permutation & combination .
Thanks in advance,
With Regards
Shantanu
Dear ALL,
I have a string suppose for example 1234 and i have to make permutation combination for the given string.
1 ) By taking all four word
2 ) By taking any three of them,
3 ) By taking any two of them,
is there any function module to provide permutation combination of the given string or please provide any logic related to calculate permutation & combination .
Thanks in advance,
With Regards
Shantanu
2008 Dec 01 11:42 AM
I have a similar requirnment .. i need your help in it , how you have solved it ...
2008 Dec 01 1:07 PM
I've no function module, but I think I've written something similar (for unique combination of elements) some time ago:
* the elements for combinations
DATA: BEGIN OF gt_matrix OCCURS 0,
value TYPE string,
END OF gt_matrix.
* the unique combinations
DATA: BEGIN OF gt_result OCCURS 0,
value TYPE string,
END OF gt_result.
DATA: l_perm TYPE i.
* we use a simple list of elements
DO 4 TIMES.
gt_matrix-value = sy-index.
APPEND gt_matrix.
ENDDO.
l_perm = 1.
DO 3 TIMES. " combination of 2, 3, and 4 items
ADD 1 TO l_perm.
PERFORM next_level USING '' " we start with an empty result.
0 " first element -1 to use from gt_matrix
4 " last element to use from gt_matrix
1 " we start in level 1.
l_perm. " we want this many elements from gt_matrix combined
ENDDO.
LOOP AT gt_result.
WRITE / gt_result-value.
ENDLOOP.
*&---------------------------------------------------------------------*
*& Form next_level
*&---------------------------------------------------------------------*
* Recursive generation of combination levels
*----------------------------------------------------------------------*
* -->P_TARGET Current result string (for next level)
* -->P_INDEX_FROM Index From
* -->P_INDEX_TO Index to
* -->P_DEPTH Current search depth
* -->P_MAX_DEPTH Maximum search depth
*----------------------------------------------------------------------*
FORM next_level USING p_target TYPE string
p_index_from TYPE i
p_index_to TYPE i
p_depth TYPE i
p_max_depth TYPE i.
DATA: l_index TYPE i,
l_next_index TYPE i,
l_depth TYPE i,
l_target TYPE string.
l_index = p_index_from.
* for each element in gt_matrix
DO.
IF l_index GE p_index_to.
EXIT.
ENDIF.
ADD 1 TO l_index.
READ TABLE gt_matrix INDEX l_index.
* search depth equals maximum depth?
IF p_depth EQ p_max_depth.
* so we have found an unique result ...
CONCATENATE p_target gt_matrix-value INTO gt_result-value.
APPEND gt_result.
ELSE. " otherwise
* go to the next level.
l_depth = p_depth + 1.
CONCATENATE p_target gt_matrix-value INTO l_target.
PERFORM next_level USING l_target " the current result string
l_index " the index of gt_matrix to start with
p_index_to " the max index of gt_matrix
l_depth " the current search depth
p_max_depth. " the maximum search depth
ENDIF.
ENDDO.
ENDFORM. "next_level
This would generate the following result:
1 2
1 3
1 4
2 3
2 4
3 4
1 2 3
1 2 4
1 3 4
2 3 4
1 2 3 4
As my algorithm uses a recursion, it also uses much memory, which may be a problem if you use it with to many elements in gt_matrix.
Regards,
Carsten
Edited by: Carsten Grafflage on Dec 1, 2008 2:09 PM
Edited by: Carsten Grafflage on Dec 1, 2008 2:09 PM
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |