Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/Roman-numerals-Decode/Perl/roman-numerals-decode-1.pl
Normal file
25
Task/Roman-numerals-Decode/Perl/roman-numerals-decode-1.pl
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use 5.10.0;
|
||||
|
||||
{
|
||||
my @trans = (
|
||||
[M => 1000], [CM => 900],
|
||||
[D => 500], [CD => 400],
|
||||
[C => 100], [XC => 90],
|
||||
[L => 50], [XL => 40],
|
||||
[X => 10], [IX => 9],
|
||||
[V => 5], [IV => 4],
|
||||
[I => 1],
|
||||
);
|
||||
|
||||
sub from_roman {
|
||||
my $r = shift;
|
||||
my $n = 0;
|
||||
foreach my $pair (@trans) {
|
||||
my ($k, $v) = @$pair;
|
||||
$n += $v while $r =~ s/^$k//i;
|
||||
}
|
||||
return $n
|
||||
}
|
||||
}
|
||||
|
||||
say "$_: ", from_roman($_) for qw(MCMXC MDCLXVI MMVIII);
|
||||
20
Task/Roman-numerals-Decode/Perl/roman-numerals-decode-2.pl
Normal file
20
Task/Roman-numerals-Decode/Perl/roman-numerals-decode-2.pl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub roman2decimal
|
||||
{
|
||||
(local $_, my $sum, my $zeros) = (shift, 0, '');
|
||||
$zeros .= 0 while
|
||||
$sum -= s/I(?=[VX])// - s/V// * 5 - s/I//g . $zeros,
|
||||
tr/MDCLX/CLXVI/;
|
||||
return $sum;
|
||||
}
|
||||
|
||||
print s/$/ ": " . roman2decimal($_) /er while <DATA>;
|
||||
|
||||
__DATA__
|
||||
MCMXC
|
||||
MMVIII
|
||||
MDCLXVI
|
||||
26
Task/Roman-numerals-Decode/Perl/roman-numerals-decode-3.pl
Normal file
26
Task/Roman-numerals-Decode/Perl/roman-numerals-decode-3.pl
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub roman2decimal
|
||||
{
|
||||
my $sum = 0;
|
||||
$sum += $^R while $_[0] =~
|
||||
/ M (?{1000})
|
||||
| D (?{ 500})
|
||||
| C (?{ 100}) (?= [MD] (?{-100}) )?
|
||||
| L (?{ 50})
|
||||
| X (?{ 10}) (?= [CL] (?{ -10}) )?
|
||||
| V (?{ 5})
|
||||
| I (?{ 1}) (?= [XV] (?{ -1}) )?
|
||||
/gx;
|
||||
return $sum;
|
||||
}
|
||||
|
||||
print s/$/ ": " . roman2decimal($_) /er while <DATA>;
|
||||
|
||||
__DATA__
|
||||
MCMXC
|
||||
MMVIII
|
||||
MDCLXVI
|
||||
Loading…
Add table
Add a link
Reference in a new issue