aboutsummaryrefslogtreecommitdiff
path: root/challenge-308
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-02-10 20:39:47 +0000
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-02-10 20:39:47 +0000
commite9d030e5fd8fa107797fac1489bca6ce2724d212 (patch)
tree67b923088c999951deb2282143353932210d2cdf /challenge-308
parenta4ee687a84267a19ebcb6d9228ffe4f73927396b (diff)
downloadperlweeklychallenge-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.
Diffstat (limited to 'challenge-308')
-rwxr-xr-xchallenge-308/eric-cheung/python/ch-1.py16
-rwxr-xr-xchallenge-308/eric-cheung/python/ch-2.py19
-rwxr-xr-xchallenge-308/perlboy1967/perl/ch-1.pl (renamed from challenge-308/perlboy1967/perl/ch1.pl)0
-rwxr-xr-xchallenge-308/perlboy1967/perl/ch-2.pl (renamed from challenge-308/perlboy1967/perl/ch2.pl)0
-rwxr-xr-xchallenge-308/ulrich-rieke/cpp/ch-1.cpp39
-rwxr-xr-xchallenge-308/ulrich-rieke/cpp/ch-2.cpp42
-rwxr-xr-xchallenge-308/ulrich-rieke/haskell/ch-1.hs16
-rwxr-xr-xchallenge-308/ulrich-rieke/haskell/ch-2.hs18
-rwxr-xr-xchallenge-308/ulrich-rieke/perl/ch-1.pl24
-rwxr-xr-xchallenge-308/ulrich-rieke/perl/ch-2.pl22
-rwxr-xr-xchallenge-308/ulrich-rieke/raku/ch-1.raku19
-rwxr-xr-xchallenge-308/ulrich-rieke/raku/ch-2.raku17
-rwxr-xr-xchallenge-308/ulrich-rieke/rust/ch-1.rs21
-rwxr-xr-xchallenge-308/ulrich-rieke/rust/ch-2.rs24
14 files changed, 277 insertions, 0 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 ) ;
+}