diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-01-28 10:42:05 +0000 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-01-28 10:42:05 +0000 |
| commit | 4bb3874d4bbd36d0b85b74031a74479faa9306b7 (patch) | |
| tree | 4064d59222afffb8800f94d42bb0d15cd2f1ca16 | |
| parent | ad3cc1b2702037ce50f25c337a931642fe744fcb (diff) | |
| download | perlweeklychallenge-club-4bb3874d4bbd36d0b85b74031a74479faa9306b7.tar.gz perlweeklychallenge-club-4bb3874d4bbd36d0b85b74031a74479faa9306b7.tar.bz2 perlweeklychallenge-club-4bb3874d4bbd36d0b85b74031a74479faa9306b7.zip | |
- Added solutions by Mark Anderson.
- Added solutions by Lubos Kolouch.
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke.
- Added solutions by E. Choroba.
- Added solutions by Peter Campbell Smith.
- Added solutions by Niels van Dijke.
- Added solutions by Conor Hoekstra.
- Added solutions by Andreas Mahnke.
- Added solutions by David Ferrone.
- Added solutions by Peter Meszaros.
- Added solutions by PokGoPun.
- Added solutions by W. Luis Mochan.
41 files changed, 3958 insertions, 3362 deletions
diff --git a/challenge-306/conor-hoekstra/ch-1.bqn b/challenge-306/conor-hoekstra/bqn/ch-1.bqn index f6bfefe208..f6bfefe208 100644 --- a/challenge-306/conor-hoekstra/ch-1.bqn +++ b/challenge-306/conor-hoekstra/bqn/ch-1.bqn diff --git a/challenge-306/conor-hoekstra/ch-2.bqn b/challenge-306/conor-hoekstra/bqn/ch-2.bqn index fc335e0e6e..fc335e0e6e 100644 --- a/challenge-306/conor-hoekstra/ch-2.bqn +++ b/challenge-306/conor-hoekstra/bqn/ch-2.bqn diff --git a/challenge-306/eric-cheung/python/ch-1.py b/challenge-306/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..1f6acfd4ab --- /dev/null +++ b/challenge-306/eric-cheung/python/ch-1.py @@ -0,0 +1,22 @@ +
+arrInts = [2, 5, 3, 6, 4] ## Example 1
+## arrInts = [1, 3] ## Example 2
+
+## ===== METHOD 1 =====
+## nSum = 0
+
+## for nLen in range(1, len(arrInts) + 1, 2):
+ ## ## print (nLen)
+
+ ## for nPos in range(len(arrInts) + 1 - nLen):
+ ## ## print (arrInts[nPos:nPos + nLen])
+ ## nSum = nSum + sum(arrInts[nPos:nPos + nLen])
+
+## print (nSum)
+## ===== METHOD 1 =====
+
+## ===== METHOD 2 =====
+arrSum = [sum(arrInts[nPos:nPos + nLen]) for nLen in range(1, len(arrInts) + 1, 2) for nPos in range(len(arrInts) + 1 - nLen)]
+
+print (sum(arrSum))
+## ===== METHOD 2 =====
diff --git a/challenge-306/eric-cheung/python/ch-2.py b/challenge-306/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..71a2f127a8 --- /dev/null +++ b/challenge-306/eric-cheung/python/ch-2.py @@ -0,0 +1,18 @@ +
+## arrInts = [3, 8, 5, 2, 9, 2] ## Example 1
+arrInts = [3, 2, 5] ## Example 2
+
+while len(arrInts) > 1:
+ arrInts = sorted(arrInts)
+
+ nY = arrInts[-1]
+ nX = arrInts[-2]
+
+ del arrInts[-2]
+ del arrInts[-1]
+
+ if nY > nX:
+ arrInts.append(nY - nX)
+
+print (0 if len(arrInts) == 0 else arrInts[0])
+
diff --git a/challenge-306/perlboy1967/perl/ch1.pl b/challenge-306/perlboy1967/perl/ch-1.pl index 402d471548..402d471548 100755 --- a/challenge-306/perlboy1967/perl/ch1.pl +++ b/challenge-306/perlboy1967/perl/ch-1.pl diff --git a/challenge-306/perlboy1967/perl/ch2.pl b/challenge-306/perlboy1967/perl/ch-2.pl index 564254d8a2..564254d8a2 100755 --- a/challenge-306/perlboy1967/perl/ch2.pl +++ b/challenge-306/perlboy1967/perl/ch-2.pl diff --git a/challenge-306/ulrich-rieke/cpp/ch-1.cpp b/challenge-306/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..80a28c342a --- /dev/null +++ b/challenge-306/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,51 @@ +#include <string>
+#include <iostream>
+#include <vector>
+#include <sstream>
+#include <numeric>
+
+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 find_subarray_sum( const std::vector<int> & numbers , int len ) {
+ if ( len == 1 || len == numbers.size( ) ) {
+ return std::accumulate( numbers.begin( ) , numbers.end( ) , 0 ) ;
+ }
+ else {
+ int total = 0 ;
+ int arraylen = numbers.size( ) ;
+ for ( int i = 0 ; i < arraylen - len + 1 ; i++ ) {
+ std::vector<int> partialarray( numbers.begin( ) + i ,
+ numbers.begin( ) + i + len ) ;
+ total += std::accumulate( partialarray.begin( ) , partialarray.end( ) ,
+ 0 ) ;
+ }
+ return total ;
+ }
+}
+
+int main( ) {
+ std::cout << "Enter some positive integers separated by whitespace!\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 ) ) ;
+ }
+ std::vector<int> sums ;
+ for ( int l = 0 ; l < numbers.size( ) + 1 ; l++ ) {
+ if ( l % 2 == 1 ) {
+ sums.push_back( find_subarray_sum( numbers , l ) ) ;
+ }
+ }
+ std::cout << std::accumulate( sums.begin( ) , sums.end( ) , 0 ) << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-306/ulrich-rieke/cpp/ch-2.cpp b/challenge-306/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..6168957a91 --- /dev/null +++ b/challenge-306/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,45 @@ +#include <iostream>
+#include <string>
+#include <vector>
+#include <sstream>
+#include <algorithm>
+
+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 ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stoi( s ) ) ;
+ while ( numbers.size( ) > 1 ) {
+ std::sort( numbers.begin( ) , numbers.end( ) ) ;
+ int len = numbers.size( ) ;
+ if ( numbers[len - 2] == numbers[len - 1] ) {
+ numbers.pop_back( ) ;
+ numbers.pop_back( ) ;
+ }
+ else {
+ numbers[ len - 1 ] = numbers[ len - 1 ] - numbers[ len - 2 ] ;
+ numbers.erase( numbers.begin( ) + len - 2 ) ;
+ }
+ }
+ if ( numbers.size( ) == 1 ) {
+ std::cout << *numbers.begin( ) ;
+ }
+ else {
+ std::cout << 0 ;
+ }
+ std::cout << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-306/ulrich-rieke/haskell/ch-1.hs b/challenge-306/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..9820922ba0 --- /dev/null +++ b/challenge-306/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,17 @@ +module Challenge306
+ where
+import Data.List.Split( divvy )
+
+findSubarraySum :: [Int] -> Int -> Int
+findSubarraySum list subarraylen = if (subarraylen == 1 || subarraylen == length
+ list) then sum list else sum $ map sum $ divvy subarraylen 1 list
+
+solution :: [Int] -> Int
+solution list = sum $ map (\i -> findSubarraySum list i) $ filter odd [1..
+ length list]
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some positive integers separated by whitespace!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-306/ulrich-rieke/haskell/ch-2.hs b/challenge-306/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..de6da9d79f --- /dev/null +++ b/challenge-306/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,25 @@ +module Challenge306_2
+ where
+import Data.List( sort , (!!) )
+
+convert :: [Int] -> [Int]
+convert list =
+ let sorted = sort list
+ len = length sorted
+ in if sorted !! ( len - 1 ) == sorted !! ( len - 2 ) then take ( len - 2)
+ sorted else sort ( take ( len - 2 ) sorted ++ [last sorted -
+ ( sorted !! ( len - 2 ))] )
+
+solution :: [Int] -> Int
+solution list = if length result == 1 then head result else 0
+ where
+ result :: [Int]
+ result = until (\l -> length l == 1 || length l == 0 ) convert list
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by whitespace!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
+
+
diff --git a/challenge-306/ulrich-rieke/perl/ch-1.pl b/challenge-306/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..2131c8cabd --- /dev/null +++ b/challenge-306/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( sum ) ;
+
+sub find_subarray_sum {
+ my $array = shift ;
+ my $len = shift ;
+ if ( $len == 1 || $len == scalar( @$array ) ) {
+ return sum( @$array ) ;
+ }
+ else {
+ my $total = 0 ;
+ my $arraylen = scalar( @$array ) ;
+ for my $start( 0..$arraylen - $len) {
+ my $currentSum = sum( @{$array}[$start..$start + $len - 1] ) ;
+ $total += $currentSum ;
+ }
+ return $total ;
+ }
+}
+
+say "Enter some positive integers separated by whitespace!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my @sums ;
+for my $i( 1..scalar( @numbers ) ) {
+ if ( $i % 2 == 1 ) {
+ push( @sums , find_subarray_sum( \@numbers , $i )) ;
+ }
+}
+say sum( @sums ) ;
diff --git a/challenge-306/ulrich-rieke/perl/ch-2.pl b/challenge-306/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..ca19bf8a24 --- /dev/null +++ b/challenge-306/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter some integers separated by whitespace!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+while ( scalar( @numbers ) > 1 ) {
+ @numbers = sort { $a <=> $b } @numbers ;
+ if ( $numbers[-1] == $numbers[-2] ) {
+ pop @numbers ;
+ pop @numbers ;
+ }
+ else {
+ $numbers[ -1] = $numbers[ -1 ] - $numbers[-2] ;
+ my $len = scalar( @numbers ) ;
+ splice( @numbers , $len - 2 , 1 ) ;
+ }
+}
+if ( scalar( @numbers ) == 1 ) {
+ say $numbers[0] ;
+}
+else {
+ say 0 ;
+}
diff --git a/challenge-306/ulrich-rieke/raku/ch-1.raku b/challenge-306/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..d95fe30ef0 --- /dev/null +++ b/challenge-306/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,28 @@ +use v6 ;
+
+sub find_subarray_sum( @array , $l is copy ) {
+ if ( $l == @array.elems || $l == 1 ) {
+ return [+] @array ;
+ }
+ else {
+ my $len = @array.elems ;
+ my $total = 0 ;
+ for (0..$len - $l ) -> $start {
+ my $sum = [+] @array[$start .. $start + $l - 1] ;
+ $total += $sum ;
+ }
+ return $total ;
+ }
+}
+
+say "Enter some positive integers separated by whitespace!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my @sums ;
+my @lengths = (1..@numbers.elems ) ;
+for @lengths -> $len {
+ unless ( $len %% 2 ) {
+ @sums.push(find_subarray_sum( @numbers , $len ) ) ;
+ }
+}
+say [+] @sums ;
diff --git a/challenge-306/ulrich-rieke/raku/ch-2.raku b/challenge-306/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..9251a66f2b --- /dev/null +++ b/challenge-306/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,23 @@ +use v6 ;
+
+say "Enter some integers separated by whitespace!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+while ( @numbers.elems > 1 ) {
+ @numbers .= sort ;
+ my $len = @numbers.elems ;
+ if ( @numbers[$len - 2] == @numbers[$len - 1] ) {
+ @numbers.pop ;
+ @numbers.pop ;
+ }
+ else {
+ @numbers[ $len - 1 ] = @numbers[ $len - 1 ] - @numbers[ $len - 2] ;
+ @numbers.splice( $len - 2 , 1 ) ;
+ }
+}
+if ( @numbers.elems == 1 ) {
+ say @numbers[0] ;
+}
+else {
+ say 0 ;
+}
diff --git a/challenge-306/ulrich-rieke/rust/ch-1.rs b/challenge-306/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..ab7a170c41 --- /dev/null +++ b/challenge-306/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,31 @@ +use std::io ; + +fn sum_of_subarrays( numbers : &Vec<u32> , l : usize ) -> u32 { + if l == numbers.len( ) || l == 1 { + numbers.iter( ).sum( ) + } + else { + let nu_slice = &numbers[..] ; + let mut iter = nu_slice.windows( l ) ; + let mut total : u32 = 0 ; + while let Some( wi ) = iter.next( ) { + let a_sum : u32 = wi.iter( ).sum::<u32>( ) ; + total += a_sum ; + } + total + } +} + +fn main() { + println!("Enter some positive integers separated by whitespace!"); + 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 len : usize = numbers.len( ) ; + let lengths : Vec<usize> = (1..=len).filter( |&d| d % 2 == 1 ).collect( ) ; + let mut subarraysums : Vec<u32> = Vec::new( ) ; + lengths.into_iter( ).map( |l| sum_of_subarrays( &numbers , l ) ).for_each( + |n| subarraysums.push( n ) ) ; + println!("{}" , subarraysums.into_iter( ).sum::<u32>( ) ) ; +} diff --git a/challenge-306/ulrich-rieke/rust/ch-2.rs b/challenge-306/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..3656912b71 --- /dev/null +++ b/challenge-306/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,27 @@ +use std::io ; + +fn main() { + println!("Enter some positive integers separated by whitespace!") ; + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let mut numbers : Vec<u32> = inline.trim( ).split_whitespace( ).map( + |s| s.parse::<u32>( ).unwrap( ) ).collect( ) ; + while numbers.len( ) > 1 { + numbers.sort( ) ; + let len = numbers.len( ) ; + if numbers[len - 2] == numbers[ len - 1] { + numbers.pop( ) ; + numbers.pop( ) ; + } + else { + numbers[len - 1] = numbers[len - 1] - numbers[len - 2] ; + numbers.remove( len - 2 ) ; + } + } + if numbers.len( ) == 1 { + println!("{}" , numbers[0] ) ; + } + else { + println!("0") ; + } +} diff --git a/stats/pwc-challenge-304.json b/stats/pwc-challenge-304.json index 941b7f565e..a4ee454370 100644 --- a/stats/pwc-challenge-304.json +++ b/stats/pwc-challenge-304.json @@ -1,8 +1,18 @@ { + "subtitle" : { + "text" : "[Champions: 26] Last updated at 2025-01-28 10:41:28 GMT" + }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + }, + "borderWidth" : 0 + } + }, "series" : [ { - "colorByPoint" : 1, - "name" : "The Weekly Challenge - 304", "data" : [ { "drilldown" : "Andreas Mahnke", @@ -11,13 +21,13 @@ }, { "drilldown" : "Arne Sommer", - "y" : 3, - "name" : "Arne Sommer" + "name" : "Arne Sommer", + "y" : 3 }, { - "drilldown" : "Athanasius", + "name" : "Athanasius", "y" : 4, - "name" : "Athanasius" + "drilldown" : "Athanasius" }, { "name" : "BarrOff", @@ -26,32 +36,32 @@ }, { "drilldown" : "Bob Lied", - "y" : 2, - "name" : "Bob Lied" + "name" : "Bob Lied", + "y" : 2 }, { - "y" : 3, "drilldown" : "Dave Jacoby", - "name" : "Dave Jacoby" + "name" : "Dave Jacoby", + "y" : 3 }, { + "name" : "David Ferrone", "y" : 2, - "drilldown" : "David Ferrone", - "name" : "David Ferrone" + "drilldown" : "David Ferrone" }, { "drilldown" : "E. Choroba", - "y" : 2, - "name" : "E. Choroba" + "name" : "E. Choroba", + "y" : 2 }, { - "name" : "Feng Chang", + "drilldown" : "Feng Chang", "y" : 2, - "drilldown" : "Feng Chang" + "name" : "Feng Chang" }, { - "y" : 5, "drilldown" : "Jaldhar H. Vyas", + "y" : 5, "name" : "Jaldhar H. Vyas" }, { @@ -60,14 +70,19 @@ "drilldown" : "Jan Krnavek" }, { - "name" : "Jorg Sommrey", "y" : 3, + "name" : "Jorg Sommrey", "drilldown" : "Jorg Sommrey" }, { - "y" : 4, + "drilldown" : "Lubos Kolouch", + "name" : "Lubos Kolouch", + "y" : 2 + }, + { "drilldown" : "Luca Ferrari", - "name" : "Luca Ferrari" + "name" : "Luca Ferrari", + "y" : 4 }, { "drilldown" : "Mark Anderson", @@ -75,23 +90,23 @@ "name" : "Mark Anderson" }, { - "drilldown" : "Matthias Muth", + "name" : "Matthias Muth", "y" : 3, - "name" : "Matthias Muth" + "drilldown" : "Matthias Muth" }, { - "y" : 3, "drilldown" : "Peter Campbell Smith", + "y" : 3, "name" : "Peter Campbell Smith" }, { + "name" : "Peter Meszaros", "y" : 2, - "drilldown" : "Peter Meszaros", - "name" : "Peter Meszaros" + "drilldown" : "Peter Meszaros" }, { - "y" : 3, "drilldown" : "Robbie Hatley", + "y" : 3, "name" : "Robbie Hatley" }, { @@ -100,9 +115,9 @@ "drilldown" : "Robert Ransbottom" }, { - "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West", "y" : 5, - "name" : "Roger Bell_West" + "drilldown" : "Roger Bell_West" }, { "drilldown" : "Santiago Leyva", @@ -115,55 +130,47 @@ "drilldown" : "Thomas Kohler" }, { - "drilldown" : "Ulrich Rieke", "y" : 4, - "name" : "Ulrich Rieke" + "name" : "Ulrich Rieke", + "drilldown" : "Ulrich Rieke" }, { + "drilldown" : "W. Luis Mochan", "name" : "W. Luis Mochan", - "y" : 3, - "drilldown" : "W. Luis Mochan" + "y" : 3 }, { + "name" : "Wanderdoc", "y" : 2, - "drilldown" : "Wanderdoc", - "name" : "Wanderdoc" + "drilldown" : "Wanderdoc" } - ] + ], + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 304" } ], "legend" : { "enabled" : 0 }, - "xAxis" : { - "type" : "category" - }, - "plotOptions" : { - "series" : { - "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - }, - "borderWidth" : 0 + "yAxis" : { + "title" : { + "text" : "Total Solutions" } }, - "chart" : { - "type" : "column" - }, "drilldown" : { "series" : [ { - "id" : "Andreas Mahnke", - "name" : "Andreas Mahnke", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Andreas Mahnke", + "name" : "Andreas Mahnke" }, { - "name" : "Arne Sommer", + "id" : "Arne Sommer", "data" : [ [ "Raku", @@ -174,11 +181,11 @@ 1 ] ], - "id" : "Arne Sommer" + "name" : "Arne Sommer" }, { - "id" : "Athanasius", "name" : "Athanasius", + "id" : "Athanasius", "data" : [ [ "Perl", @@ -191,27 +198,27 @@ ] }, { - "id" : "BarrOff", - "name" : "BarrOff", "data" : [ [ "Raku", 2 ] - ] + ], + "id" : "BarrOff", + "name" : "BarrOff" }, { + "name" : "Bob Lied", "data" : [ [ "Perl", 2 ] ], - "name" : "Bob Lied", "id" : "Bob Lied" }, { - "id" : "Dave Jacoby", + "name" : "Dave Jacoby", "data" : [ [ "Perl", @@ -222,39 +229,41 @@ 1 ] ], - "name" : "Dave Jacoby" + "id" : "Dave Jacoby" }, { "id" : "David Ferrone", - "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "David Ferrone" }, { - "id" : "E. Choroba", "data" : [ [ "Perl", 2 ] |
