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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I didn't notice anyone using Roman numerals. I tested the applicable subroutine with lots of values, since I wrote it for this and hadn't used it before. If there are any bugs with it other than the design decision documented, please let me know. This might actually sneak into a project since I spent a good half hour designing, coding, testing, and commenting it.

$x = 'ii'; $y = 'ii'; sub Roman { =pod This doesn't check for validity, as the subtraction rule is always honored, even for I before M if you want to do that. Roman numerals generally allow I only before V or X for the subtraction rule, X before an L or a C, and C before a D or an M. If you need to check validity, it'd be a task suitable for a separate sub since this one should be able to handle horked but workable values by design, just in case. Heck, this doesn't even check for the existence of invalid characters. It just ignores them. =cut my $z = 0; my @foo = uc(shift) =~ /(M+|D+|C+|L+|X+|V+|I+)/g; my %value = ( 'M' => 1000, 'D' => 500, 'C' => 100, 'L' => 50, 'X' => 10, 'V' => 5, 'I' => 1, ); for ( my $i = 0; $i < scalar @foo; $i++ ) { my $ch = substr $foo[$i], 0, 1; my $v = $value{$ch}; my $nxt_ch = ''; my $nxt_v = -1; if ( defined ( $foo[$i + 1] ) ) { $nxt_ch = substr( $foo[$i + 1], 0, 1 ); $nxt_v = $value{$nxt_ch}; } foreach ( 1..length $foo[$i] ) { if ( $nxt_v > $v ) { $z -= $value{$ch}; } else { $z += $value{$ch}; } } } return $z; } printf "The result is: %d\n", (Roman $x) + (Roman $y);

In reply to Re: Adding 2 + 2 by mr_mischief
in thread Adding 2 + 2 by merlyn

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (3)
    As of 2015-11-27 06:50 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (719 votes), past polls