aboutsummaryrefslogtreecommitdiff
path: root/challenge-145
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-28 16:19:40 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-28 16:19:40 +0000
commitdff54ff32dfd8efb7bf7ff7893200234bc938f0f (patch)
tree0b16c633b0e38d832e599cb8425d33915dc99812 /challenge-145
parent36140e30ca795963deb90ce1f97debf895f52646 (diff)
downloadperlweeklychallenge-club-dff54ff32dfd8efb7bf7ff7893200234bc938f0f.tar.gz
perlweeklychallenge-club-dff54ff32dfd8efb7bf7ff7893200234bc938f0f.tar.bz2
perlweeklychallenge-club-dff54ff32dfd8efb7bf7ff7893200234bc938f0f.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-145')
-rw-r--r--challenge-145/ulrich-rieke/cpp/ch-1.cpp35
-rw-r--r--challenge-145/ulrich-rieke/cpp/ch-2.cpp38
-rw-r--r--challenge-145/ulrich-rieke/haskell/ch-1.hs5
-rw-r--r--challenge-145/ulrich-rieke/haskell/ch-2.hs14
-rw-r--r--challenge-145/ulrich-rieke/perl/ch-1.pl26
-rw-r--r--challenge-145/ulrich-rieke/perl/ch-2.pl43
-rw-r--r--challenge-145/ulrich-rieke/raku/ch-1.raku14
-rw-r--r--challenge-145/ulrich-rieke/raku/ch-2.raku33
8 files changed, 208 insertions, 0 deletions
diff --git a/challenge-145/ulrich-rieke/cpp/ch-1.cpp b/challenge-145/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..9ab7cad276
--- /dev/null
+++ b/challenge-145/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,35 @@
+#include <iostream>
+#include <vector>
+
+int main( ) {
+ std::cout << "Please enter an array, number by number( negative number " ;
+ std::cout << "to end!)\n" ;
+ int num ;
+ std::vector<int> nums1 ;
+ std::vector<int> nums2 ;
+ std::cin >> num ;
+ while ( num > 0 ) {
+ nums1.push_back( num ) ;
+ std::cin >> num ;
+ }
+ std::cout << "Enter second array, as many numbers as in first array!\n" ;
+ std::cin >> num ;
+ while ( num > 0 ) {
+ nums2.push_back( num ) ;
+ std::cin >> num ;
+ }
+ while ( nums2.size( ) != nums1.size( ) ) {
+ nums2.clear( ) ;
+ std::cout << "second array should have as many numbers as the first array!\n" ;
+ std::cin >> num ;
+ while ( num > 0 ) {
+ nums2.push_back( num ) ;
+ std::cin >> num ;
+ }
+ }
+ int sum = 0 ;
+ for ( int i = 0 ; i < nums1.size( ) ; i++ )
+ sum += nums1[ i ] * nums2[ i ] ;
+ std::cout << sum << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-145/ulrich-rieke/cpp/ch-2.cpp b/challenge-145/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..0344afadfe
--- /dev/null
+++ b/challenge-145/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <algorithm>
+
+bool isPalindrome( std::string & word ) {
+ std::string comparedTo( word ) ;
+ std::reverse( comparedTo.begin( ) , comparedTo.end( ) ) ;
+ return comparedTo == word ;
+}
+
+int main( int argc , char * argv[ ] ) {
+ std::string s( argv[ 1 ] ) ;
+ std::vector<std::string> palindromes ;
+ int len = s.length( ) ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ if ( std::find( palindromes.begin( ) , palindromes.end( ) ,
+ s.substr( i , 1 ) ) == palindromes.end( ) )
+ palindromes.push_back( s.substr( i , 1 ) ) ;
+ int diff = len - i ;
+ while ( diff > 1 ) {
+ std::string wordpart { s.substr( i , diff ) } ;
+ if ( isPalindrome( wordpart ) && std::find( palindromes.begin( ) ,
+ palindromes.end( ) , wordpart ) == palindromes.end( ) ) {
+ palindromes.push_back( wordpart ) ;
+ break ;
+ }
+ else {
+ diff-- ;
+ }
+ }
+ }
+ for ( auto str : palindromes ) {
+ std::cout << str << ' ' ;
+ }
+ std::cout << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-145/ulrich-rieke/haskell/ch-1.hs b/challenge-145/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..6449c1c948
--- /dev/null
+++ b/challenge-145/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,5 @@
+module Challenge145
+ where
+
+solution :: [Int] -> [Int] -> Int
+solution list1 list2 = sum $ zipWith( * ) list1 list2
diff --git a/challenge-145/ulrich-rieke/haskell/ch-2.hs b/challenge-145/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..6a3e19c680
--- /dev/null
+++ b/challenge-145/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,14 @@
+module Challenge145_2
+ where
+import Data.List ( nub , inits , (!!) )
+
+findList :: Int -> String -> [String]
+findList pos str = [[ str !! pos ]] ++ [ head $ filter isPalindrome $ reverse
+$ inits $ drop pos str ]
+
+isPalindrome :: String -> Bool
+isPalindrome str = str == reverse str
+
+solution :: String -> [String]
+solution str = nub $ concat $ map (\i -> findList i str )
+[0 .. length str - 1]
diff --git a/challenge-145/ulrich-rieke/perl/ch-1.pl b/challenge-145/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..cc3525d33a
--- /dev/null
+++ b/challenge-145/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( sum ) ;
+
+say "Enter an array of numbers!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers1 = split( /\s+/ , $line ) ;
+say "Enter a second array of the same size!" ;
+$line = <STDIN> ;
+chomp $line ;
+my @numbers2 = split( /\s+/ , $line ) ;
+while ( scalar( @numbers2 ) != scalar( @numbers1 ) ) {
+ say "The second array must contain as many numbers as the first one!" ;
+ say "Re-enter!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+ @numbers2 = split( /\s+/ , $line ) ;
+}
+my @products ;
+for my $i ( 0 .. scalar( @numbers1 ) - 1 ) {
+ push @products , $numbers1[ $i ] * $numbers2[ $i ] ;
+}
+say sum( @products ) ;
diff --git a/challenge-145/ulrich-rieke/perl/ch-2.pl b/challenge-145/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..ed674306f3
--- /dev/null
+++ b/challenge-145/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub contains {
+ my $words = shift ;
+ my $searched = shift ;
+ my @nums = @$words ;
+ for my $num ( @nums ) {
+ if ( $searched eq $num ) {
+ return 1 ;
+ }
+ }
+ return 0 ;
+}
+
+sub isPalindrome {
+ my $word = shift ;
+ return $word eq join( '' , reverse split( // , $word )) ;
+}
+
+my $s = $ARGV[0] ;
+my @palindromes ;
+my $len = length( $s ) ;
+for my $let ( 0 .. $len - 1 ) {
+ unless ( contains( \@palindromes , substr( $s , $let , 1 ))) {
+ push @palindromes , substr( $s , $let , 1 ) ;
+ }
+ my $diff = $len - $let ;
+ while ( $diff > 1 ) {
+ my $wordpart = substr( $s , $let , $diff ) ;
+ if ( isPalindrome( $wordpart ) && not contains( \@palindromes ,
+ $wordpart)) {
+ push @palindromes, $wordpart ;
+ last ;
+ }
+ else {
+ $diff-- ;
+ }
+ }
+}
+say join( ' ' , @palindromes ) ;
diff --git a/challenge-145/ulrich-rieke/raku/ch-1.raku b/challenge-145/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..8ea036abfc
--- /dev/null
+++ b/challenge-145/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,14 @@
+use v6 ;
+
+say "Enter first number array!" ;
+my $line = $*IN.get ;
+my @nums1 = $line.split( /\s+/).map( {.Int} ) ;
+say "and another number array of the same size!" ;
+$line = $*IN.get ;
+my @nums2 = $line.split( /\s+/ ).map( {.Int} ) ;
+while ( @nums2.elems != @nums1.elems ) {
+ say "second array must have the same size as first array! Re-enter!" ;
+ $line = $*IN.get ;
+ @nums2 = $line.split( /\s+/ ).map( {.Int} ) ;
+}
+say [+] ( @nums1 Z* @nums2 ) ;
diff --git a/challenge-145/ulrich-rieke/raku/ch-2.raku b/challenge-145/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..b872d57d36
--- /dev/null
+++ b/challenge-145/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,33 @@
+use v6 ;
+
+sub isPalindrome( Str $word --> Bool ) {
+ return $word eq $word.flip ;
+}
+
+sub contains( @strings , $substring ) {
+ my $words = @strings.Set ;
+ return $substring (elem) $words ;
+}
+
+sub MAIN( Str $s is copy ) {
+ my @palindromes ;
+ my $len = $s.chars ;
+ for (0 .. $len - 1 ) -> $let {
+ unless ( contains( @palindromes , $s.substr( $let , 1 ) ) ) {
+ @palindromes.push( $s.substr( $let , 1 ) ) ;
+ }
+ my $diff = $len - $let ;
+ while ( $diff > 1 ) {
+ my $wordpart = $s.substr( $let , $diff ) ;
+ if ( isPalindrome( $wordpart) && not contains( @palindromes ,
+ $wordpart)) {
+ @palindromes.push( $wordpart ) ;
+ last ;
+ }
+ else {
+ $diff-- ;
+ }
+ }
+ }
+ say @palindromes.join( ' ' ) ;
+}