diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-04-10 20:49:34 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-04-10 20:49:34 +0100 |
| commit | 1d50a0e5a8b5d70058b677ea19cc4a294f1aba32 (patch) | |
| tree | b168c7a1539c1bb568654191c17b3e76ee3789c1 /challenge-055 | |
| parent | 917d45905c9dc5d3048200ab2984df78f11252a0 (diff) | |
| download | perlweeklychallenge-club-1d50a0e5a8b5d70058b677ea19cc4a294f1aba32.tar.gz perlweeklychallenge-club-1d50a0e5a8b5d70058b677ea19cc4a294f1aba32.tar.bz2 perlweeklychallenge-club-1d50a0e5a8b5d70058b677ea19cc4a294f1aba32.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-055')
| -rw-r--r-- | challenge-055/ulrich-rieke/cpp/ch-2.cpp | 67 | ||||
| -rw-r--r-- | challenge-055/ulrich-rieke/haskell/ch-2.hs | 16 | ||||
| -rw-r--r-- | challenge-055/ulrich-rieke/perl/ch-1.pl | 55 | ||||
| -rw-r--r-- | challenge-055/ulrich-rieke/raku/ch-1.p6 | 36 | ||||
| -rw-r--r-- | challenge-055/ulrich-rieke/raku/ch-2.p6 | 27 |
5 files changed, 201 insertions, 0 deletions
diff --git a/challenge-055/ulrich-rieke/cpp/ch-2.cpp b/challenge-055/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..10adfc4756 --- /dev/null +++ b/challenge-055/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,67 @@ +#include <iostream> +#include <vector> +#include <iterator> +#include <algorithm> +#include <utility> +#include <cstdlib> + +//this function creates pairs of neighbouring numbers in a vector +std::vector<std::pair<int, int>> createDoublets ( const std::vector<int> & numbers ) { + std::vector<std::pair<int , int>> doublets ; + int len = numbers.size( ) ; + for ( int i = 0 ; i < len - 1 ; i++ ) { + std::pair<int , int> p { *(numbers.begin( ) + i ) , + *(numbers.begin( ) + i + 1 ) } ; + doublets.push_back( p ) ; + } + return doublets ; +} + +//in order to create a "wavy list of numbers", a vector of numbers is +//converted into a vector of pairs of neighbouring numbers. To be wavy, +//even-indexed pairs must be descending, odd-indexed ones must ascend. +//if this holds true for all pairs a permutation is "wavy" +bool myCondition( const std::vector<int> & numbers ) { + std::vector<std::pair<int, int>> doublets { createDoublets( numbers ) } ; + int len = doublets.size( ) ; + std::vector<bool> validWavy ; + for (int i = 0 ; i < len ; i++ ) { + std::pair<int , int> p = doublets[ i ] ; + validWavy.push_back (( i % 2 == 0 ) && ( p.first >= p.second ) ) ; + validWavy.push_back (( i % 2 == 1 ) && ( p.first <= p.second ) ) ; + } + int i = 0 ; + for ( auto b : validWavy ) { + if ( b ) + i++ ; + } + return ( i == len ) ; +} + +//numbers are entered as arguments on the command line. A minimum number +//of 2 is desirable and requested +int main( int argc, char * argv[] ) { + std::vector<int> input ; + if ( argc < 3 ) { + std::cout << "There is little sense in permuting such short sequences!\n" ; + return 1 ; + } + for ( int i = 1 ; i < argc ; i++ ) { + input.push_back( std::atoi( argv[ i ] ) ) ; + } + std::vector<std::vector<int>> permus ; + std::vector<std::vector<int>> wavies ; + do { + permus.push_back( input ) ; + } while ( std::next_permutation( input.begin( ) , input.end( ) )) ; + std::copy_if( permus.begin( ) , permus.end( ) , + std::back_inserter( wavies ) , myCondition ) ; + for ( auto & it : wavies ) { + std::cout << "[ " ; + for ( int i : it ) { + std::cout << i << " " ; + } + std::cout << " ]\n" ; + } + return 0 ; +} diff --git a/challenge-055/ulrich-rieke/haskell/ch-2.hs b/challenge-055/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..b87dbe4034 --- /dev/null +++ b/challenge-055/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,16 @@ +module Challenge055_2 + where +import Data.List.Split ( divvy ) +import Data.List ( permutations , (!!) ) + +wavyLists :: [Int] -> [[Int]] +wavyLists list = filter myCondition $ permutations list + +myCondition :: [Int] -> Bool +myCondition sublist = all numbersteps $ zip doublets [0,1..] +where + doublets = divvy 2 1 sublist + numbersteps :: ([Int] , Int ) -> Bool + numbersteps ( li , num ) + |even num = li !! 0 >= li !! 1 + |odd num = li !! 0 <= li !! 1 diff --git a/challenge-055/ulrich-rieke/perl/ch-1.pl b/challenge-055/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..0496f5a6da --- /dev/null +++ b/challenge-055/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use POSIX qw ( floor ) ; + +sub toBinary { + my $num = shift ; + my @nums ; + while ( $num != 0 ) { + my $remainder = $num % 2 ; + push @nums, $remainder ; + $num = floor ( $num / 2 ) ; + } + return reverse @nums ; +} + +sub countChars { + my $string = shift ; + my $searched = shift ; + return scalar ( grep { $_ eq $searched } split ( // , $string ) ) ; +} + +sub myFlip { + my $string = shift ; + my $len = length $string ; + for my $i ( 0..$len - 1) { + if ( substr( $string , $i , 1 ) eq "1" ) { + substr( $string , $i , 1 ) = "0" ; + } + else { + substr( $string , $i , 1 ) = "1" ; + } + } + return $string ; +} + +my @flippedArrs ; +my @digits = toBinary ( $ARGV[0] ) ; +my $len = scalar @digits ; +my $binary = join ( '' , @digits ) ; +for my $l (0 .. $len - 1 ) { + for my $r ( $l .. $len - 1 ) { + my $flipped = substr( $binary , 0 , $l ) . + myFlip( substr( $binary , $l , $r - $l ) ) . + substr ( $binary, $r ) ; + push @flippedArrs, [ $l , $r , countChars( $flipped , "1" ) ] ; + } +} +my @sorted = sort { ${$b}[2] <=> ${$a}[2] } @flippedArrs ; +print ("L " . $sorted[0][0] . " , R " . $sorted[0][1] . "\n" ) ; +my $i = 1 ; +while ( $sorted[ $i ][ 2 ] == $sorted[ 0 ][ 2 ] ) { + print ("L " . $sorted[$i][0] . " , R " . $sorted[$i][1] . "\n") ; + $i++ ; +} diff --git a/challenge-055/ulrich-rieke/raku/ch-1.p6 b/challenge-055/ulrich-rieke/raku/ch-1.p6 new file mode 100644 index 0000000000..2537ea0e47 --- /dev/null +++ b/challenge-055/ulrich-rieke/raku/ch-1.p6 @@ -0,0 +1,36 @@ +use v6 ; + +sub myFlip( Str $n --> Str ) { + return $n.comb.map( { $_ eq "1" ?? "0" !! "1" } ).join ; +} + +sub countChar( Str $string , Str $searched --> Int ) { + return $string.comb.grep( {$_ eq $searched} ).elems ; +} + +sub MAIN( Int $num ) { + my $binary = $num.base( 2 ).Str ; + $binary.say ; + my $len = $binary.chars ; + my @pairs ; + for (0..$len - 1 ) -> $l { + for ( $l..$len - 1 ) -> $r { + my $flipped = $binary.substr(0 , $l ) ~ + myFlip( $binary.substr( $l, $r - $l ) ) ~ + $binary.substr( $r ) ; + @pairs.push( ( $l , $r , $flipped ) ) ; + } + } + my @changedPairs ; + for @pairs <-> $element { + @changedPairs.push( ( $element[ 0 ] , $element[ 1] , + countChar( $element[ 2] , "1" ) ) ) ; + } + my @sorted = @changedPairs.sort( { $^b[ 2 ] <=> $^a[ 2 ] } ) ; + say "L {@sorted[0][0] } , R { @sorted[0][1 ] }" ; + my $i = 1 ; + while ( @sorted[ $i ][2] == @sorted[ 0 ][ 2 ] ) { + say "L {@sorted[$i][0] } , R { @sorted[$i][1 ] }" ; + $i++ ; + } +} diff --git a/challenge-055/ulrich-rieke/raku/ch-2.p6 b/challenge-055/ulrich-rieke/raku/ch-2.p6 new file mode 100644 index 0000000000..c247e6c2f5 --- /dev/null +++ b/challenge-055/ulrich-rieke/raku/ch-2.p6 @@ -0,0 +1,27 @@ +use v6 ; + +sub myCondition( @array --> Bool ) { + my @doublets = @array.rotor(2 => -1) ; + my $len = @doublets.elems ; + my @trues ; + for (0..$len - 1 ) -> $i { + if ( $i %% 2 ) { + @trues.push( @doublets[$i][0] >= @doublets[$i][1] ) ; + } + else { + @trues.push( @doublets[$i][ 0 ] <= @doublets[$i][ 1 ] ) ; + } + } + return @trues.grep( { $_ == True } ).elems == $len ; +} + +sub MAIN( ) { + my $num ; + my @theArray ; + $num = prompt "Enter a number (negative number to stop) : " ; + while ( $num > 0 ) { + @theArray.push( $num ) ; + $num = prompt "next number! " ; + } + .say for @theArray.permutations.grep( { myCondition( @_) } ) ; +} |
