The following code is neither fast nor short. It also cannot find a solution
for input 8 8 8 9. 

Need a solution always exist? 


use strict;
die "Invalid number of chars" unless @ARGV == 4;
for (@ARGV) {
        die "Invalid input $_" unless /^[1-9]$/;
} 

my @vals=(undef,@ARGV); # Use 1 as first value;
my @ops =(undef,"*","+","-","/","");
my @op_ord_list  = grep {/[1-5]{3}/}               (111..555); #Duplicate
Ops OK
my @val_ord_list = grep {/[1-4]{4}/ && !/(.).*\1/} (1111..4444); #No
Duplicate Nums

for my $op_ord (@op_ord_list) {
        my @op_index=split//,$op_ord;
        for my $var_ord (@val_ord_list) {
                my @val_index =split//,$var_ord;
                #((a+b)+c)+d
                test("((". 
                        $vals[$val_index[0]].    $ops[$op_index[0]].
                        $vals[$val_index[1]].")".$ops[$op_index[1]].
                        $vals[$val_index[2]].")".$ops[$op_index[2]].
                        $vals[$val_index[3]]
                );
                #(a+(b+c))+d
                test("(". 
                        $vals[$val_index[0]].    $ops[$op_index[0]]."(".
                        $vals[$val_index[1]].    $ops[$op_index[1]].
                        $vals[$val_index[2]].")".$ops[$op_index[2]].
                        $vals[$val_index[3]]
                );
                #(a+b)+(c+d)
                test("(". 
                        $vals[$val_index[0]].    $ops[$op_index[0]].
                        $vals[$val_index[1]].")".$ops[$op_index[1]]."(".
                        $vals[$val_index[2]].    $ops[$op_index[2]].
                        $vals[$val_index[3]].")"
                );
                #a+(b+(c+d))
                test(
                        $vals[$val_index[0]].   $ops[$op_index[0]]."(".
                        $vals[$val_index[1]].   $ops[$op_index[1]]."(".
                        $vals[$val_index[2]].   $ops[$op_index[2]].
                        $vals[$val_index[3]]."))"
                );
                #a+((b+c)+d)
                test(
                        $vals[$val_index[0]].    $ops[$op_index[0]]."((".
                        $vals[$val_index[1]].    $ops[$op_index[1]].
                        $vals[$val_index[2]].")".$ops[$op_index[2]].

                        $vals[$val_index[3]].")"
                );
        }
}
print "Result not found\n";

sub test {
        my $expr=shift;
        return if $expr =~ /\d\(|\)\d/; # Ignore concat across ()'s 
#       print "Testing: $expr == ";
        my $answer = eval $expr;
#       print "$answer\n";
        if ($answer==24) {
                print "$expr == 24";
                exit 
        }
}




> ----------------------------------------------------------------------
> Alistair McGlinchy,           [EMAIL PROTECTED]
> Sizing and Performance, Central IT,   ext. 5012,   ph +44 20 7268-5012
> Marks and Spencer, 3 Longwalk Rd, Stockley Park, Uxbridge UB11 1AW, UK 
> 
> -----Original Message-----
> From: user who [SMTP:[EMAIL PROTECTED]]
> Sent: Friday, January 18, 2002 9:52 AM
> To:   [EMAIL PROTECTED]
> Subject:      make a 24(another practical problem)
> 
> 
> 4 numbers (all from 1-9) are given, make a formula(include +-*/()) for
> result 24.  ex:
> 
> 4, 2, 7, 5
> (4-2)*(7+5) = 24
> 
> shortest/quickest solution?


-----------------------------------------------------------------------


Registered Office:
Marks & Spencer p.l.c
Michael House, Baker Street,
London, W1U 8EP
Registered No. 214436 in England and Wales.

Telephone (020) 7935 4422 
Facsimile (020) 7487 2670

www.marksandspencer.com

Please note that electronic mail may be monitored.

This e-mail is confidential. If you received it by mistake, please let us know and 
then delete it from your system; you should not copy, disclose, or distribute its 
contents to anyone nor act in reliance on this e-mail, as this is prohibited and may 
be unlawful.

The registered office of Marks and Spencer Financial Services Limited, Marks and 
Spencer Unit Trust Management Limited, Marks and Spencer Life Assurance Limited and 
Marks and Spencer Savings and Investments Limited is Kings Meadow, Chester, CH99 9FB.

Reply via email to