Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re^3: Help with a Regex by insaniac
in thread Help with a Regex by planetscape

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found