aboutsummaryrefslogtreecommitdiff
path: root/challenge-070
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-20 19:32:47 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-20 19:32:47 +0100
commitd96c3b3776fbb99ff873a07312075e0d34ea77c1 (patch)
tree2f85e979bbf74f4073aab5af604525f9cbacb700 /challenge-070
parent58ac447afca0a3469b65da82b48e200058a9ce1a (diff)
downloadperlweeklychallenge-club-d96c3b3776fbb99ff873a07312075e0d34ea77c1.tar.gz
perlweeklychallenge-club-d96c3b3776fbb99ff873a07312075e0d34ea77c1.tar.bz2
perlweeklychallenge-club-d96c3b3776fbb99ff873a07312075e0d34ea77c1.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-070')
-rw-r--r--challenge-070/ulrich-rieke/cpp/ch-1.cpp29
-rw-r--r--challenge-070/ulrich-rieke/perl/ch-1.pl23
-rw-r--r--challenge-070/ulrich-rieke/raku/ch-1.raku16
-rw-r--r--challenge-070/ulrich-rieke/raku/ch-2.raku21
4 files changed, 89 insertions, 0 deletions
diff --git a/challenge-070/ulrich-rieke/cpp/ch-1.cpp b/challenge-070/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..721956fde7
--- /dev/null
+++ b/challenge-070/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,29 @@
+#include <string>
+#include <iostream>
+#include <cstdlib>
+
+void swapLetters( std::string & word, int firstPos, int secondPos ) {
+ std::string temp( word.substr( secondPos , 1 ) ) ;
+ int len = word.length( ) ;
+ if ( secondPos < len - 1 )
+ word = word.substr( 0 , secondPos ) + word.substr( firstPos, 1 )
+ + word.substr( secondPos + 1 ) ;
+ else
+ word = word.substr( 0 , secondPos ) + word.substr( firstPos, 1 ) ;
+ if ( firstPos > 0 )
+ word = word.substr( 0 , firstPos ) + temp + word.substr( firstPos
+ + 1 ) ;
+ else
+ word = temp + word.substr( firstPos + 1 ) ;
+}
+
+int main( int argc, char * argv[] ) {
+ std::string word( argv[ 1 ] ) ;
+ int c = std::atoi( argv[ 2 ] ) ;
+ int o = std::atoi( argv[ 3 ] ) ;
+ int n = word.length( ) ;
+ for ( int i = 1 ; i < c + 1 ; i++ )
+ swapLetters( word , i % n , ( i + o ) % n ) ;
+ std::cout << word << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-070/ulrich-rieke/perl/ch-1.pl b/challenge-070/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..d71f920269
--- /dev/null
+++ b/challenge-070/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub swapLetters {
+ my $word = shift ;
+ my $firstPos = shift ;
+ my $secondPos = shift ;
+ my $temp = substr( $word , $secondPos , 1 ) ;
+ substr( $word , $secondPos , 1 ) = substr( $word, $firstPos , 1 ) ;
+ substr( $word , $firstPos , 1 ) = $temp ;
+ return $word ;
+}
+
+my $word = $ARGV[0] ;
+my $C = $ARGV[ 1 ] ;
+my $O = $ARGV[ 2 ] ;
+my $len = length $word ;
+for my $i ( 1..$C ) {
+ $word = swapLetters( $word , $i % $len , ( $i + $O ) % $len ) ;
+}
+say $word ;
diff --git a/challenge-070/ulrich-rieke/raku/ch-1.raku b/challenge-070/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..7d264bc09e
--- /dev/null
+++ b/challenge-070/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,16 @@
+use v6 ;
+
+sub swapLetters( Str $word is copy , Int $firstPos, Int $secondPos --> Str) {
+ my $temp = $word.substr( $secondPos , 1 ) ;
+ $word.substr-rw( $secondPos , 1 ) = $word.substr( $firstPos , 1 ) ;
+ $word.substr-rw( $firstPos , 1 ) = $temp ;
+ return $word ;
+}
+
+sub MAIN( Str $word is copy, Int $C , Int $O ) {
+ my $len = $word.chars ;
+ for (1..$C) -> $i {
+ $word = swapLetters( $word , $i % $len , ( $i + $O ) % $len ) ;
+ }
+ $word.say ;
+}
diff --git a/challenge-070/ulrich-rieke/raku/ch-2.raku b/challenge-070/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..7e164dffa5
--- /dev/null
+++ b/challenge-070/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,21 @@
+use v6 ;
+
+sub MAIN( Int $limit ) {
+ my $n = 1 ;
+ my @current = ( "0" , "1" ) ;
+ while ( $n < $limit ) {
+ for @current.reverse -> $element {
+ @current.push( $element ) ;
+ }
+ my $len = @current.elems ;
+ for (0.. $len div 2 - 1) -> $i {
+ @current[ $i ] = "0" ~ @current[ $i ] ;
+ }
+ for ( $len div 2 .. $len - 1 ) -> $i {
+ @current[ $i ] = "1" ~ @current[ $i ] ;
+ }
+ $n++ ;
+ }
+ my @numbers = @current.map( {.parse-base( 2 ) } ) ;
+ say @numbers ;
+}