aboutsummaryrefslogtreecommitdiff
path: root/challenge-045
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-02-02 00:00:10 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-02-02 00:00:10 +0000
commit50f57a2e61b5f8a67894cb5fb1963c2c419ace76 (patch)
tree8f62e51eafc76d62a8416ea96e0ea9b278a0eddc /challenge-045
parente3adc1cacced96b270400f4511f5909c473b7678 (diff)
downloadperlweeklychallenge-club-50f57a2e61b5f8a67894cb5fb1963c2c419ace76.tar.gz
perlweeklychallenge-club-50f57a2e61b5f8a67894cb5fb1963c2c419ace76.tar.bz2
perlweeklychallenge-club-50f57a2e61b5f8a67894cb5fb1963c2c419ace76.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-045')
-rw-r--r--challenge-045/ulrich-rieke/haskell/ch-1.hs11
-rw-r--r--challenge-045/ulrich-rieke/haskell/ch-2.hs10
-rw-r--r--challenge-045/ulrich-rieke/perl/ch-1.pl42
-rw-r--r--challenge-045/ulrich-rieke/perl/ch-2.pl9
-rw-r--r--challenge-045/ulrich-rieke/raku/ch-1.p630
-rw-r--r--challenge-045/ulrich-rieke/raku/ch-2.p65
6 files changed, 107 insertions, 0 deletions
diff --git a/challenge-045/ulrich-rieke/haskell/ch-1.hs b/challenge-045/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..45bccf5724
--- /dev/null
+++ b/challenge-045/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,11 @@
+module Challenge045
+ where
+import Data.List ( transpose , intercalate )
+import Data.List.Split ( chunksOf )
+
+solution :: String -> String
+solution toBeEncoded =
+ let withoutSpaces = foldl1 (++) $ words toBeEncoded
+ columnstarter = chunksOf 8 withoutSpaces
+ downTheColumns = transpose columnstarter
+ in intercalate " " downTheColumns
diff --git a/challenge-045/ulrich-rieke/haskell/ch-2.hs b/challenge-045/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..6609ee2a0e
--- /dev/null
+++ b/challenge-045/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,10 @@
+module Challenge045_2
+ where
+import System.Environment ( getProgName )
+import System.IO
+
+main :: IO ( )
+main = do
+ filename <- getProgName
+ filelines <- fmap lines $ readFile filename
+ mapM_ putStrLn filelines
diff --git a/challenge-045/ulrich-rieke/perl/ch-1.pl b/challenge-045/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..2fca5c7b58
--- /dev/null
+++ b/challenge-045/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+
+sub encode {
+ my $str = shift ;
+ my @strings ;
+ my $times = int ( (length $str) / 8 ) ;
+ my $pos = 0 ;
+ for ( my $i = 0 ; $i < $times ; $i++ ) {
+ push @strings, substr( $str, $pos , 8 ) ;
+ $pos += 8 ;
+ }
+ push @strings , substr( $str, $pos ) ;
+ my $encoded ;
+ for ( my $i = 0 ; $i < 8 ; $i++ ) {
+ for my $word ( @strings ) {
+ my $len = length $word ;
+ if ( $len > $i ) {
+ $encoded .= substr( $word , $i , 1 ) ;
+ }
+ }
+ }
+ my $stringslen = scalar @strings ;
+ my $len = length $encoded ;
+ $times = length $strings[-1] ;
+ my @encodedStrings ;
+ $pos = 0 ;
+ for ( my $i = 0 ; $i < $times ; $i++ ) {
+ push @encodedStrings, substr( $encoded , $pos , $stringslen ) ;
+ $pos += $stringslen ;
+ }
+ my $theRest = 8 - $times ;
+ for ( my $i = 0 ; $i < $theRest ; $i++ ) {
+ push @encodedStrings , substr( $encoded , $pos , $stringslen - 1 ) ;
+ $pos += $stringslen - 1 ;
+ }
+ return ( join ( ' ' , @encodedStrings ) ) ;
+}
+my $toBeEncoded = join ('', @ARGV) ;
+my $encoded = encode( $toBeEncoded ) ;
+print "$encoded\n" ;
diff --git a/challenge-045/ulrich-rieke/perl/ch-2.pl b/challenge-045/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..96db887b74
--- /dev/null
+++ b/challenge-045/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,9 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+
+open (FH , "< $0" ) or die "Can't open file $0!\n" ;
+while ( <FH> ) {
+ print ;
+}
+close (FH) ;
diff --git a/challenge-045/ulrich-rieke/raku/ch-1.p6 b/challenge-045/ulrich-rieke/raku/ch-1.p6
new file mode 100644
index 0000000000..ae4f30820a
--- /dev/null
+++ b/challenge-045/ulrich-rieke/raku/ch-1.p6
@@ -0,0 +1,30 @@
+use v6 ;
+
+sub convertString( Str $str is copy ) {
+ $str ~~ s:g/\s+// ;
+ my @strings = $str.comb.rotor( 8, :partial).map( {.join} ).Array ;
+ my $encoded ;
+ for (0..7) -> $i {
+ for @strings -> $word {
+ my $len = $word.chars ;
+ $encoded ~= $word.substr( $i , 1 ) if $len > $i ;
+ }
+ }
+ my $len = @strings.elems ;
+#create as many full-length words as the last word in the columns is long
+#then create as many full-length - 1-words as the difference of the length
+#of the last word in the columns and 8 indicates
+ my $lastwordlen = @strings[$len - 1].chars ;
+ my @cycle ;
+ for (1..$lastwordlen) {
+ @cycle.push( $len ) ;
+ }
+ for ( 1..8 - $lastwordlen) {
+ @cycle.push( $len - 1 ) ;
+ }
+ return $encoded.comb.rotor( @cycle ).map( {.join} ).Array ;
+}
+
+sub MAIN( Str $arg ) {
+ say convertString( $arg ) ;
+}
diff --git a/challenge-045/ulrich-rieke/raku/ch-2.p6 b/challenge-045/ulrich-rieke/raku/ch-2.p6
new file mode 100644
index 0000000000..866d850f23
--- /dev/null
+++ b/challenge-045/ulrich-rieke/raku/ch-2.p6
@@ -0,0 +1,5 @@
+use v6 ;
+
+my $fh = open $?FILE , :r ;
+.say for $fh.lines ;
+close $fh ;