aboutsummaryrefslogtreecommitdiff
path: root/challenge-040
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-12-23 22:02:49 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-12-23 22:02:49 +0000
commitba19570c2b953edb5816d412e9079ce8ddd9403d (patch)
treeb5bd69a3f5b65ad61a94a87c4a0419f8e5aa9968 /challenge-040
parent9dbb42361a244f24c5e2eae745264e991f580c09 (diff)
downloadperlweeklychallenge-club-ba19570c2b953edb5816d412e9079ce8ddd9403d.tar.gz
perlweeklychallenge-club-ba19570c2b953edb5816d412e9079ce8ddd9403d.tar.bz2
perlweeklychallenge-club-ba19570c2b953edb5816d412e9079ce8ddd9403d.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-040')
-rw-r--r--challenge-040/ulrich-rieke/cpp/ch-1.cpp38
-rw-r--r--challenge-040/ulrich-rieke/cpp/ch-2.cpp26
-rw-r--r--challenge-040/ulrich-rieke/haskell/ch-1.hs9
-rw-r--r--challenge-040/ulrich-rieke/haskell/ch-2.hs17
-rw-r--r--challenge-040/ulrich-rieke/perl5/ch-1.pl27
-rw-r--r--challenge-040/ulrich-rieke/perl5/ch-2.pl17
6 files changed, 134 insertions, 0 deletions
diff --git a/challenge-040/ulrich-rieke/cpp/ch-1.cpp b/challenge-040/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..80bc93f39e
--- /dev/null
+++ b/challenge-040/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,38 @@
+#include <vector>
+#include <iostream>
+#include <algorithm>
+
+std::vector<std::vector<char>> createNewList(
+ std::vector<std::vector<char>> & list ) {
+ int minlength = std::min_element( list.begin( ) , list.end( ) ,
+ []( auto & listA , auto & listB ) { return listA.size( ) <
+ listB.size( ) ; } )->size( ) ;
+ std::vector<std::vector<char>> result ;
+ std::vector<char> partialResult ;
+ for ( int i = 0 ; i < minlength ; i++ ) {
+ for ( auto it = list.begin( ) ; it != list.end( ) ; it++ ) {
+ partialResult.push_back( *(it->begin() + i) ) ;
+ }
+ result.push_back( partialResult ) ;
+ partialResult.clear( ) ;
+ }
+ return result ;
+}
+
+int main( ) {
+ std::vector<char> array1 {'I', 'L' , 'O', 'V' , 'E' , 'Y' , 'O', 'U' } ;
+ std::vector<char> array2 {'2', '4' , '0', '3', '2' , '0' , '1' , '9' } ;
+ std::vector<char> array3 {'!' , '?' , 'F', '$' , '%' , '^' , '&' , '*' } ;
+ std::vector<std::vector<char>> list ;
+ list.push_back( array1 ) ;
+ list.push_back( array2 ) ;
+ list.push_back( array3 ) ;
+ std::vector<std::vector<char>> result = createNewList( list ) ;
+ for ( auto & li : result ) {
+ for ( auto it = li.begin( ) ; it != li.end( ) ; it++ ) {
+ std::cout << *it << ' ' ;
+ }
+ std::cout << std::endl ;
+ }
+ return 0 ;
+}
diff --git a/challenge-040/ulrich-rieke/cpp/ch-2.cpp b/challenge-040/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..9e8cf8c589
--- /dev/null
+++ b/challenge-040/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,26 @@
+#include <vector>
+#include <iostream>
+#include <algorithm>
+
+void reorderList( std::vector<int> & numbers ,
+ std::vector<int> & indices ) {
+ std::vector<int> sublist ;
+ for ( int i : indices ) {
+ sublist.push_back( numbers[ i ] ) ;
+ }
+ std::sort( indices.begin( ) , indices.end( ) ) ;
+ std::sort( sublist.begin( ) , sublist.end( ) ) ;
+ for ( int i = 0 ; i < indices.size( ) ; i++ ) {
+ numbers[ indices[ i ]] = sublist[ i ] ;
+ }
+}
+
+int main( ) {
+ std::vector<int> numbers { 10 , 4 , 1 , 8 , 12 , 3 } ;
+ std::vector<int> indices { 0 , 2 , 5 } ;
+ reorderList( numbers , indices ) ;
+ for ( int i : numbers )
+ std::cout << i << ' ' ;
+ std::cout << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-040/ulrich-rieke/haskell/ch-1.hs b/challenge-040/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..f477d9b48e
--- /dev/null
+++ b/challenge-040/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,9 @@
+module Challenge040
+ where
+import Data.List( (!!) , zip3 )
+
+convertLists :: [String] -> [String]
+convertLists list = map glue $ zip3 ( list !! 0 ) ( list !! 1 ) ( list !! 2 )
+ where
+ glue ::(Char, Char , Char) -> String
+ glue ( a , b , c ) = [a] ++ [b] ++ [c]
diff --git a/challenge-040/ulrich-rieke/haskell/ch-2.hs b/challenge-040/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..afc5a3b59d
--- /dev/null
+++ b/challenge-040/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,17 @@
+module Challenge040_2
+ where
+import qualified Data.Set as S
+import Data.Function ( on )
+import Data.List( (!! ), sort , sortBy )
+
+convert :: [Int] -> [Int] -> [Int]
+convert list indices =
+ let orderedIndices = sort indices
+ sublist = map (\i -> list !! i ) orderedIndices
+ sortedSublist = sort sublist
+ stableIndices = (S.fromList [0..length list - 1]) S.\\ (S.fromList
+ orderedIndices )
+ stableElements = map (\i -> list !! i) $ S.toList stableIndices
+ stablePairs = zip (S.toList stableIndices) stableElements
+ reorderedPairs = zip orderedIndices sortedSublist
+ in map snd $ sortBy ( compare `on` fst ) ( stablePairs ++ reorderedPairs )
diff --git a/challenge-040/ulrich-rieke/perl5/ch-1.pl b/challenge-040/ulrich-rieke/perl5/ch-1.pl
new file mode 100644
index 0000000000..8910e7e40b
--- /dev/null
+++ b/challenge-040/ulrich-rieke/perl5/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+
+sub printListItems {
+ my $minlength = @{$_[0]} + 0 ;
+ foreach my $list ( @_ ) {
+ my $len = @{$list} + 0 ;
+ if ( $len < $minlength ) {
+ $minlength = $len ;
+ }
+ }
+ print "$minlength \n" ;
+ my $i = 0 ;
+ while ( $i < $minlength ) {
+ foreach my $list ( @_ ) {
+ print "${$list}[$i] " ;
+ }
+ print "\n" ;
+ $i++ ;
+ }
+}
+
+my @array1 = ( 'I', 'L' , 'O' , 'V', 'E' , 'Y', 'O' , 'U' ) ;
+my @array2 = (2 , 4, 0, 3 , 2, 0 , 1, 9 ) ;
+my @array3 = ('!', '?' , '£' , '$' , '%' , '^', '&' , '*' ) ;
+printListItems( \@array1 , \@array2 , \@array3 ) ;
diff --git a/challenge-040/ulrich-rieke/perl5/ch-2.pl b/challenge-040/ulrich-rieke/perl5/ch-2.pl
new file mode 100644
index 0000000000..ce13d04eb0
--- /dev/null
+++ b/challenge-040/ulrich-rieke/perl5/ch-2.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+use strict ;
+use warnings ;
+
+my @list = ( 10, 4, 1, 8, 12, 3 ) ;
+my @indices = ( 0, 2, 5 ) ;
+#a numerical comparison is to be enforced in the next 2 lines
+my @sortedSlice = sort { $a <=> $b } @list[@indices] ;
+my @sortedIndices = sort { $a <=> $b } @indices ;
+my $arraylen = @sortedIndices + 0 ;
+for ( my $i = 0 ; $i < $arraylen ; $i++ ) {
+ $list[ $sortedIndices[ $i ]] = $sortedSlice[ $i ] ;
+}
+for my $item ( @list ) {
+ print "$item " ;
+}
+print "\n" ;