Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Adding 2 + 2

by mr_mischief (Monsignor)
on Dec 21, 2001 at 14:21 UTC ( #133732=note: print w/replies, xml ) Need Help??

in reply to Adding 2 + 2

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);

Replies are listed 'Best First'.
Re: Re: Adding 2 + 2
by Juerd (Abbot) on Dec 21, 2001 at 16:30 UTC
    A very creative solution :)
    Be sure to check out Roman if you didn't know it already. It can convert roman numbers both ways, and checks if your roman input is valid.

    use Roman; $calc = "II + II"; $calc =~ s/([MDCLXVI]+)/isroman($1) ? arabic($1) : $1/; print eval $calc;

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      Thanks for the review and the suggestion.

      I knew Roman was available, but it didn't feel right to use it for this. I felt it was more in the spirit of the thread to go overboard and develop the needed tools specifically for the node. I'm actually a bit surprised it took someone posting a hand-crafted Roman to Arabic numeral conversion before someone gave an example using the module. It was one of the first twists on the problem that popped into my deranged mind.
Re^2: Adding 2 + 2
by ambrus (Abbot) on Jan 29, 2007 at 14:06 UTC
      I didn't know about the contest until just now. Thanks for asking, though. I'm enjoying looking at the site.

      Christopher E. Stith

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://133732]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-05-25 02:09 GMT
Find Nodes?
    Voting Booth?