diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-05-04 19:48:29 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-05-04 19:48:29 +0100 |
| commit | 7ce5b5ddb80747dcbab87fb650c44d5a5c31cd29 (patch) | |
| tree | d6e1f73a2b0efa1e9ddab29d02c9fe086e1db8c3 /challenge-059 | |
| parent | 883e01221b45e49604f8bee7ea015b19795bfa51 (diff) | |
| download | perlweeklychallenge-club-7ce5b5ddb80747dcbab87fb650c44d5a5c31cd29.tar.gz perlweeklychallenge-club-7ce5b5ddb80747dcbab87fb650c44d5a5c31cd29.tar.bz2 perlweeklychallenge-club-7ce5b5ddb80747dcbab87fb650c44d5a5c31cd29.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-059')
| -rw-r--r-- | challenge-059/ulrich-rieke/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-059/ulrich-rieke/raku/ch-1.p6 | 19 | ||||
| -rw-r--r-- | challenge-059/ulrich-rieke/raku/ch-2.p6 | 30 |
3 files changed, 73 insertions, 0 deletions
diff --git a/challenge-059/ulrich-rieke/perl/ch-1.pl b/challenge-059/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..88bd4280cd --- /dev/null +++ b/challenge-059/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; + +#create 2 partitions of elements less than or greater than k by "grepping" +#through the array. Shifting from the second array preserves the relative +#order +sub reorder { + my $array = shift ; + my $k = shift ; + my @first_part = grep { $_ < $k } @{$array} ; + my @second_part = grep { $_ >= $k } @{$array} ; + while ( @second_part ) { + push( @first_part, shift @second_part ) ; + } + return @first_part ; +} + +#of n elements in @ARGV, the first n - 1 are the list, the last element is k +my $len = $#ARGV ; +my @array = @ARGV[0..$len - 1 ] ; +my @reordered = reorder (\@array , $ARGV[ -1 ] ) ; +map { print "$_ -> " } @reordered[0..$len - 2] ; +print "$reordered[ -1]\n" ; diff --git a/challenge-059/ulrich-rieke/raku/ch-1.p6 b/challenge-059/ulrich-rieke/raku/ch-1.p6 new file mode 100644 index 0000000000..f1fef5c43c --- /dev/null +++ b/challenge-059/ulrich-rieke/raku/ch-1.p6 @@ -0,0 +1,19 @@ +use v6 ; + +sub reorder( @array , Int $k ) { + my @first_part = @array.grep( { $_ < $k } ) ; + my @second_part = @array.grep( { $_ >= $k } ) ; + my @reordered = @first_part.append: @second_part ; + return @reordered ; +} + +#the last element in the argument list is k, the rest in front is the +#array +sub MAIN( *@ARGS ) { + my @numarray = @ARGS[0..@ARGS.elems - 2] ; + my $num = @ARGS[ *-1 ] ; + my @reordered = reorder( @numarray, $num ) ; + my $len = @numarray.elems ; + @reordered[0..$len - 2].map( { print "$_ -> " }) ; + say @reordered[*-1] ; +} diff --git a/challenge-059/ulrich-rieke/raku/ch-2.p6 b/challenge-059/ulrich-rieke/raku/ch-2.p6 new file mode 100644 index 0000000000..382691ffb9 --- /dev/null +++ b/challenge-059/ulrich-rieke/raku/ch-2.p6 @@ -0,0 +1,30 @@ +use v6 ; + +sub myF( Int $a , Int $b --> Int ) { + my $numstring1 = $a.base(2).Str ; + my $numstring2 = $b.base(2).Str ; + my $len1 = $numstring1.chars ; + my $len2 = $numstring2.chars ; + if ( $len1 < $len2 ) { + $numstring1 = ("0" xx ($len2 - $len1)).join ~ $numstring1 ; + } + if ( $len2 < $len1 ) { + $numstring2 = ("0" xx ($len1 - $len2)).join ~ $numstring2 ; + } + my Int $diff = 0 ; + for (0..$numstring1.chars - 1 ) -> $i { + if ( $numstring1.substr( $i , 1 ) ne $numstring2.substr( $i , 1 ) ) { + $diff++ ; + } + } + return $diff ; +} + +sub MAIN( *@ARGS ) { + my $sum = 0 ; + my @combinations = @ARGS.combinations( 2 ) ; + for @combinations -> $combi { + $sum += myF( $combi[ 0 ] , $combi[ 1 ] ) ; + } + say "The sum is $sum!" ; +} |
