diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-02-10 20:39:47 +0000 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-02-10 20:39:47 +0000 |
| commit | e9d030e5fd8fa107797fac1489bca6ce2724d212 (patch) | |
| tree | 67b923088c999951deb2282143353932210d2cdf | |
| parent | a4ee687a84267a19ebcb6d9228ffe4f73927396b (diff) | |
| download | perlweeklychallenge-club-e9d030e5fd8fa107797fac1489bca6ce2724d212.tar.gz perlweeklychallenge-club-e9d030e5fd8fa107797fac1489bca6ce2724d212.tar.bz2 perlweeklychallenge-club-e9d030e5fd8fa107797fac1489bca6ce2724d212.zip | |
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke.
- Added solutions by Mark Anderson.
- Added solutions by David Ferrone.
- Added solutions by Ali Moradi.
- Added solutions by Niels van Dijke.
- Added solutions by Peter Pentchev.
- Added solutions by E. Choroba.
- Added solutions by Peter Meszaros.
- Added solutions by Thomas Kohler.
- Added solutions by W. Luis Mochan.
- Added solutions by BarrOff.
38 files changed, 3762 insertions, 3248 deletions
diff --git a/challenge-308/eric-cheung/python/ch-1.py b/challenge-308/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..e05d16d0b8 --- /dev/null +++ b/challenge-308/eric-cheung/python/ch-1.py @@ -0,0 +1,16 @@ +
+## Example 1
+## arrStr_01 = ["perl", "weekly", "challenge"]
+## arrStr_02 = ["raku", "weekly", "challenge"]
+
+## Example 2
+## arrStr_01 = ["perl", "raku", "python"]
+## arrStr_02 = ["python", "java"]
+
+## Example 3
+arrStr_01 = ["guest", "contribution"]
+arrStr_02 = ["fun", "weekly", "challenge"]
+
+arrCommon = [strLoop for strLoop in arrStr_01 if strLoop in arrStr_02]
+
+print (len(arrCommon))
diff --git a/challenge-308/eric-cheung/python/ch-2.py b/challenge-308/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..3aaa8972ac --- /dev/null +++ b/challenge-308/eric-cheung/python/ch-2.py @@ -0,0 +1,19 @@ +
+## Ref.
+## If c = a ^ b, then a = c ^ b or a = b ^ c
+## Similarly, b = c ^ a or b = a ^ c
+
+## Example 1
+## arrEncoded = [1, 2, 3]
+## nInitial = 1
+
+## Example 2
+arrEncoded = [6, 2, 7, 3]
+nInitial = 4
+
+arrOutput = [nInitial]
+
+for nElem in arrEncoded:
+ arrOutput.append(arrOutput[-1] ^ nElem)
+
+print (arrOutput)
diff --git a/challenge-308/perlboy1967/perl/ch1.pl b/challenge-308/perlboy1967/perl/ch-1.pl index c9009fa9f2..c9009fa9f2 100755 --- a/challenge-308/perlboy1967/perl/ch1.pl +++ b/challenge-308/perlboy1967/perl/ch-1.pl diff --git a/challenge-308/perlboy1967/perl/ch2.pl b/challenge-308/perlboy1967/perl/ch-2.pl index 11b87c4cc7..11b87c4cc7 100755 --- a/challenge-308/perlboy1967/perl/ch2.pl +++ b/challenge-308/perlboy1967/perl/ch-2.pl diff --git a/challenge-308/ulrich-rieke/cpp/ch-1.cpp b/challenge-308/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..409cea5b7c --- /dev/null +++ b/challenge-308/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,39 @@ +#include <iostream>
+#include <string>
+#include <sstream>
+#include <vector>
+#include <map>
+
+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 words separated by whitespace!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto firstTokens { split( line , ' ' ) } ;
+ std::string secondline ;
+ std::cout << "Enter some more words separated by whitespace!\n" ;
+ std::getline( std::cin , secondline ) ;
+ auto secondTokens { split( secondline , ' ' ) } ;
+ std::map<std::string , int> firstFrequency , secondFrequency ;
+ int common = 0 ;
+ for ( auto s : firstTokens )
+ firstFrequency[s]++ ;
+ for ( auto s : secondTokens )
+ secondFrequency[s]++ ;
+ for ( auto aPair : firstFrequency ) {
+ if ( secondFrequency.find( aPair.first ) != secondFrequency.end( ) ) {
+ common++ ;
+ }
+ }
+ std::cout << common << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-308/ulrich-rieke/cpp/ch-2.cpp b/challenge-308/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..8a80cf6fc5 --- /dev/null +++ b/challenge-308/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,42 @@ +#include <iostream>
+#include <string>
+#include <sstream>
+#include <vector>
+
+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 numbers separated by whitespace!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> encoded ;
+ for ( auto s : tokens )
+ encoded.push_back( std::stoi( s ) ) ;
+ std::cout << "Enter an initial integer!\n" ;
+ int initial ;
+ std::cin >> initial ;
+ std::vector<int> decoded ;
+ decoded.push_back( initial ) ;
+ //to find x in a xor x = b you can do a xor b since xor is its own
+ //inverse function!
+ for ( auto it = encoded.begin( ) ; it != encoded.end( ) ; ++it ) {
+ int last = decoded[decoded.size( ) - 1] ;
+ decoded.push_back( last ^ *it ) ;
+ }
+ std::cout << "( " ;
+ for ( int i : decoded ) {
+ std::cout << i << ' ' ;
+ }
+ std::cout << ")\n" ;
+ return 0 ;
+}
+
diff --git a/challenge-308/ulrich-rieke/haskell/ch-1.hs b/challenge-308/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..fa5117eb64 --- /dev/null +++ b/challenge-308/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,16 @@ +module Challenge308
+ where
+import qualified Data.Set as S
+
+solution :: [String] -> [String] -> Int
+solution firstWords secondWords = S.size $ S.intersection ( S.fromList
+ firstWords ) ( S.fromList secondWords )
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some words separated by whitespace!"
+ firstLine <- getLine
+ putStrLn "Enter some more words separated by whitespace!"
+ secondLine <- getLine
+ print $ solution ( words firstLine ) ( words secondLine )
+
diff --git a/challenge-308/ulrich-rieke/haskell/ch-2.hs b/challenge-308/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..b6400ec04b --- /dev/null +++ b/challenge-308/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,18 @@ +module Challenge308_2
+ where
+import Data.Bits ( xor )
+
+--we take advantage of the fact that xor is its own inverse function
+solution :: [Int] -> Int -> [Int]
+solution encoded initial = scanl1 xor newList
+ where
+ newList = initial : encoded
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by whitespace!"
+ numberline <- getLine
+ putStrLn "Enter an initial value!"
+ value <- getLine
+ print $ solution ( map read $ words numberline ) ( read value )
+
diff --git a/challenge-308/ulrich-rieke/perl/ch-1.pl b/challenge-308/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..34b80238fb --- /dev/null +++ b/challenge-308/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter some words separated by whitespace!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @firstWords = split( /\s/ , $line ) ;
+say "Enter some more words separated by whitespace!" ;
+$line = <STDIN> ;
+chomp $line ;
+my @secondWords = split( /\s/ , $line ) ;
+my %firstHash ;
+map { $firstHash{$_}++ } @firstWords ;
+my %secondHash ;
+map { $secondHash{$_}++ } @secondWords ;
+my $common = 0 ;
+for my $word ( keys %firstHash ) {
+ if ( exists( $secondHash{ $word } ) ) {
+ $common++ ;
+ }
+}
+say $common ;
diff --git a/challenge-308/ulrich-rieke/perl/ch-2.pl b/challenge-308/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..7b4d400a45 --- /dev/null +++ b/challenge-308/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter some integers separated by whitespace!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @encoded = split( /\s/ , $line ) ;
+say "Enter an integer!" ;
+my $num = <STDIN> ;
+chomp $num ;
+#we can use xor as its own inverse function so to find x in a xor x = b
+#we can do a xor b!
+my @decoded = ($num) ;
+for my $number ( @encoded ) {
+ my $last = $decoded[-1] ;
+#by adding zero I enforce numberlike behaviour ; simply applying ^ as
+#operator doesn't produce results reliably!!!
+ push(@decoded , ($last + 0) ^ ($number + 0)) ;
+}
+say '(' . join( ',' , @decoded ) . ')' ;
diff --git a/challenge-308/ulrich-rieke/raku/ch-1.raku b/challenge-308/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..e5d7ded475 --- /dev/null +++ b/challenge-308/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,19 @@ +use v6 ;
+
+say "Enter some words separated by whitespace!" ;
+my $line = $*IN.get ;
+my @firstWords = $line.words ;
+say "Enter some more words separated by whitespace!" ;
+$line = $*IN.get ;
+my @secondWords = $line.words ;
+my %firstHash ;
+@firstWords.map( {%firstHash{$_}++} ) ;
+my %secondHash ;
+@secondWords.map( {%secondHash{$_}++} ) ;
+my $common = 0 ;
+for %firstHash.keys -> $word {
+ if ( %secondHash{$word}:exists ) {
+ $common++ ;
+ }
+}
+say $common ;
diff --git a/challenge-308/ulrich-rieke/raku/ch-2.raku b/challenge-308/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..07c1f81a52 --- /dev/null +++ b/challenge-308/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,17 @@ +use v6 ;
+
+say "Enter some integers separated by whitespace!" ;
+my $line = $*IN.get ;
+my @encoded = $line.words.map( {.Int} ) ;
+say "Enter an integer!" ;
+$line = $*IN.get ;
+my $number = $line.Int ;
+#if we are given a xor b = c and we are given only a and c , we can find
+#b by a xor c since xor is its own inverse function!
+my @decoded ;
+@decoded.push( $number ) ;
+for @encoded -> $num {
+ my $lastNum = @decoded[*-1] ;
+ @decoded.push( $num +^ $lastNum ) ;
+}
+say '(' ~ @decoded.join( ',' ) ~ ')' ;
diff --git a/challenge-308/ulrich-rieke/rust/ch-1.rs b/challenge-308/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..83f6265f11 --- /dev/null +++ b/challenge-308/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,21 @@ +use std::io ; + +fn main() { + println!("Enter some strings separated by whitespace!"); + let mut firstline : String = String::new( ) ; + io::stdin( ).read_line( &mut firstline ).unwrap( ) ; + println!("Enter some more strings separated by whitespace!" ) ; + let mut secondline : String = String::new( ) ; + io::stdin( ).read_line( &mut secondline ).unwrap( ) ; + let firstwords : Vec<&str> = firstline.trim( ).split_whitespace( ). + collect( ) ; + let secondwords : Vec<&str> = secondline.trim( ).split_whitespace( ). + collect( ) ; + let mut common : usize = 0 ; + for w in firstwords { + if secondwords.contains( &w ) { + common += 1 ; + } + } + println!("{}" , common) ; +} diff --git a/challenge-308/ulrich-rieke/rust/ch-2.rs b/challenge-308/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..d603360e06 --- /dev/null +++ b/challenge-308/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,24 @@ +use std::io ; + +fn main() { + println!("Enter some integers separated by whitespace!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + println!("Enter an initial integer!") ; + let mut numline : String = String::new( ) ; + io::stdin( ).read_line( &mut numline ).unwrap( ) ; + let encoded : Vec<u16> = inline.trim( ).split_whitespace( ).map( |s| + s.parse::<u16>( ).unwrap( ) ).collect( ) ; + let ini_value : u16 = numline.trim( ).parse::<u16>( ).unwrap( ) ; + let mut decoded : Vec<u16> = Vec::new( ) ; + //the basic idea behind the solution is that xor serves as its own + //inverse function! so if we know a xor b = c and we are given a and c + // b is a xor c! We are given an initial value and so can construct the + //other values + decoded.push( ini_value ) ; + for i in encoded { + let last : u16 = decoded[ decoded.len( ) - 1] ; + decoded.push( i ^ last ) ; + } + println!("{:?}" , decoded ) ; +} diff --git a/stats/pwc-challenge-307.json b/stats/pwc-challenge-307.json new file mode 100644 index 0000000000..194489719e --- /dev/null +++ b/stats/pwc-challenge-307.json @@ -0,0 +1,479 @@ +{ + "subtitle" : { + "text" : "[Champions: 25] Last updated at 2025-02-10 20:38:56 GMT" + }, + "xAxis" : { + "type" : "category" + }, + "tooltip" : { + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", + "followPointer" : 1, + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" + }, + "title" : { + "text" : "The Weekly Challenge - 307" + }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + }, + "borderWidth" : 0 + } + }, + "series" : [ + { + "colorByPoint" : 1, + "data" : [ + { + "name" : "Ali Moradi", + "y" : 3, + "drilldown" : "Ali Moradi" + }, + { + "drilldown" : "Andreas Mahnke", + "name" : "Andreas Mahnke", + "y" : 2 + }, + { + "drilldown" : "Arne Sommer", + "y" : 3, + "name" : "Arne Sommer" + }, + { + "y" : 4, + "name" : "Athanasius", + "drilldown" : "Athanasius" + }, + { + "name" : "BarrOff", + "y" : 2, + "drilldown" : "BarrOff" + }, + { + "drilldown" : "Bob Lied", + "name" : "Bob Lied", + "y" : 2 + }, + { + "name" : "Dave Jacoby", + "y" : 2, + "drilldown" : "Dave Jacoby" + }, + { + "y" : 2, + "name" : "David Ferrone", + "drilldown" : "David Ferrone" + }, + { + "y" : 2, + "name" : "E. Choroba", + "drilldown" : "E. Choroba" + }, + { + "name" : "Jaldhar H. Vyas", + "y" : 5, + "drilldown" : "Jaldhar H. Vyas" + }, + { + "drilldown" : "Jan Krnavek", + "name" : "Jan Krnavek", + "y" : 2 + }, + { + "y" : 3, + "name" : "Jorg Sommrey", + "drilldown" : "Jorg Sommrey" + }, + { + "drilldown" : "Lubos Kolouch", + "name" : "Lubos Kolouch", + "y" : 2 + }, + { + "drilldown" : "Mark Anderson", + "name" : "Mark Anderson", + "y" : 2 + }, + { + "y" : 3, + "name" : "Matthias Muth", + "drilldown" : "Matthias Muth" + }, + { + "drilldown" : "Peter Campbell Smith", + "y" : 3, + "name" : "Peter Campbell Smith" + }, + { + "drilldown" : "Peter Meszaros", + "y" : 2, + "name" : "Peter Meszaros" + }, + { + "y" : 2, + "name" : "Robert Ransbottom", + "drilldown" : "Robert Ransbottom" + }, + { + "name" : "Roger Bell_West", + "y" : 5, + "drilldown" : "Roger Bell_West" + }, + { + "name" : "Simon Green", + "y" : 3, + "drilldown" : "Simon Green" + }, + { + "name" : "Steven Wilson", + "y" : 2, + "drilldown" : "Steven Wilson" + }, + { + "name" : "Thomas Kohler", + "y" : 4, + "drilldown" : "Thomas Kohler" + }, + { + "y" : 4, + "name" : "Ulrich Rieke", + "drilldown" : "Ulrich Rieke" + }, + { + "drilldown" : "W. Luis Mochan", + "name" : "W. Luis Mochan", + "y" : 3 + }, + { + "drilldown" : "Wanderdoc", + "name" : "Wanderdoc", + "y" : 2 + } + ], + "name" : "The Weekly Challenge - 307" + } + ], + "chart" : { + "type" : "column" + }, + "legend" : { + "enabled" : 0 + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "drilldown" : { + "series" : [ + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Ali Moradi", + "id" : "Ali Moradi" + }, + { + "name" : "Andreas Mahnke", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Andreas Mahnke" + }, + { + "name" : "Arne Sommer", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Arne Sommer" + }, + { + "id" : "Athanasius", + "name" : "Athanasius", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ] + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "name" : "BarrOff", + "id" : "BarrOff" + }, + { + "name" : "Bob Lied", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Bob Lied" + }, + { + "id" : "Dave Jacoby", + "name" : "Dave Jacoby", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "David Ferrone", + "id" : "David Ferrone" + }, + { + "id" : "E. Choroba", + "name" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "name" : "Jaldhar H. Vyas", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Jaldhar H. Vyas" + }, + { + "name" : "Jan Krnavek", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Jan Krnavek" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Jorg Sommrey", + "id" : "Jorg Sommrey" + }, + { + "name" : "Lubos Kolouch", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Lubos Kolouch" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "name" : "Mark Anderson", + "id" : "Mark Anderson" + }, + { + "id" : "Matthias Muth", + "name" : "Matthias Muth", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "id" : "Peter Campbell Smith", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Peter Campbell Smith" + }, + { + "name" : "Peter Meszaros", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Peter Meszaros" + }, + { + "name" : "Robert Ransbottom", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Robert Ransbottom" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Roger Bell_West", + "id" : "Roger Bell_West" + }, + { + "name" : "Simon Green", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Simon Green" + }, + { + "id" : "Steven Wilson", + "name" : "Steven Wilson", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + |
