aboutsummaryrefslogtreecommitdiff
path: root/challenge-047/ulrich-rieke
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-02-16 18:32:15 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-02-16 18:32:15 +0000
commit8f8b23f01a2f8d84ff387f90be863e6db497c243 (patch)
tree692fc865662fd691e5f27280f3f33c71debfaeff /challenge-047/ulrich-rieke
parentfacb9ae8d124bc5c4d31ba0b12d1031f3de7db72 (diff)
downloadperlweeklychallenge-club-8f8b23f01a2f8d84ff387f90be863e6db497c243.tar.gz
perlweeklychallenge-club-8f8b23f01a2f8d84ff387f90be863e6db497c243.tar.bz2
perlweeklychallenge-club-8f8b23f01a2f8d84ff387f90be863e6db497c243.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-047/ulrich-rieke')
-rw-r--r--challenge-047/ulrich-rieke/haskell/ch-2.hs13
-rw-r--r--challenge-047/ulrich-rieke/perl/ch-2.pl23
-rw-r--r--challenge-047/ulrich-rieke/raku/ch-1.p653
-rw-r--r--challenge-047/ulrich-rieke/raku/ch-2.p610
4 files changed, 99 insertions, 0 deletions
diff --git a/challenge-047/ulrich-rieke/haskell/ch-2.hs b/challenge-047/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..b069e3d621
--- /dev/null
+++ b/challenge-047/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,13 @@
+module Challenge047_2
+ where
+
+isGapful :: Int -> Bool
+isGapful n =
+ let numberstring = show n
+ firstDigit = head numberstring
+ lastDigit = last numberstring
+ in mod n ( read ( [firstDigit] ++ [lastDigit] )) == 0
+
+solution :: [Int]
+solution = take 20 $ filter isGapful [100 , 101..]
+
diff --git a/challenge-047/ulrich-rieke/perl/ch-2.pl b/challenge-047/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..708aa69c93
--- /dev/null
+++ b/challenge-047/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+
+sub isGapful {
+ my $number = shift ;
+ $number =~ /(\d)\d+(\d)/ ;
+ my $first = $1 ;
+ my $last = $2 ;
+ return ( ($number % ( $first . $last )) == 0 ) ;
+}
+
+my @numbers ;
+my $start = 99 ;
+while ( scalar @numbers != 20 ) {
+ $start++ ;
+ if ( isGapful( $start ) ) {
+ push( @numbers, $start ) ;
+ }
+}
+
+map { print "$_ " } @numbers ;
+print "\n" ;
diff --git a/challenge-047/ulrich-rieke/raku/ch-1.p6 b/challenge-047/ulrich-rieke/raku/ch-1.p6
new file mode 100644
index 0000000000..797320fae9
--- /dev/null
+++ b/challenge-047/ulrich-rieke/raku/ch-1.p6
@@ -0,0 +1,53 @@
+use v6 ;
+use Slang::Roman ;
+
+sub romanToArab( Str $roman --> Int) {
+ my $sum = 0 ;
+ my $len = $roman.chars ;
+ my %romanToArab = "M" => 1000 , "D" => 500 , "C" => 100 ,
+ "L" => 50 , "X" => 10 , "V" => 5 , "I" => 1 ;
+ if ( $len == 1 ) {
+ return %romanToArab{ $roman } ;
+ }
+ my $currentIndex = 0 ;
+ my $nextIndex = 1 ;
+ my $num1 ;
+ my $num2 ;
+ while ( $currentIndex < $len - 1 ) {
+ $num1 = %romanToArab{ $roman.substr( $currentIndex , 1 ) } ;
+ $num2 = %romanToArab{ $roman.substr( $nextIndex , 1 ) } ;
+ if ( $num1 < $num2 ) {
+ $sum += ( $num2 - $num1 ) ;
+ $nextIndex += 2 ;
+ $currentIndex += 2 ;
+ }
+ else {
+ $sum += $num1 ;
+ $nextIndex++ ;
+ $currentIndex++ ;
+ }
+ }
+ if ( %romanToArab{ $roman.substr( *-1, 1 ) } <=
+ %romanToArab{ $roman.substr( *-2 , 1 ) } ) {
+ $sum += %romanToArab{ $roman.substr( *-1 , 1 ) } ;
+ }
+ return $sum ;
+}
+
+sub MAIN( Str $entry ) {
+ if $entry ~~ /^^(<[IVXLCM]>+) \s+ (<[\+\-\/\*]>) \s+ (<[IVXLCM]>+)$$/ {
+ my $first_operand = romanToArab( ~$0 ) ;
+ my $second_operand = romanToArab( ~$2 ) ;
+ my $result ;
+ given ( ~$1 ) {
+ when '+' { $result = $first_operand + $second_operand }
+ when '-' { $result = $first_operand - $second_operand }
+ when '/' { $result = $first_operand div $second_operand }
+ when '*' { $result = $first_operand * $second_operand }
+ }
+ say to-roman( $result ) ;
+ }
+ else {
+ say "erroneous entry!" ;
+ }
+}
diff --git a/challenge-047/ulrich-rieke/raku/ch-2.p6 b/challenge-047/ulrich-rieke/raku/ch-2.p6
new file mode 100644
index 0000000000..c819565877
--- /dev/null
+++ b/challenge-047/ulrich-rieke/raku/ch-2.p6
@@ -0,0 +1,10 @@
+use v6 ;
+
+sub isGapful( Int $num is copy --> Bool ) {
+ my $first = $num.Str.comb.Array.shift ;
+ my $last = $num.Str.comb.Array.pop ;
+ my $divisor = ( $first ~ $last ).Int ;
+ return $num %% $divisor ;
+}
+
+say (100, 101 ... *).grep( { isGapful( $_ ) } )[^20] ;