well... i got some suckie code (just woke up with a terrible hangover... too much free beer ;-) )
but i think i understand it a bit more now... take the input string, look for the highest value (first time it's M, second time it's D), and start looking from that position for the next highest value (skipping of course the previous highest one).
#!/usr/bin/perl
use warnings;
use strict;
my @a = ('M','D','C','L','X','V','I');
my $max = 0;
my %a_val=map { $_ => $max++ } split //,'MDCLXVI';
sub search_biggest {
my ($array,$model)= @_;
my $begin=7;
my $pos = 0;
my $j;
for (my $i = 0; $i < @$array; $i++) {
$j = 0;
while ($j < @$model) {
if ($array->[$i] eq $model->[$j] and $a_val{$array->[$i]}
+< $begin){
$begin = $a_val{$array->[$i]};
$pos = $i;
$j++;
last;
}
$j++;
}
}
return $pos;
}
while(my $line = <DATA>){
my ($b,$expect) = split /\s+/, $line;
my @b_arr = split(//,$b);
my $count = 0;
my %val=map { $_ => $count++ } split //, $b;
my @results = ('.') x @a;
my $searched="0";
my $j = 0;
my $pos = search_biggest(\@b_arr, \@a);
for (my $i = $pos; $i < @b_arr; $i++) {
my $found = 0;
while ($j < @a) {
last if $b_arr[$i] =~ /$searched/;
if ($b_arr[$i] eq $a[$j]){
$found = 1;
$results[$j]=$a[$j];
$searched .= $a[$j];
}
$j++;
if($found and $i < @b_arr){
my @new = ( $i + 1 < @b_arr ? @b_arr[ $i +1 .. $#b_a
+rr ] : ($b_arr[$#b_arr]) );
my @new2 = @a;
$i = search_biggest(\@new,\@new2) if not @new = 1;
last;
}
}
$j = 0 unless $found;
}
my $result = join "",@results;
printf "%7s => %7s ( %7s : %-3s)\n",
$b, $expect, $result, $result eq $expect ? "Ok" : "Not Ok";
}
print "\n";
__DATA__
I ......I
IV .....V.
V .....V.
VI .....VI
IX ....X..
X ....X..
XI ....X.I
XIV ....XV.
XV ....XV.
XVI ....XVI
XIX ....X.I
X ....X..
XL ....X..
LX ...LX..
XC ....X..
CLXIX ..CLX.I
CDXLVI .D..XVI
MCMXCVI M.C.XVI
MDCLI MDCL..I
for the suckie code, here's the output:
I => ......I ( ......I : Ok )
IV => .....V. ( .....V. : Ok )
V => .....V. ( .....V. : Ok )
VI => .....VI ( .....VI : Ok )
IX => ....X.. ( ....X.. : Ok )
X => ....X.. ( ....X.. : Ok )
XI => ....X.I ( ....X.I : Ok )
XIV => ....XV. ( ....X.I : Not Ok)
XV => ....XV. ( ....XV. : Ok )
XVI => ....XVI ( ....XVI : Ok )
XIX => ....X.I ( ....X.I : Ok )
X => ....X.. ( ....X.. : Ok )
XL => ....X.. ( ...L... : Not Ok)
LX => ...LX.. ( ...LX.. : Ok )
XC => ....X.. ( ..C.... : Not Ok)
CLXIX => ..CLX.I ( ..CLX.I : Ok )
CDXLVI => .D..XVI ( .D..XVI : Ok )
MCMXCVI => M.C.XVI ( M.C.XVI : Ok )
MDCLI => MDCL..I ( MDCL..I : Ok )
I think i know what to do, but my code is not really working as I expected.. or maybe I should wait till my hangover is ... over ;-)
to ask a question is a moment of shame
to remain ignorant is a lifelong shame
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.