Combinations: Difference between revisions
→{{header|REXX}}
Walterpachl (talk | contribs) (→{{header|REXX}}: refurbished for better variable names and formatting) |
Walterpachl (talk | contribs) |
||
Line 4,601:
=={{header|REXX}}==
===Version 1===
This REXX program supports up to 100 symbols (one symbol for each "thing").
Line 4,681 ⟶ 4,682:
{{out|output|text= when using the input of: <tt> 5 3 01234 </tt>}}
<pre>
0 1 2
0 1 3
Line 4,692 ⟶ 4,693:
1 3 4
2 3 4
</pre>
{{out|output|text= when using the input of: <tt> 5 3 abcde </tt>}}
<pre>
a b c
a b d
Line 4,707 ⟶ 4,708:
b d e
c d e
</pre>
{{out|output|text= when using the input of: <tt> 44 0 </tt>}}
<pre>
</pre>
{{out|output|text= when using the input of: <tt> 52 -5 </tt>}}
<pre>
</pre>
{{out|output|text= when using the input of: <tt> 5 -8 </tt>}}
Line 4,725 ⟶ 4,724:
*****error***** Not enough things (5) for size (8).
</pre>
===Version 2===
{{trans|Java}}
<syntaxhighlight lang="rexx">/*REXX program displays combination sets for X things taken Y at a time. */
Parse Arg things size characters
If things='?' Then Do
Say 'rexx combi2 things size characters'
Say ' defaults: 5 3 123456789...'
Say 'example rexx combi2 , , xyzuvw'
Say 'size<0 shows only the number of possible combinations'
Exit
End
If things==''|things=="," Then things=5 /* No things specified? Then use default*/
If size=='' |size=="," Then size=3 /* No size specified? Then use default*/
Numeric Digits 20
show=sign(size)
size=abs(size)
If things<size Then
Call exit 'Not enough things ('things') for size ('size').' Say '----------' things 'things taken' size 'at a time:'
n=2**things-1
nc=0
Do u=1 to n
nc=nc+combinations(u)
End
Say '------------' nc 'combinations.'
Exit
combinations: Procedure Expose things size characters show
Parse Arg u
nc=0
bu=x2b(d2x(u))
bu1=space(translate(bu,' ',0),0)
If length(bu1)=size Then Do
ub=reverse(bu)
res=''
Do i=1 To things
If characters<>'' then
c=substr(characters,i,1)
Else
c=i
If substr(ub,i,1)=1 Then res=res c
End
If show=1 then
Say res
Return 1
End
Else
Return 0
exit:
Say '*****error*****' arg(1)
Exit 13 </syntaxhighlight>
=={{header|Ring}}==
|