From e9d030e5fd8fa107797fac1489bca6ce2724d212 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Mon, 10 Feb 2025 20:39:47 +0000 Subject: - 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. --- challenge-308/eric-cheung/python/ch-1.py | 16 +++++++++++ challenge-308/eric-cheung/python/ch-2.py | 19 ++++++++++++ challenge-308/perlboy1967/perl/ch-1.pl | 46 ++++++++++++++++++++++++++++++ challenge-308/perlboy1967/perl/ch-2.pl | 37 ++++++++++++++++++++++++ challenge-308/perlboy1967/perl/ch1.pl | 46 ------------------------------ challenge-308/perlboy1967/perl/ch2.pl | 37 ------------------------ challenge-308/ulrich-rieke/cpp/ch-1.cpp | 39 +++++++++++++++++++++++++ challenge-308/ulrich-rieke/cpp/ch-2.cpp | 42 +++++++++++++++++++++++++++ challenge-308/ulrich-rieke/haskell/ch-1.hs | 16 +++++++++++ challenge-308/ulrich-rieke/haskell/ch-2.hs | 18 ++++++++++++ challenge-308/ulrich-rieke/perl/ch-1.pl | 24 ++++++++++++++++ challenge-308/ulrich-rieke/perl/ch-2.pl | 22 ++++++++++++++ challenge-308/ulrich-rieke/raku/ch-1.raku | 19 ++++++++++++ challenge-308/ulrich-rieke/raku/ch-2.raku | 17 +++++++++++ challenge-308/ulrich-rieke/rust/ch-1.rs | 21 ++++++++++++++ challenge-308/ulrich-rieke/rust/ch-2.rs | 24 ++++++++++++++++ 16 files changed, 360 insertions(+), 83 deletions(-) create mode 100755 challenge-308/eric-cheung/python/ch-1.py create mode 100755 challenge-308/eric-cheung/python/ch-2.py create mode 100755 challenge-308/perlboy1967/perl/ch-1.pl create mode 100755 challenge-308/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-308/perlboy1967/perl/ch1.pl delete mode 100755 challenge-308/perlboy1967/perl/ch2.pl create mode 100755 challenge-308/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-308/ulrich-rieke/cpp/ch-2.cpp create mode 100755 challenge-308/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-308/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-308/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-308/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-308/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-308/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-308/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-308/ulrich-rieke/rust/ch-2.rs (limited to 'challenge-308') 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/ch-1.pl b/challenge-308/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..c9009fa9f2 --- /dev/null +++ b/challenge-308/perlboy1967/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 303 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Count Common +Submitted by: Mohammad Sajid Anwar + +You are given two array of strings, @str1 and @str2. + +Write a script to return the count of common strings in both arrays. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0 qw(-no_srand); + +no warnings qw(experimental::signatures); + +sub countCommon :prototype(\@\@) ($ar1,$ar2) { + my (%f1,%f2,%f3); + map { $f1{$_} = 1 } @$ar1; + map { $f2{$_} = 1 } @$ar2; + map { $f3{$_}++ } keys %f1, keys %f2; + scalar grep { $f3{$_} == 2 } keys %f3; +} + +is(countCommon(@{[qw(perl weekly challenge)]}, + @{[qw(raku weekly challenge)]}), + 2,'Example 1'); +is(countCommon(@{[qw(perl raku python)]}, + @{[qw(python java)]}), + 1,'Example 2'); +is(countCommon(@{[qw(guest contribution)]}, + @{[qw(fun weekly challenge)]}), + 0,'Example 3'); +is(countCommon(@{[1,2,2,3]},@{[2,3,3,4]}),2,'Own test'); + +done_testing; diff --git a/challenge-308/perlboy1967/perl/ch-2.pl b/challenge-308/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..11b87c4cc7 --- /dev/null +++ b/challenge-308/perlboy1967/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 303 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Decode XOR +Submitted by: Mohammad Sajid Anwar + +You are given an encoded array and an initial integer. + +Write a script to find the original array that produced the given encoded +array. It was encoded such that encoded[i] = orig[i] XOR orig[i + 1]. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0 qw(-no_srand); + +no warnings qw(experimental::signatures); + +sub decodeXOR ($initial,@encoded) { + my @l = ($initial); + push(@l,$l[-1] ^ $_) for (@encoded); + return @l; +} + +is([decodeXOR(1,1,2,3)],[1,0,2,1],'Example 1'); +is([decodeXOR(4,6,2,7,3)],[4,2,0,7,4],'Example 2'); + +done_testing; diff --git a/challenge-308/perlboy1967/perl/ch1.pl b/challenge-308/perlboy1967/perl/ch1.pl deleted file mode 100755 index c9009fa9f2..0000000000 --- a/challenge-308/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 303 -L - -Author: Niels 'PerlBoy' van Dijke - -Task 1: Count Common -Submitted by: Mohammad Sajid Anwar - -You are given two array of strings, @str1 and @str2. - -Write a script to return the count of common strings in both arrays. - -=cut - -use v5.32; -use common::sense; -use feature qw(signatures); - -use Test2::V0 qw(-no_srand); - -no warnings qw(experimental::signatures); - -sub countCommon :prototype(\@\@) ($ar1,$ar2) { - my (%f1,%f2,%f3); - map { $f1{$_} = 1 } @$ar1; - map { $f2{$_} = 1 } @$ar2; - map { $f3{$_}++ } keys %f1, keys %f2; - scalar grep { $f3{$_} == 2 } keys %f3; -} - -is(countCommon(@{[qw(perl weekly challenge)]}, - @{[qw(raku weekly challenge)]}), - 2,'Example 1'); -is(countCommon(@{[qw(perl raku python)]}, - @{[qw(python java)]}), - 1,'Example 2'); -is(countCommon(@{[qw(guest contribution)]}, - @{[qw(fun weekly challenge)]}), - 0,'Example 3'); -is(countCommon(@{[1,2,2,3]},@{[2,3,3,4]}),2,'Own test'); - -done_testing; diff --git a/challenge-308/perlboy1967/perl/ch2.pl b/challenge-308/perlboy1967/perl/ch2.pl deleted file mode 100755 index 11b87c4cc7..0000000000 --- a/challenge-308/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 303 -L - -Author: Niels 'PerlBoy' van Dijke - -Task 2: Decode XOR -Submitted by: Mohammad Sajid Anwar - -You are given an encoded array and an initial integer. - -Write a script to find the original array that produced the given encoded -array. It was encoded such that encoded[i] = orig[i] XOR orig[i + 1]. - -=cut - -use v5.32; -use common::sense; -use feature qw(signatures); - -use Test2::V0 qw(-no_srand); - -no warnings qw(experimental::signatures); - -sub decodeXOR ($initial,@encoded) { - my @l = ($initial); - push(@l,$l[-1] ^ $_) for (@encoded); - return @l; -} - -is([decodeXOR(1,1,2,3)],[1,0,2,1],'Example 1'); -is([decodeXOR(4,6,2,7,3)],[4,2,0,7,4],'Example 2'); - -done_testing; 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 +#include +#include +#include +#include + +std::vector split( const std::string & text , char delimiter ) { + std::vector 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 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 +#include +#include +#include + +std::vector split( const std::string & text , char delimiter ) { + std::vector 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 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 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 = ; +chomp $line ; +my @firstWords = split( /\s/ , $line ) ; +say "Enter some more words separated by whitespace!" ; +$line = ; +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 = ; +chomp $line ; +my @encoded = split( /\s/ , $line ) ; +say "Enter an integer!" ; +my $num = ; +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 = inline.trim( ).split_whitespace( ).map( |s| + s.parse::( ).unwrap( ) ).collect( ) ; + let ini_value : u16 = numline.trim( ).parse::( ).unwrap( ) ; + let mut decoded : Vec = 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 ) ; +} -- cgit