diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-12-24 12:57:17 +0000 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-12-24 12:57:17 +0000 |
| commit | f6b563df807de57ef1db8cc3bf80ca55717dffaa (patch) | |
| tree | 2d69c61146d19d95adfc1055c6aa360883428705 | |
| parent | bccae08dc1de6129f4fa12834cba48b3beed4c2b (diff) | |
| download | perlweeklychallenge-club-f6b563df807de57ef1db8cc3bf80ca55717dffaa.tar.gz perlweeklychallenge-club-f6b563df807de57ef1db8cc3bf80ca55717dffaa.tar.bz2 perlweeklychallenge-club-f6b563df807de57ef1db8cc3bf80ca55717dffaa.zip | |
- Added solutions by E. Choroba.
- Added solutions by Robbie Hatley.
- Added solutions by Ulrich Rieke.
- Added solutions by Jan Krnavek.
- Added solutions by PokGoPun.
32 files changed, 2954 insertions, 2597 deletions
diff --git a/challenge-301/ulrich-rieke/cpp/ch-1.cpp b/challenge-301/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..b347a117f3 --- /dev/null +++ b/challenge-301/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,34 @@ +#include <vector>
+#include <string>
+#include <iostream>
+#include <sstream>
+#include <algorithm>
+#include <numeric>
+
+std::vector<std::string> split( const std::string & text , char delim ) {
+ std::istringstream istr { text } ;
+ std::vector<std::string> tokens ;
+ std::string word ;
+ while ( std::getline( istr , word , delim ) ) {
+ 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::sort( tokens.begin( ) , tokens.end( )) ;
+ std::vector<long> longnumbers ;
+ while ( std::next_permutation( tokens.begin( ) , tokens.end( ) ) ) {
+ std::string total = std::accumulate( tokens.begin( ) ,
+ tokens.end( ) , std::string( "" ) , []( auto a ,
+ auto b ){ return a.append( b ) ; } ) ;
+ longnumbers.push_back( std::stol( total ) ) ;
+ }
+ std::cout << *std::max_element( longnumbers.begin( ) ,
+ longnumbers.end( ) ) << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-301/ulrich-rieke/cpp/ch-2.cpp b/challenge-301/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..f8fbda404c --- /dev/null +++ b/challenge-301/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,72 @@ +#include <string>
+#include <vector>
+#include <sstream>
+#include <algorithm>
+#include <iostream>
+
+std::vector<std::string> split( const std::string & text , char delim ) {
+ std::istringstream istr { text } ;
+ std::vector<std::string> tokens ;
+ std::string word ;
+ while ( std::getline( istr , word , delim ) ) {
+ tokens.push_back( word ) ;
+ }
+ return tokens ;
+}
+
+std::string to_binary_string( int num ) {
+ std::string binary ;
+ while ( num != 0 ) {
+ int remainder = num % 2 ;
+ binary.append( std::to_string( remainder ) ) ;
+ num /= 2 ;
+ }
+ std::reverse( binary.begin( ) , binary.end( ) ) ;
+ return binary ;
+}
+
+//the Hamming distance is the difference in set bits between the orred
+//and the anded bitwise combination of 2 integers
+int hamming_dist( int a , int b ) {
+ int orred = a | b ;
+ int anded = a & b ;
+ std::string orstring { to_binary_string( orred ) } ;
+ std::string andstring { to_binary_string( anded ) } ;
+ auto orones = std::count( orstring.begin( ) , orstring.end( ) , '1' ) ;
+ auto andones = std::count( andstring.begin( ) , andstring.end( ) ,
+ '1' ) ;
+ return ( static_cast<int>( orones ) - static_cast<int>( andones ) ) ;
+}
+
+std::vector<std::vector<int>> create_two_combinations( const
+ std::vector<int> & numbers ) {
+ int len = static_cast<int>( numbers.size( ) ) ;
+ std::vector<std::vector<int>> combis_of_two ;
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ std::vector<int> nums ;
+ nums.push_back( numbers[i] ) ;
+ for ( int j = i + 1 ; j < len ; j++ ) {
+ nums.push_back( numbers[j] ) ;
+ combis_of_two.push_back( nums ) ;
+ nums.pop_back( ) ;
+ }
+ }
+ return combis_of_two ;
+}
+
+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 ) ) ;
+ int sum = 0 ;
+ auto combis { create_two_combinations( numbers ) } ;
+ for ( auto vec : combis ) {
+ sum += hamming_dist( vec[0] , vec[1] ) ;
+ }
+ std::cout << sum << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-301/ulrich-rieke/haskell/ch-1.hs b/challenge-301/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..837c0c14b6 --- /dev/null +++ b/challenge-301/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,15 @@ +module Challenge301
+ where
+import Data.List ( permutations ) ;
+
+convert :: [Int] -> Integer
+convert list = read $ foldl1 ( ++ ) $ map show list
+
+solution :: [Int] -> Integer
+solution list = maximum $ map convert $ permutations list
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by whitespace!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-301/ulrich-rieke/haskell/ch-2.hs b/challenge-301/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..03cfa6238d --- /dev/null +++ b/challenge-301/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,27 @@ +module Challenge301_2
+ where
+import Data.Bits
+import qualified Data.Set as S
+
+combinations :: Int -> [a] -> [[a]]
+combinations 0 _ = [[]]
+combinations n xs = [ xs !! i : x | i <- [0..(length xs ) - 1 ] ,
+ x <- combinations (n - 1 ) ( drop ( i + 1 ) xs ) ]
+
+hamming_distance :: Int -> Int -> Int
+hamming_distance first second =
+ let orred = first .|. second
+ anded = first .&. second
+ in popCount orred - popCount anded
+
+solution :: [Int] -> Int
+solution list =
+ let allCombis = S.toList $ S.fromList $ combinations 2 list
+ in sum $ map (\subli -> hamming_distance ( head subli ) ( last subli ) )
+ allCombis
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by whitespace!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-301/ulrich-rieke/perl/ch-1.pl b/challenge-301/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..6eef120451 --- /dev/null +++ b/challenge-301/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use Algorithm::Combinatorics qw ( permutations ) ;
+use List::Util qw ( max ) ;
+
+say "Enter some integers separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my $total ;
+my @allTotals ;
+my $iter = permutations( \@numbers ) ;
+while ( my $c = $iter->next ) {
+ map { $total .= $_ } @$c ;
+ push( @allTotals , $total ) ;
+ $total = ( ) ;
+}
+say max( @allTotals ) ;
diff --git a/challenge-301/ulrich-rieke/perl/ch-2.pl b/challenge-301/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..f1bafcf532 --- /dev/null +++ b/challenge-301/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use Algorithm::Combinatorics qw ( combinations ) ;
+
+#the Hamming distance is equivalent to the difference in ones between
+#the orred and the anded combination of the 2 numbers
+sub distance {
+ my $firstNum = shift ;
+ my $secondNum = shift ;
+ $firstNum += 0 ; #enforce numberlike behaviour
+ $secondNum += 0 ;#same here
+ my $orred = $firstNum | $secondNum ;#otherwise, it will produce
+#nonsense right here !
+ my $anded = $firstNum & $secondNum ; #same here....
+ my $orstring = sprintf( "%b" , $orred ) ;
+ my $andstring = sprintf( "%b" , $anded ) ;
+ my $orones = scalar( grep { $_ eq '1' } split( // , $orstring ) ) ;
+ my $andones = scalar( grep { $_ eq '1' } split( // , $andstring ) ) ;
+ return ( $orones - $andones ) ;
+}
+
+say "Enter some integers separated by whitespace!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my $iter = combinations( \@numbers , 2 ) ;
+my $sum = 0 ;
+while ( my $c = $iter->next ) {
+ $sum += distance( $c->[0] , $c->[1] ) ;
+}
+say $sum ;
diff --git a/challenge-301/ulrich-rieke/raku/ch-1.raku b/challenge-301/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..aa3ffd383c --- /dev/null +++ b/challenge-301/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,17 @@ +use v6 ;
+
+say "Enter some integers separated by whitespace!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my $len = @numbers.elems ;
+my @permus = @numbers.permutations ;
+my @allTotals ;
+for @permus -> $permu {
+ my $total ;
+ for ( 0..$len - 1 ) -> $i {
+ $total ~= ~$permu[$i] ;
+ }
+ @allTotals.push( $total ) ;
+}
+my @totalNums = @allTotals.map( {.Int} ) ;
+say @totalNums.max ;
diff --git a/challenge-301/ulrich-rieke/raku/ch-2.raku b/challenge-301/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..393154dee3 --- /dev/null +++ b/challenge-301/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,23 @@ +use v6 ;
+
+#the Hamming distance equals the difference in the number of ones in the
+#bitwise orred number - the bitwise anded number
+sub distance( Int $first , Int $second ) {
+ my $orred = $first +| $second ;
+ my $anded = $first +& $second ;
+ my $firstString = $orred.base( 2 ) ;
+ my $secondString = $anded.base( 2 ) ;
+ my $firstOnes = $firstString.comb.grep( {$_ eq '1'} ).elems ;
+ my $secondOnes = $secondString.comb.grep( {$_ eq '1'} ).elems ;
+ return ( $firstOnes - $secondOnes ) ;
+}
+
+say "Enter some integers separated by whitespace!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my @combis = @numbers.combinations( 2 ) ;
+my $sum = 0 ;
+for @combis -> $seq {
+ $sum += distance( $seq[0] , $seq[1] ) ;
+}
+say $sum ;
diff --git a/challenge-301/ulrich-rieke/rust/ch-1.rs b/challenge-301/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..d59f7f0d85 --- /dev/null +++ b/challenge-301/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,22 @@ +use std::io ; +use itertools::Itertools ; + +fn main() { + println!("Enter some integers separated by whitespace!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let numbers : Vec<u16> = inline.trim( ).split_whitespace( ).map( + |s| s.parse::<u16>( ).unwrap( ) ).collect( ) ; + let len : usize = numbers.len( ) ; + let mut longies : Vec<u64> = Vec::new( ) ; + for vec in numbers.into_iter( ).permutations( len ) { + let mut total : String = String::new( ) ; + for n in vec { + let numstring : String = n.to_string( ) ; + total.push_str( & numstring ) ; + } + let a_long_number : u64 = total.parse::<u64>( ).unwrap( ) ; + longies.push( a_long_number ) ; + } + println!("{}" , longies.into_iter( ).max( ).unwrap( ) ) ; +} diff --git a/challenge-301/ulrich-rieke/rust/ch-2.rs b/challenge-301/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..b6425fbf8e --- /dev/null +++ b/challenge-301/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,26 @@ +use std::io ; +use itertools::Itertools ; + +//the Hamming distance is the difference in the number of ones between +//the number that is created by bitwise orring and bitwise anding two +//numbers + +fn distance( first : u32 , second : u32 ) -> u32 { + let orred : u32 = first | second ; + let anded : u32 = first & second ; + orred.count_ones( ) - anded.count_ones( ) +} + +fn main() { + println!("Enter some 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 mut sum : u32 = 0 ; + for vec in numbers.into_iter( ).combinations( 2 ) { + let dist : u32 = distance( vec[0] , vec[1] ) ; + sum += dist ; + } + println!("{}" , sum ) ; +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index afc5d123a9..83dd0479c4 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,22 +1,96 @@ { + "chart" : { + "type" : "column" + }, + "xAxis" : { + "type" : "category" + }, "legend" : { "enabled" : 0 }, + "series" : [ + { + "data" : [ + { + "y" : 2, + "name" : "E. Choroba", + "drilldown" : "E. Choroba" + }, + { + "name" : "Jan Krnavek", + "y" : 2, + "drilldown" : "Jan Krnavek" + }, + { + "drilldown" : "Mark Anderson", + "name" : "Mark Anderson", + "y" : 2 + }, + { + "drilldown" : "Peter Meszaros", + "name" : "Peter Meszaros", + "y" : 2 + }, + { + "y" : 3, + "name" : "Robbie Hatley", + "drilldown" : "Robbie Hatley" + }, + { + "name" : "Thomas Kohler", + "y" : 4, + "drilldown" : "Thomas Kohler" + }, + { + "drilldown" : "Ulrich Rieke", + "name" : "Ulrich Rieke", + "y" : 4 + }, + { + "drilldown" : "W. Luis Mochan", + "y" : 3, + "name" : "W. Luis Mochan" + } + ], + "name" : "The Weekly Challenge - 301", + "colorByPoint" : 1 + } + ], "drilldown" : { "series" : [ { "data" : [ [ - "Raku", + "Perl", 2 ] ], + "name" : "E. Choroba", + "id" : "E. Choroba" + }, + { + "id" : "Jan Krnavek", + "name" : "Jan Krnavek", + "data" : [ + [ + "Raku", + 2 + ] + ] + }, + { "id" : "Mark Anderson", - "name" : "Mark Anderson" + "name" : "Mark Anderson", + "data" : [ + [ + "Raku", + 2 + ] + ] }, { - "name" : "Peter Meszaros", "id" : "Peter Meszaros", + "name" : "Peter Meszaros", "data" : [ [ "Perl", @@ -25,7 +99,20 @@ ] }, { - "id" : "Thomas Kohler", + "name" : "Robbie Hatley", + "id" : "Robbie Hatley", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { "data" : [ [ "Perl", @@ -36,11 +123,24 @@ 2 ] ], + "id" : "Thomas Kohler", "name" : "Thomas Kohler" }, { - "name" : "W. Luis Mochan", - "id" : "W. Luis Mochan", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "id" : "Ulrich Rieke", + "name" : "Ulrich Rieke" + }, + { "data" : [ [ "Perl", @@ -50,59 +150,24 @@ "Blog", 1 ] - ] + ], + "id" : "W. Luis Mochan", + "name" : "W. Luis Mochan" } ] }, - "chart" : { - "type" : "column" - }, - "tooltip" : { - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", - "followPointer" : 1, - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>" - }, - "subtitle" : { - "text" : "[Champions: 4] Last updated at 2024-12-23 15:41:29 GMT" - }, "yAxis" : { "title" : { "text" : "Total Solutions" } }, - "xAxis" : { - "type" : "category" + "subtitle" : { + "text" : "[Champions: 8] Last updated at 2024-12-24 12:57:13 GMT" }, - "series" : [ - { - "colorByPoint" : 1, - "data" : [ - { - "y" : 2, - "drilldown" : "Mark Anderson", - "name" : "Mark Anderson" - }, - { - "drilldown" : "Peter Meszaros", - "y" : 2, - "name" : "Peter Meszaros" - }, - { - "y" : 4, - "drilldown" : "Thomas Kohler", - "name" : "Thomas Kohler" - }, - { - "y" : 3, - "drilldown" : "W. Luis Mochan", - "name" : "W. Luis Mochan" - } - ], - "name" : "The Weekly Challenge - 301" - } - ], - "title" : { - "text" : "The Weekly Challenge - 301" + "tooltip" : { + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", + "followPointer" : 1, + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>" }, "plotOptions" : { "series" : { @@ -112,5 +177,8 @@ }, "borderWidth" : 0 } + }, + "title" : { + "text" : "The Weekly Challenge - 301" } } diff --git a/stats/pwc-language-breakdown-2019.json b/stats/pwc-language-breakdown-2019.json index b317a16cca..82caaf0062 100644 --- a/stats/pwc-language-breakdown-2019.json +++ b/stats/pwc-language-breakdown-2019.json @@ -1,23 +1,10 @@ { - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2024-12-23 15:41:29 GMT" - }, - "tooltip" : { - "followPointer" : "true", - "pointFormat" : "<span style=\"color:{point.color}\">Challenge {point.name}</span>: <b>{point.y:f}</b><br/>", - "headerFormat" : "<span style=\"font-size:11px\"></span>" + "xAxis" : { + "type" : "category" }, "chart" : { "type" : "column" }, - "legend" : { - "enabled" : "false" - }, "drilldown" : { "series" : [ { @@ -35,12 +22,10 @@ 9 ] ], - "id" : "041", - "name" : "041" + "name" : "041", + "id" : "041" }, { - "name" : "040", - "id" : "040", "data" : [ [ "Perl", @@ -54,10 +39,13 @@ "Blog", 10 ] - ] + ], + "id" : "040", + "name" : "040" }, { "name" : "039", + "id" : "039", "data" : [ [ "Perl", @@ -71,11 +59,9 @@ "Blog", 12 ] - ], - "id" : "039" + ] }, { - "name" : "038", "data" : [ [ "Perl", @@ -90,9 +76,12 @@ 12 ] ], + "name" : "038", "id" : "038" }, { + "id" : "037", + "name" : "037", "data" : [ [ "Perl", @@ -106,9 +95,7 @@ "Blog", 9 ] - ], - "id" : "037", - "name" : "037" + ] }, { "name" : "036", @@ -130,6 +117,7 @@ }, { "id" : "035", + "name" : "035", "data" : [ [ "Perl", @@ -143,8 +131,7 @@ "Blog", 9 ] - ], - "name" : "035" + ] }, { "data" : [ @@ -165,7 +152,6 @@ "name" : "034" }, { - "id" : "033", "data" : [ [ "Perl", @@ -180,10 +166,12 @@ 10 ] ], - "name" : "033" + "name" : "033", + "id" : "033" }, { "id" : "032", + "name" : "032", "data" : [ [ "Perl", @@ -197,12 +185,11 @@ "Blog", 10 ] - ], - "name" : "032" + ] }, { - "name" : "031", "id" : "031", + "name" : "031", "data" : [ [ "Perl", @@ -220,6 +207,7 @@ }, { "name" : "030", + "id" : "030", "data" : [ [ "Perl", @@ -233,10 +221,10 @@ "Blog", 10 ] - ], - "id" : "030" + ] }, { + "id" : "029", "name" : "029", "data" : [ [ @@ -251,11 +239,11 @@ "Blog", 12 ] - ], - "id" : "029" + ] }, { "name" : "028", + "id" : "028", "data" : [ [ "Perl", @@ -269,10 +257,10 @@ "Blog", 9 ] - ], - "id" : "028" + ] }, { + "name" : "027", "id" : "027", "data" : [ [ @@ -287,12 +275,9 @@ "Blog", 9 ] - ], - "name" : "027" + ] }, { - "name" : "026", - "id" : "026", "data" : [ [ "Perl", @@ -306,9 +291,13 @@ "Blog", 10 ] - ] + ], + "name" : "026", + "id" : "026" }, { + "id" : "025", + "name" : "025", "data" : [ [ "Perl", @@ -322,13 +311,9 @@ "Blog", 12 ] - ], - "id" : "025", - "name" : "025" + ] }, { - "name" : "024", - "id" : "024", "data" : [ [ "Perl", @@ -342,7 +327,9 @@ "Blog", 11 ] - ] + ], + "name" : "024", + "id" : "024" }, { "data" : [ @@ -359,11 +346,10 @@ 12 ] ], - "id" : "023", - "name" : "023" + "name" : "023", + "id" : "023" }, { - "id" : "022", "data" : [ [ "Perl", @@ -378,10 +364,10 @@ 10 ] ], - "name" : "022" + "name" : "022", + "id" : "022" }, { - "name" : "021", "data" : [ [ "Perl", @@ -396,9 +382,11 @@ 10 ] ], - "id" : |
