dideod.yang has asked for the wisdom of the Perl Monks concerning the following question:
## PERL ########## use warnings; use strict; my @input = qw/ 2 3 5 /; my $target = 10; my @test = &find( 0, [], [], $target, @input ); foreach my $test (@test) { print "@{$test}\n"; } sub find { my ($sum, $sofar, $output, $want, @numbers ) = @_; # print "$sum //// @{$sofar} /// @numbers\n"; if( $sum == $want ) { # print "@{$sofar}\n"; push @{$output},$sofar; } elsif( $sum < $want and @numbers and $numbers[0] > 0 ) { find( $sum + $numbers[0], [ @{$sofar}, $numbers[0] ], $output, $wa +nt, @numbers ); find( $sum, $sofar, $output, $want, @numbers[1..$#numbers] ); } return @{$output}; } ############# TCL proc combinationSum {sum sofar want numbers output } { if { $sum == $want } { #puts $sofar lappend output $sofar } if { ( $sum < $want ) && ( [lindex $numbers 0] > 0 ) && ( [llength + $numbers] > 0 ) } { combinationSum [expr $sum + [lindex $numbers 0]] [concat $sofa +r [lindex $numbers 0]] $want $numbers $output combinationSum $sum $sofar $want [lrange $numbers 1 end] $outp +ut + } return $output } set test_input [list 2 3 5 ] set test_target 10 set test_output [combinationSum 0 [] $test_target $test_input []]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: convert script from perl to tcl
by hippo (Archbishop) on Aug 07, 2019 at 08:40 UTC | |
by Anonymous Monk on Aug 07, 2019 at 08:43 UTC | |
|
Re: convert script from perl to tcl (brain....)
by Anonymous Monk on Aug 07, 2019 at 07:58 UTC | |
|
Re: convert script from perl to tcl
by LanX (Saint) on Aug 07, 2019 at 06:04 UTC | |
|
Re: convert script from perl to tcl
by atcroft (Abbot) on Aug 08, 2019 at 03:55 UTC |