diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-06-03 13:13:54 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-06-03 13:13:54 +0100 |
| commit | dabf254dc1122783dcbf5087e89aa5e436017fdb (patch) | |
| tree | 76fc3e5450e22b744f6ae8777fe5c42f923c68a5 | |
| parent | e69b50b8756af15632896349155e845bd0b10e96 (diff) | |
| download | perlweeklychallenge-club-dabf254dc1122783dcbf5087e89aa5e436017fdb.tar.gz perlweeklychallenge-club-dabf254dc1122783dcbf5087e89aa5e436017fdb.tar.bz2 perlweeklychallenge-club-dabf254dc1122783dcbf5087e89aa5e436017fdb.zip | |
- Added solutions by Andrew Shitov.
- Added solutions by Jaldhar H. Vyas.
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke.
- Added solutions by Niels van Dijke.
- Added solutions by Mark Anderson.
- Added solutions by Robbie Hatley.
- Added solutions by PokGoPun.
- Added solutions by Alexander Karelas.
- Added solutions by E. Choroba.
- Added solutions by Feng Chang.
- Added solutions by Ali Moradi.
- Added solutions by W. Luis Mochan.
- Added solutions by David Ferrone.
- Added solutions by Peter Campbell Smith.
- Added solutions by Conor Hoekstra.
40 files changed, 1110 insertions, 399 deletions
diff --git a/challenge-324/conor-hoekstra/ch-1.bqn b/challenge-324/conor-hoekstra/bqn/ch-1.bqn index 4d15712906..4d15712906 100644 --- a/challenge-324/conor-hoekstra/ch-1.bqn +++ b/challenge-324/conor-hoekstra/bqn/ch-1.bqn diff --git a/challenge-324/conor-hoekstra/ch-2.bqn b/challenge-324/conor-hoekstra/bqn/ch-2.bqn index 4ebf54eecf..4ebf54eecf 100644 --- a/challenge-324/conor-hoekstra/ch-2.bqn +++ b/challenge-324/conor-hoekstra/bqn/ch-2.bqn diff --git a/challenge-324/eric-cheung/python/ch-1.py b/challenge-324/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..72d716b01d --- /dev/null +++ b/challenge-324/eric-cheung/python/ch-1.py @@ -0,0 +1,21 @@ +
+import numpy as np
+
+## Example 1
+## arrInt = [1, 2, 3, 4]
+## nRow = 2
+## nCol = 2
+
+## Example 2
+## arrInt = [1, 2, 3]
+## nRow = 1
+## nCol = 3
+
+## Example 3
+arrInt = [1, 2, 3, 4]
+nRow = 4
+nCol = 1
+
+arrOutput = np.array(arrInt).reshape((nRow, nCol)).tolist()
+
+print (arrOutput)
diff --git a/challenge-324/eric-cheung/python/ch-2.py b/challenge-324/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..4c159d4955 --- /dev/null +++ b/challenge-324/eric-cheung/python/ch-2.py @@ -0,0 +1,37 @@ +
+from itertools import chain, combinations
+
+## ==== METHOD 2 ====
+def GetXOR (arrInput):
+ nXOR = 0
+ for nElem in arrInput:
+ nXOR = nXOR ^ nElem
+
+ return nXOR
+## ==== METHOD 2 ====
+
+## arrInt = [1, 3] ## Example 1
+## arrInt = [5, 1, 6] ## Example 2
+arrInt = [3, 4, 5, 6, 7, 8] ## Example 3
+
+arrSubsetList = list(chain.from_iterable(combinations(arrInt, nIndx) for nIndx in range(len(arrInt) + 1)))
+
+## ==== METHOD 1 ====
+## arrOutput = []
+
+## for arrLoop in arrSubsetList:
+ ## if len(arrLoop) == 0:
+ ## continue
+
+ ## nXOR = 0
+ ## for nElem in arrLoop:
+ ## nXOR = nXOR ^ nElem
+
+ ## arrOutput.append(nXOR)
+## ==== METHOD 1 ====
+
+## ==== METHOD 2 ====
+arrOutput = [GetXOR (arrLoop) for arrLoop in arrSubsetList if len(arrLoop) > 0]
+## ==== METHOD 2 ====
+
+print (sum(arrOutput))
diff --git a/challenge-324/perlboy1967/perl/ch1.pl b/challenge-324/perlboy1967/perl/ch-1.pl index 9621c376be..9621c376be 100755 --- a/challenge-324/perlboy1967/perl/ch1.pl +++ b/challenge-324/perlboy1967/perl/ch-1.pl diff --git a/challenge-324/perlboy1967/perl/ch2.pl b/challenge-324/perlboy1967/perl/ch-2.pl index e3ea35aedb..e3ea35aedb 100755 --- a/challenge-324/perlboy1967/perl/ch2.pl +++ b/challenge-324/perlboy1967/perl/ch-2.pl diff --git a/challenge-324/ulrich-rieke/cpp/ch-1.cpp b/challenge-324/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..4e3d48e040 --- /dev/null +++ b/challenge-324/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,56 @@ +#include <vector>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::istringstream istr { text } ;
+ std::string word ;
+ while ( std::getline( istr , word , delimiter ) )
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+int main( ) {
+ std::cout << "Enter some integers separated by whitespace!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::cout << "Enter number of rows and columns!\n" ;
+ std::string dimline ;
+ std::getline( std::cin , dimline ) ;
+ std::vector<int> numbers , dimensions ;
+ auto numbertokens { split( line , ' ' ) } ;
+ auto dimtokens { split( dimline , ' ' ) } ;
+ for ( auto s : numbertokens )
+ numbers.push_back( std::stoi( s ) ) ;
+ for ( auto s : dimtokens )
+ dimensions.push_back( std::stoi( s )) ;
+ if ( dimensions[0] * dimensions[1] != static_cast<int>(numbers.size( ) ) ) {
+ std::cerr << "product of rows and columns must equal number of elts!\n" ;
+ return 1 ;
+ }
+ else {
+ int pos = 0 ;
+ std::vector<std::vector<int>> myArray ;
+ for ( int r = 0 ; r < dimensions[0] ; r++ ) {
+ std::vector<int> currentRow ;
+ for ( int c = 0 ; c < dimensions[1] ; c++ ) {
+ currentRow.push_back( numbers[pos] ) ;
+ pos++ ;
+ }
+ myArray.push_back( currentRow ) ;
+ }
+ std::cout << "( " ;
+ for ( auto vec : myArray ) {
+ std::cout << "[ " ;
+ for ( auto it = vec.begin( ) ; it != vec.end( ) ; ++it ) {
+ std::cout << *it << ' ' ;
+ }
+ std::cout << "] " ;
+ }
+ std::cout << ") \n" ;
+ return 0 ;
+ }
+}
+
diff --git a/challenge-324/ulrich-rieke/cpp/ch-2.cpp b/challenge-324/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..97584673a0 --- /dev/null +++ b/challenge-324/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,77 @@ +#include <string>
+#include <algorithm>
+#include <iostream>
+#include <cmath>
+#include <numeric>
+#include <sstream>
+#include <vector>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::string word ;
+ std::istringstream istr { text } ;
+ while ( std::getline( istr , word , delimiter ))
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+//create a binary string from a number! Fill up as many 0 at the front
+//as necessary
+std::string createBinaryString( int length , int number ) {
+ std::string binary ;
+ while ( number != 0 ) {
+ binary.append( std::to_string( number % 2 ) ) ;
+ number /= 2 ;
+ }
+ int len = static_cast<int>( binary.length( ) ) ;
+ if ( len < length ) {
+ for ( int i = 0 ; i < length - len ; i++ ) {
+ binary.push_back( '0' ) ;
+ }
+ }
+ //digits in the wrong order , have to be reversed ;
+ std::reverse( binary.begin( ) , binary.end( ) ) ;
+ return binary ;
+}
+
+int main( ) {
+ std::cout << "Enter some integers separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stoi( s ) ) ;
+ //creating a powerset of all subsets is equivalent to picking an
+ //element from a set if a given binary for all the numbers from 0
+ //to 2 ^ ( length of set ) - 1 contains a 1
+ std::vector<std::string> allBinaries ;
+ int len = numbers.size( ) ;
+ for ( int i = 0 ; i < static_cast<int>( std::pow( 2.0 , len ) ) ; i++ ) {
+ allBinaries.push_back( createBinaryString( len , i ) ) ;
+ }
+ std::vector<std::vector<int>> powerset ;
+ for ( auto s : allBinaries ) {
+ std::vector<int> selected ;
+ int pos = 0 ;
+ for ( char c : s ) {
+ if ( c == '1' ) {
+ selected.push_back(numbers[pos] ) ;
+ }
+ pos++ ;
+ }
+ powerset.push_back( selected ) ;
+ }
+ std::vector<int> sums ; // for the xor sums of sublists
+ for ( auto vec : powerset ) {
+ if (vec.size( ) == 1)
+ sums.push_back( vec[0] ) ;
+ else {
+ int subsum = std::accumulate( vec.begin( ) , vec.end( ) , 0 , [](
+ int a , int b ){ return a ^ b ; } ) ;
+ sums.push_back( subsum ) ;
+ }
+ }
+ std::cout << std::accumulate( sums.begin( ) , sums.end( ) , 0 ) << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-324/ulrich-rieke/haskell/ch-1.hs b/challenge-324/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..ac808e635f --- /dev/null +++ b/challenge-324/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,18 @@ +module Challenge324
+ where
+import Data.List.Split ( chunksOf )
+
+solution :: [Int] -> Int -> Int -> [[Int]]
+solution list rows columns
+ |rows * columns /= length list = error "rows and columns don't fit array"
+ |otherwise = chunksOf columns list
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by blanks!"
+ numberline <- getLine
+ putStrLn "Enter number of rows and columns!"
+ dimline <- getLine
+ let ( rows , cols ) = (read $ head $ words dimline , read $ last $
+ words dimline )
+ print $ solution ( map read $ words numberline ) rows cols
diff --git a/challenge-324/ulrich-rieke/haskell/ch-2.hs b/challenge-324/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..5f685868d7 --- /dev/null +++ b/challenge-324/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,17 @@ +module Challenge324_2
+ where
+import qualified Data.Set as S
+import Data.Bits ( xor )
+
+solution :: [Int] -> Int
+solution = sum . map op . filter ( not . null ) . map S.toList . S.toList
+ . S.powerSet . S.fromList
+ where
+ op :: [Int] -> Int
+ op list = if length list == 1 then head list else foldl1 xor list
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by blanks!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-324/ulrich-rieke/perl/ch-1.pl b/challenge-324/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..088936ab04 --- /dev/null +++ b/challenge-324/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter some integers separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+say "Enter number of rows and columns!" ;
+$line = <STDIN> ;
+chomp $line ;
+my ( $rows , $columns ) = split( /\s/ , $line ) ;
+if ( $rows * $columns != scalar( @numbers )) {
+ say "The product of rows and columns must equal the element number!" ;
+}
+else {
+ my @array ;
+ my $pos = 0 ;
+ for my $r( 0..$rows - 1 ) {
+ my @row ;
+ for my $c( 0..$columns - 1 ) {
+ push( @row , $numbers[$pos] ) ;
+ $pos++ ;
+ }
+ push( @array , \@row ) ;
+ }
+ print '(' ;
+ for my $a ( @array ) {
+ print '[' ;
+ print join( ',' , @$a ) . "] " ;
+ }
+ say ")" ;
+}
+
diff --git a/challenge-324/ulrich-rieke/perl/ch-2.pl b/challenge-324/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..c457e8e8a8 --- /dev/null +++ b/challenge-324/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use Algorithm::Combinatorics qw ( subsets ) ;
+use List::Util qw ( sum ) ;
+
+#the task is equivalent to finding the sum of all pairwise application of
+#xor in all subsets in the powerset of the array that was passed
+#all the sums of the subsets are then summed up
+
+sub mySum {
+ my $array = shift ;
+ my $sum = 0 ;
+ for my $elt ( @$array ) {
+ $sum ^= $elt ;
+ }
+ return $sum ;
+}
+
+say "Enter some integers separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my $iter = subsets( \@numbers ) ;
+my @subsums ;
+while ( my $p = $iter->next( ) ) {
+ push( @subsums , mySum( $p ) ) ;
+}
+say sum( @subsums ) ;
diff --git a/challenge-324/ulrich-rieke/raku/ch-1.raku b/challenge-324/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..b8a955879c --- /dev/null +++ b/challenge-324/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,28 @@ +use v6 ;
+
+say "Enter some integers separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+say "Enter number of rows and columns!" ;
+$line = $*IN.get ;
+my ( $rows , $columns ) = $line.words.map( {.Int} ) ;
+if ( $rows * $columns != @numbers.elems ) {
+ say "Error! The product of rows and columns must equal the number of elements!" ;
+}
+else {
+ my @array ;
+ my $pos = 0 ;
+ for (0..$rows - 1 ) -> $r {
+ my @currentRow ;
+ for (0..$columns - 1 ) -> $c {
+ @currentRow.push( @numbers[$pos] ) ;
+ $pos++ ;
+ }
+ @array.push( @currentRow ) ;
+ }
+ print "( " ;
+ for ( @array ) -> $a {
+ print '[' ~ $a.join( ',' ) ~ "] " ;
+ }
+ say ")" ;
+}
diff --git a/challenge-324/ulrich-rieke/raku/ch-2.raku b/challenge-324/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..4f53a6f8ae --- /dev/null +++ b/challenge-324/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,52 @@ +use v6 ;
+
+#the task is to find the xor-sum for each subset of the powerset of
+#numbers entered. I didn't find a suitable Raku library for that
+#so I resorted to creating the binaries of all numbers from 0 to
+#2 ^ ( n - 1 ) where n is the number of elements in the set. 1 means
+#element has to be entered , 0 not
+
+sub createSubsetStrings( $n ) {
+ my @numberstrings ;
+ for (0..2 ** $n - 1 ) -> $number {
+ @numberstrings.push( fillUp( $n , $number.base( 2 ) )) ;
+ }
+ return @numberstrings ;
+}
+
+sub fillUp( $number , $string ) {
+ my $diff = $number - $string.chars ;
+ if ( $diff == 0 ) {
+ return $string ;
+ }
+ else {
+ return ('0' x $diff) ~ $string ;
+ }
+}
+
+sub mySum( @list ) {
+ my $sum = 0 ;
+ if ( @list.elems == 1 ) {
+ $sum = @list[0] ;
+ }
+ else {
+ for @list -> $elt {
+ $sum = $sum +^ $elt ;
+ }
+ }
+ return $sum ;
+}
+
+say "Enter some integers separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my $len = @numbers.elems ;
+my @selectionStrings = createSubsetStrings( $len ) ;
+my @allLists ;
+for @selectionStrings -> $str {
+ my @pairs = $str.comb Z, @numbers ;
+ my @list ;
+ @pairs.grep( {$_[0] eq "1"} ).map( { @list.push( $_[1] ) } ) ;
+ @allLists.push( @list ) ;
+}
+say [+] @allLists.map( {mySum( $_ ) } ) ;
diff --git a/challenge-324/ulrich-rieke/rust/ch-1.rs b/challenge-324/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..295481ebdb --- /dev/null +++ b/challenge-324/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,31 @@ +use std::io ; + +fn main() { + println!("Enter some integers separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let numbers : Vec<i32> = inline.trim( ).split_whitespace( ).map( |s| + s.parse::<i32>( ).unwrap( ) ).collect( ) ; + println!("Enter 2 integers r and c denoting rows and columns!") ; + let mut dimline : String = String::new( ) ; + io::stdin( ).read_line( &mut dimline ).unwrap( ) ; + let dimensions : Vec<usize> = dimline.trim( ).split_whitespace( ). + map( |s| s.parse::<usize>( ).unwrap( ) ).collect( ) ; + if dimensions[0] * dimensions[1] != numbers.len( ) { + print!("Error! the product of rows and columns must equal") ; + println!("the number of elements in the number vector!") ; + } + else { + let mut my_array : Vec<Vec<i32>> = Vec::new( ) ; + let mut pos : usize = 0 ; + for _ in 0..dimensions[0] { + let mut row : Vec<i32> = Vec::new( ) ; + for _ in 0..dimensions[1] { + row.push( numbers[pos] ) ; + pos += 1 ; + } + my_array.push( row.clone( ) ) ; + } + println!("{:?}" , my_array ) ; + } +} diff --git a/challenge-324/ulrich-rieke/rust/ch-2.rs b/challenge-324/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..9c298b63fc --- /dev/null +++ b/challenge-324/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,15 @@ +use std::io ; +use itertools::Itertools ; + +fn main() { + println!("Enter some integers separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let numbers : Vec<u32> = inline.trim( ).split_whitespace( ).map( |s| + s.parse::<u32>( ).unwrap( )).collect( ) ; + let all_subsets : Vec<Vec<u32>> = numbers.into_iter( ).powerset( ). + collect( ) ; + let totals : Vec<u32> = all_subsets.into_iter( ).map( |v| v.iter( ). + fold(0 , |acc , n| acc ^ n ) ).collect( ) ; + println!("{}" , totals.into_iter( ).sum::<u32>( ) ) ; +} diff --git a/stats/pwc-challenge-323.json b/stats/pwc-challenge-323.json new file mode 100644 index 0000000000..d5d93cb0b2 --- /dev/null +++ b/stats/pwc-challenge-323.json @@ -0,0 +1,540 @@ +{ + "chart" : { + "type" : "column" + }, + "drilldown" : { + "series" : [ + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Ali Moradi", + "name" : "Ali Moradi" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Andreas Mahnke", + "name" : "Andreas Mahnke" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Andrew Shitov", + "name" : "Andrew Shitov" + }, + { + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Arne Sommer", + "name" : "Arne Sommer" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Bob Lied", + "name" : "Bob Lied" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Dave Jacoby", + "name" : "Dave Jacoby" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "David Ferrone", + "name" : "David Ferrone" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "E. Choroba", + "name" : "E. Choroba" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Feng Chang", + "name" : "Feng Chang" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Jan Krnavek", + "name" : "Jan Krnavek" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Kjetil Skotheim", + "name" : "Kjetil Skotheim" + }, + { + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 10 + ] + ], + "id" : "Luca Ferrari", + "name" : "Luca Ferrari" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Mark Anderson", + "name" : "Mark Anderson" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Matthias Muth", + "name" : "Matthias Muth" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Niels van Dijke", + "name" : "Niels van Dijke" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Packy Anderson", + "name" : "Packy Anderson" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith" + }, + { + "data" : [< |
