Anonymous user
Subset sum problem: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, re-arranged the list of words for a smaller footprint within the program.
(Added Wren) |
m (→{{header|REXX}}: added/changed comments and whitespace, re-arranged the list of words for a smaller footprint within the program.) |
||
Line 1,902:
if target=='' | target=="," then target= 0 /*Not specified? Then use the default.*/
if stopAt=='' | stopAt=="," then stopAt= 1 /* " " " " " " */
y= 0
@.= 0▼
▲@.=0
end /*N*/
call eSort N /*sort the names with weights. */
Line 1,931 ⟶ 1,926:
call tello center(' doing chunk:' chunk" ", 79, '─')
call combN N, chunk /*N items, a CHUNK at a time. */
_= ??
if _==0 then _= 'No' /*Englishise _ for a zero count. */
call tello _ 'solution's(??) "found so far."
end /*chunk*/
Line 1,940 ⟶ 1,936:
/*──────────────────────────────────────────────────────────────────────────────────────*/
combN: procedure expose @. #. ?? stopAt target; parse arg x,y; !.= @.0
base= x + 1;
do n=1 for y; !.n=n /*construct the first combination. */
end /*n*/
ym= y - 1
do j=1; _= !.1;
if s>target then leave /*Is 1st dig>target? Then we're done.*/
do k=2 for ym; _= !.k; s= s + #._ /*Σ the weights; is sum > target ? */
Line 1,950 ⟶ 1,946:
end /*k*/
if s==target then call telly /*have we found a pot of gold? */
!.y= !.y + 1;
end /*j*/;
/*──────────────────────────────────────────────────────────────────────────────────────*/
.combUp: procedure expose !. y bbase;
p= !.d; do u=d to y; !.u= p + 1
if !.u >= bbase+u then return .combUp(u-1)
p= !.u /*P will be used for the next digt. */
end /*u*/; return 0 /*go back and sum this combination. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
eSort: procedure expose #. @. $.; parse arg N,$; h= N
do while h>1; h= h % 2
do i=1 for N-h; j= i;
if $==. then do while $.k<$.j; parse value $.j $.k with $.k $.j
if h>=j then leave; j= j - h;
end /*while $.k<$.j*/
else do while #.k<#.j; parse value @.j @.k #.j #.k with @.k @.j #.k #.j
if h>=j then leave; j= j - h;
end /*while #.k<#.j*/
end /*i*/
end /*while h>1*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's', 1)
tello: parse arg _,e; if e==. then say; say _; call lineout 'SUBSET.'y, _; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
telly: ??= ?? + 1; nameL=
do gi=1 for y;
$.gi= @.ggg
end /*gi*/
call eSort y, . /*sort the names alphabetically. */
do gs=1 for y; nameL= nameL $.gs /*build a list of names whose sum = 0 */
Line 1,991 ⟶ 1,987:
call tello
call tello 'There are ' N " entries in the (above)" arg(1) 'table.'
call tello; return</lang>
Output note: this program also writes the displayed output to file(s): SUBSET.nnn
<br> ──────── where nnn is the ''chunk'' number.
|