diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-02-16 17:01:12 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-16 17:01:12 +0000 |
| commit | eca6c760fe7544d0ab489c7fe890c2dfad936981 (patch) | |
| tree | a62ad809249ea17f650946c35ceafa0385ad268a | |
| parent | d9cab073fe7edabf24c06abf81216e8e74047d4d (diff) | |
| parent | f56f5718190175a80081bde5966a92ec27f649ee (diff) | |
| download | perlweeklychallenge-club-eca6c760fe7544d0ab489c7fe890c2dfad936981.tar.gz perlweeklychallenge-club-eca6c760fe7544d0ab489c7fe890c2dfad936981.tar.bz2 perlweeklychallenge-club-eca6c760fe7544d0ab489c7fe890c2dfad936981.zip | |
Merge pull request #1255 from user-person/master
User Person solutions for challenge 47
| -rw-r--r-- | challenge-047/user-person/perl/README | 1 | ||||
| -rw-r--r-- | challenge-047/user-person/perl/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-047/user-person/perl/ch-2.pl | 48 |
3 files changed, 101 insertions, 0 deletions
diff --git a/challenge-047/user-person/perl/README b/challenge-047/user-person/perl/README new file mode 100644 index 0000000000..75ac375d49 --- /dev/null +++ b/challenge-047/user-person/perl/README @@ -0,0 +1 @@ +solutions by User Person diff --git a/challenge-047/user-person/perl/ch-1.pl b/challenge-047/user-person/perl/ch-1.pl new file mode 100644 index 0000000000..ec2e27b494 --- /dev/null +++ b/challenge-047/user-person/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-1.pl # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-047/ # +# # +# Write a script that accepts two roman numbers and operation. It should # +# then perform the operation on the give roman numbers and print the # +# result. # +# # +# e.g. # +# ch-1.pl V + VI # +# # +# output: # +# XI # +# # +########################################################################### + +use strict; +use warnings; + +use FindBin; +use Roman; + +my $argString = uc "@ARGV"; +my $script = $FindBin::Script; + +die "$FindBin::Script requires arguments.\n" if scalar @ARGV == 0; + +print STDERR "[Arabic number detected in input]\n" if $argString =~ m{\d+}; + +$argString =~ s{(\s*[-+*/%]\s*)}{ $1 }g; +$argString =~ s{([MDCLXVI]+)}{arabic $1}ge; + +my $result = eval $argString; + +my $oldResult = $result; +$result = int $result; +my $decimal = $oldResult - $result; +print STDERR "Calculation result had a decimal $decimal that was truncated.\n" if $decimal; + +if ( $result == 0) { + print "N (no formal zero)\n"; # https://en.wikipedia.org/wiki/Roman_numerals#Zero +} elsif ($result > 3_999) { + die "Calculation result $result exceeds MMMCMXCIX (3,999) the maximum value of the Roman number format.\n"; +} elsif ($result < 0) { + die "Calculation result $result is negative. Roman numbers are positive integers.\n"; +} else { + $result = Roman(int $result); + print "$result\n"; +} diff --git a/challenge-047/user-person/perl/ch-2.pl b/challenge-047/user-person/perl/ch-2.pl new file mode 100644 index 0000000000..a501321878 --- /dev/null +++ b/challenge-047/user-person/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-2.pl # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-047/ # +# # +# Write a script to print first 20 Gapful Numbers greater than or equal # +# to 100. # +# # +# https://oeis.org/A108343 # +# # +# numbers that are divisible by the number formed by their first and last # +# digit. # +# # +########################################################################### + +use strict; +use warnings; + +my $QUANTITY = 20; +my ($first, $last); +my $count = 0; + +sub firstDigit { + my $number = $_[0]; + while ($number >= 10) { + $number /= 10; + } + return int($number); +} + +for (my $i = 100; $count < $QUANTITY ; ++$i) { + + $first = firstDigit $i; + $last = $i % 10; + my $formedBy = ($first * 10) + $last; + + if ( $i % $formedBy == 0 ) { + print "$i "; + ++$count; + } +} +print "\n"; + +__END__ +output: + 100 105 108 110 120 121 130 132 135 140 143 150 154 160 165 170 176 180 187 190 |
