Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Subject
- Posted on
- Recursion without lists
- 03-01-2010
- Simon Andrews
March 1, 2010, 12:36 pm
In a discussion about interview questions the following problem came up:
"Take the numbers 123456789. Insert between each number either a + * or
nothing and find the two equations whose answer is 2002"
Of course I had a go and managed to find a solution, but I'm not really
happy with the way I did it. In particular I'm not wild about:
1) Having to build the full set of symbols before performing the actual
calculations. Is there a simple way to do this which only holds one
equation in memory at a time?
2) The use of eval. In this particular case it's probably the nicest
way to do it, but is it really necessary?
Any thoughts?
Simon.
#!perl
use warnings;
use strict;
my @numbers = (1,2,3,4,5,6,7,8,9);
my @symbols = ('','+','*');
my $options = build_symbol_options([[]]);
foreach my $option (@$options) {
my $equation = '';
for my $index(0..$#numbers-1) {
$equation .= $numbers[$index];
$equation .= $option->[$index];
}
$equation .= $numbers[$#numbers];
my $answer = eval($equation);
if ($answer == 2002) {
print $equation," = 2002\n";
}
}
sub build_symbol_options {
my ($options) = @_;
if (@ == 8) {
return $options;
}
my $new_options;
foreach my $option (@$options) {
foreach my $symbol (@symbols) {
my @new_symbols = @$option;
push @new_symbols,$symbol;
push @$new_options, \@new_symbols;
}
}
build_symbol_options($new_options);
}
Re: Recursion without lists
Is this a test about guessing customer intentions from vague and
actually meaningless or impossible specificiations?
Onehundredtwentythreemillionfourhundredfiftysixthousandsevenhundredeightynine
is only one number. Where is/are the other number(s) this task is
talking about? How do you insert anything between a single element?
There are no equal signs mentioned anywhere in that spec. Where do you
get those "two equations" if you don't have an equal sign?
What does some "answer" have to do with a (non-existing) equation?
Equations don't have answers, at most they may have a solution or are
true or false.
jue
Re: Recursion without lists
Concerning your point 2) I have no better idea at the moment
(or at least things would look quite a bit more complicated),
but building the complete list of possible strings first and
only then iterating over them can be avoided if you use re-
cursion (and you can stop once you have the requested two
solutions), e.g. like this:
#!/usr/bin/perl
use strict;
use warnings;
my $cnt = 0;
add_op( '1', 2 );
sub add_op {
my ( $s, $n ) = @_;
add_num( $s . $_, $n ) for ( '', '+', '*' );
}
sub add_num {
my ( $s, $n ) = @_;
if ( $n < 9 ) {
add_op( $s . $n, $n + 1 );
} elsif ( eval $s . $n == 2002 ) {
print "$s$n = 2002\n";
if ( ++$cnt == 2 ) {
exit 0;
}
}
}
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Re: Recursion without lists
On 1 Mar 2010 21:57:02 GMT, jt@toerring.de (Jens Thoms Toerring) wrote:
I tried that glob above but it didn't work on my machine.
I read the docs for File::Glob, supposedly what CORE::Glob is since 5.6
Here's what I think I understand about it:
- glob() does recursive pattern expansion.
- If wildcards are included, it will flesh out each
pattern and try to match files, but if it can't,
the pattern is not included in the result list.
- If no wildcards are in the pattern it just returns
the pattern.
- Characters can be escaped with \, presumeably for metachars
and for wildcard chars.
- Among others metachars {} denote multiple patters and
[] denote character class. Other metachars exist as recognised by
globe \*?~
Finally, on DOSISH machines, the \ is a valid directory separator
so it cannot be used as a quoting character. This fact makes the
\* in invalid and the \* instigates a dos substitution
of the current expansion using a root plus wild card embedded into the
current pattern. This fails since no such file is found, therefore
the pre-expanded pattern is not included in the result list.
So all the other combinations are valid, but not \* and
doesen't work on windows.
This takes a long time to expand on my machine. Probably because
its trying to do the dos wildcard expansion.
1234
56789
-sln
Site Timeline
- » Generate a wsdl file from a cgi script
- — Next thread in » PERL Discussions
- » FAQ 8.35 How do I close a process's filehandle without waiting for it to complete?
- — Previous thread in » PERL Discussions
- » s suffix question
- — Newest thread in » PERL Discussions
- » Dell Battery Slice LED codes
- — The site's Newest Thread. Posted in » Laptop Computers Forum