in reply to Risque Romantic Rosetta Roman Race

Maybe hash operations are slow, so let's do away with them :)

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11152065 use warnings; { my @r2d; @r2d[qw(1 12 2 13 3 34 4 35 5 56 6 57 7)] = qw(1 4 5 9 10 40 50 90 100 400 500 900 1000); use List::Util qw( sum ); sub roman_to_dec3 { sum @r2d[ shift =~ tr/ivxlcdmIVXLCDM/1-71-7/r =~ /5?[67]|3?[45]|1?[2 +3]|1/g ] } } my @testdata = ( qw( XLII LXIX mi cd xcix mdclxvi cdxliv i ii iii iv v vi vii viii ix x xi xii xiii xiv xv) ); for my $r (@testdata) { print "$r: ", roman_to_dec3($r), "\n"; }

Replies are listed 'Best First'.
Re^2: Risque Romantic Rosetta Roman Race
by hippo (Archbishop) on May 11, 2023 at 09:27 UTC

    Now that, ladies and gentlemen, is a proper hack. Bravo!

    I am so glad you are here, coming up with approaches like this. They inspire me to think more laterally and I'm sure that even pondering this problem for a week I would not have come up with such a solution.


    🦛