diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-02-16 01:51:38 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-16 01:51:38 +0000 |
| commit | 7b1f3661f6fef055ada5ecb824d7b7f8246f4f4a (patch) | |
| tree | cd1dc992c2af22dec70461dd46ad04c35ba358f4 | |
| parent | 96ee1334ec9e0843eb99f50a7106285f57ae0c3f (diff) | |
| parent | d659147b1158db0de7350ed34d005731da76601c (diff) | |
| download | perlweeklychallenge-club-7b1f3661f6fef055ada5ecb824d7b7f8246f4f4a.tar.gz perlweeklychallenge-club-7b1f3661f6fef055ada5ecb824d7b7f8246f4f4a.tar.bz2 perlweeklychallenge-club-7b1f3661f6fef055ada5ecb824d7b7f8246f4f4a.zip | |
Merge pull request #1249 from phillip-harris/new-branch
New branch
| -rw-r--r-- | challenge-047/phillip-harris/README | 1 | ||||
| -rw-r--r-- | challenge-047/phillip-harris/perl/ch-1.pl | 68 | ||||
| -rw-r--r-- | challenge-047/phillip-harris/perl/ch-2.pl | 12 |
3 files changed, 81 insertions, 0 deletions
diff --git a/challenge-047/phillip-harris/README b/challenge-047/phillip-harris/README new file mode 100644 index 0000000000..3636cf47d8 --- /dev/null +++ b/challenge-047/phillip-harris/README @@ -0,0 +1 @@ +Solution by Phillip Harris. diff --git a/challenge-047/phillip-harris/perl/ch-1.pl b/challenge-047/phillip-harris/perl/ch-1.pl new file mode 100644 index 0000000000..7affcfa318 --- /dev/null +++ b/challenge-047/phillip-harris/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl
+
+# For multiplication, the asterisk must be escaped \*.
+
+use strict;
+
+my $input = join( " ", @ARGV );
+
+if ( $input !~ /.*?([IVXLCDM]+)(.*?)([IVXLCDM]+)/ ) { die "Invalid input" }
+my $number1 = $1;
+my $operator = $2;
+my $number2 = $3;
+$operator =~ s/[^\+\-\*\/]//g;
+
+if ( $operator eq '+' ) {
+ print dec2rom( rom2dec($number1) + rom2dec($number2) );
+}
+elsif ( $operator eq '-' ) {
+ print dec2rom( rom2dec($number1) - rom2dec($number2) );
+}
+elsif ( $operator eq '*' ) {
+ print dec2rom( rom2dec($number1) * rom2dec($number2) );
+}
+elsif ( $operator eq '/' ) {
+ print dec2rom( int( rom2dec($number1) / rom2dec($number2) + .5 ) );
+}
+else { die "Invalid operator" }
+print "\n";
+
+sub dec2rom {
+ my @rdb = qw(I IV V IX X XL L XC C CD D CM M);
+ my @ddb = qw(1 4 5 9 10 40 50 90 100 400 500 900 1000);
+ my $dec = $_[0];
+ my $rom;
+ for ( my $x = $#ddb ; $x >= 0 ; $x-- ) {
+ if ( $dec / $ddb[$x] >= 1 ) {
+ $rom .= $rdb[$x] x int( $dec / $ddb[$x] );
+ $dec = $dec - ( $ddb[$x] * int( $dec / $ddb[$x] ) );
+ }
+ }
+ return $rom;
+}
+
+sub rom2dec {
+ my %r2d = (
+ "I" => 1,
+ "V" => 5,
+ "X" => 10,
+ "L" => 50,
+ "C" => 100,
+ "D" => 500,
+ "M" => 1000
+ );
+ my $dec;
+ my @char = split //, $_[0];
+ for ( my $x = 0 ; $x <= $#char ; $x++ ) {
+ my $current = $r2d{ $char[$x] };
+ my $lookahead = $r2d{ $char[ $x + 1 ] };
+ if ( $lookahead > $current ) {
+ $dec += $lookahead - $current;
+ $x++;
+ }
+ else {
+ $dec += $current;
+ }
+ }
+ return $dec;
+}
diff --git a/challenge-047/phillip-harris/perl/ch-2.pl b/challenge-047/phillip-harris/perl/ch-2.pl new file mode 100644 index 0000000000..6e21bd1417 --- /dev/null +++ b/challenge-047/phillip-harris/perl/ch-2.pl @@ -0,0 +1,12 @@ +#!/usr/bin/perl
+
+#Write a script to print first 20 Gapful Numbers greater than or equal to 100.
+
+$x = 100;
+while ( $gaps < 20 ) {
+ if ( $x % ( substr( $x, 0, 1 ) . substr( $x, -1 ) ) == 0 ) {
+ print "$x\n";
+ $gaps++;
+ }
+ $x++;
+}
|
