diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-07-22 00:44:04 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-07-22 00:44:04 +0100 |
| commit | c5ca130343e62834e6079e43b5cb9588ce0243b1 (patch) | |
| tree | 181a67d80a7c27d84101f21a9fe2f58435475344 | |
| parent | 1131559ab4a8d9a87b099304a8629deea0b4b0a1 (diff) | |
| download | perlweeklychallenge-club-c5ca130343e62834e6079e43b5cb9588ce0243b1.tar.gz perlweeklychallenge-club-c5ca130343e62834e6079e43b5cb9588ce0243b1.tar.bz2 perlweeklychallenge-club-c5ca130343e62834e6079e43b5cb9588ce0243b1.zip | |
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke.
- Added solutions by Lukas Mai.
- Added solutions by Andrew Shitov.
- Added solutions by Feng Chang.
- Added solutions by Alexander Karelas.
- Added solutions by Simon Proctor.
- Added solutions by E. Choroba.
- Added solutions by Andreas Mahnke.
- Added solutions by Kjetil Skotheim.
- Added solutions by PokGoPun.
- Added solutions by W. Luis Mochan.
- Added solutions by Mark Anderson.
- Added solutions by David Ferrone.
- Added solutions by Peter Meszaros.
- Added solutions by Thomas Kohler.
- Added solutions by Peter Campbell Smith.
40 files changed, 1145 insertions, 460 deletions
diff --git a/challenge-330/perlboy1967/perl/ch1.pl b/challenge-330/perlboy1967/perl/ch-1.pl index 6c36119f96..6c36119f96 100755 --- a/challenge-330/perlboy1967/perl/ch1.pl +++ b/challenge-330/perlboy1967/perl/ch-1.pl diff --git a/challenge-330/perlboy1967/perl/ch2.pl b/challenge-330/perlboy1967/perl/ch-2.pl index f5393b611c..f5393b611c 100755 --- a/challenge-330/perlboy1967/perl/ch2.pl +++ b/challenge-330/perlboy1967/perl/ch-2.pl diff --git a/challenge-331/eric-cheung/python/ch-1.py b/challenge-331/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..a7d0dcd351 --- /dev/null +++ b/challenge-331/eric-cheung/python/ch-1.py @@ -0,0 +1,12 @@ +
+import re
+
+## strInput = "The Weekly Challenge" ## Example 1
+## strInput = " Hello World " ## Example 2
+strInput = "Let's begin the fun" ## Example 3
+
+strOutput = re.sub(r"\s+", " ", strInput).strip()
+
+arrOutput = [strLoop for strLoop in strOutput.split(" ") if strLoop]
+
+print (len(arrOutput[-1]))
diff --git a/challenge-331/eric-cheung/python/ch-2.py b/challenge-331/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..3b21723f44 --- /dev/null +++ b/challenge-331/eric-cheung/python/ch-2.py @@ -0,0 +1,32 @@ +
+def IsBuddyStr (strSource, strTarget):
+ arrSource = list(strSource)
+
+ for nRow in range(0, len(arrSource) - 1):
+ for nCol in range(nRow + 1, len(arrSource)):
+ arrTemp = arrSource[:]
+ arrTemp[nRow] = arrSource[nCol]
+ arrTemp[nCol] = arrSource[nRow]
+ strSwap = "".join(arrTemp)
+ if strSwap == strTarget:
+ return True
+
+ return False
+
+## Example 1
+## strInputSource = "fuck"
+## strInputTarget = "fcuk"
+
+## Example 2
+## strInputSource = "love"
+## strInputTarget = "love"
+
+## Example 3
+## strInputSource = "fodo"
+## strInputTarget = "food"
+
+## Example 4
+strInputSource = "feed"
+strInputTarget = "feed"
+
+print (IsBuddyStr(strInputSource, strInputTarget))
diff --git a/challenge-331/perlboy1967/perl/ch1.pl b/challenge-331/perlboy1967/perl/ch-1.pl index 7d4786e960..7d4786e960 100755 --- a/challenge-331/perlboy1967/perl/ch1.pl +++ b/challenge-331/perlboy1967/perl/ch-1.pl diff --git a/challenge-331/perlboy1967/perl/ch2.pl b/challenge-331/perlboy1967/perl/ch-2.pl index 2f2a6c578f..2f2a6c578f 100755 --- a/challenge-331/perlboy1967/perl/ch2.pl +++ b/challenge-331/perlboy1967/perl/ch-2.pl diff --git a/challenge-331/ulrich-rieke/cpp/ch-1.cpp b/challenge-331/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..e1028f38d8 --- /dev/null +++ b/challenge-331/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,28 @@ +#include <string>
+#include <iostream>
+#include <vector>
+#include <regex>
+#include <cctype>
+#include <algorithm>
+
+int main( ) {
+ std::cout << "Enter a string!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::regex rx ( "\\S+" ) ;
+ std::vector<std::string> words ;
+ auto end = std::sregex_token_iterator {} ;
+ for ( auto it = std::sregex_token_iterator( std::begin( line ) ,
+ std::end( line ) , rx ) ; it != end ; it++ ) {
+ words.push_back( *it ) ;
+ }
+ std::vector<std::string> selected ;
+ for ( auto s : words ) {
+ if ( std::all_of( s.begin( ) , s.end( ) , []( auto c ){ return
+ std::isalnum( c ) ; } ) )
+ selected.push_back( s ) ;
+ }
+ int len = static_cast<int>( selected.size( ) ) ;
+ std::cout << selected[len - 1].size( ) << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-331/ulrich-rieke/cpp/ch-2.cpp b/challenge-331/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..c0011269b6 --- /dev/null +++ b/challenge-331/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,60 @@ +#include <iostream>
+#include <string>
+#include <vector>
+#include <sstream>
+#include <utility>
+#include <set>
+#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 ;
+}
+
+bool isSwapped( const std::pair<std::pair<char , char> , std::pair<char , char>>
+ & neighbours ) {
+ auto firstPair = neighbours.first ;
+ auto secondPair = neighbours.second ;
+ return ((firstPair.first == secondPair.second) && firstPair.second ==
+ secondPair.first) ;
+}
+
+int main( ) {
+ std::cout << "Enter two strings separated by space!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ auto firstWord { tokens[0] } ;
+ auto secondWord { tokens[1] } ;
+ std::set<char> firstLetters( firstWord.begin( ) , firstWord.end( ) ) ;
+ std::set<char> secondLetters( secondWord.begin( ) , secondWord.end( ) ) ;
+ if ( firstLetters == secondLetters && firstWord.length( ) ==
+ secondWord.length( ) ) {
+ std::vector<std::pair<char , char>> zipped ;
+ int len = static_cast<int>( firstWord.length( ) ) ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ zipped.push_back( std::make_pair( firstWord[i] , secondWord[i] ) ) ;
+ }
+ std::vector<std::pair<std::pair<char, char>, std::pair<char, char>>>
+ allNeighbours ;
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ allNeighbours.push_back(std::make_pair( zipped[i] , zipped[i + 1] )) ;
+ }
+ if ( std::count_if( allNeighbours.begin( ) , allNeighbours.end( ) , [](
+ const auto & p1 ) { return isSwapped( p1 ) ; } ) == 1 ) {
+ std::cout << "true" ;
+ }
+ else {
+ std::cout << "false" ;
+ }
+ }
+ else {
+ std::cout << "false" ;
+ }
+ std::cout << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-331/ulrich-rieke/haskell/ch-1.hs b/challenge-331/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..e61a8bff7d --- /dev/null +++ b/challenge-331/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,15 @@ +module Challenge331
+ where
+import Data.List.Split ( splitWhen )
+import Data.Char ( isSpace , isAlphaNum)
+
+solution :: String -> Int
+solution = length . last . filter (\s -> all isAlphaNum s ) .
+ filter ( not . null ) . splitWhen isSpace
+
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a string!"
+ line <- getLine
+ print $ solution line
diff --git a/challenge-331/ulrich-rieke/haskell/ch-2.hs b/challenge-331/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..6774402063 --- /dev/null +++ b/challenge-331/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,27 @@ +module Challenge331_2
+ where
+import Data.List.Split ( divvy )
+import qualified Data.Set as S
+
+isSwapped :: [(Char , Char)] -> Bool
+isSwapped neighbours = and [fst f == snd s , snd f == fst s]
+ where
+ f :: (Char , Char)
+ f = head neighbours
+ s :: (Char , Char)
+ s = last neighbours
+
+solution :: [String] -> Bool
+solution theWords = and [ S.fromList first == S.fromList second , length first ==
+ length second , (length $ filter isSwapped $ divvy 2 1 $ zip first second)
+ == 1 ]
+ where
+ first = head theWords
+ second = last theWords
+
+main :: IO ( )
+main = do
+ putStrLn "Enter two words separated by space!"
+ line <- getLine
+ print $ solution $ words line
+
diff --git a/challenge-331/ulrich-rieke/perl/ch-1.pl b/challenge-331/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..5ccdf2d58d --- /dev/null +++ b/challenge-331/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,12 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter a string!" ;
+my $word = <STDIN> ;
+chomp $word ;
+$word =~ s/^\s+// ;
+$word =~ s/\s+$// ;
+my @words = split( /\s+/ , $word ) ;
+say length( $words[-1] ) ;
diff --git a/challenge-331/ulrich-rieke/perl/ch-2.pl b/challenge-331/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..fabf2a342f --- /dev/null +++ b/challenge-331/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( zip ) ;
+
+sub isSwapped {
+ my $neighbours = shift ;
+ if ( $neighbours->[0][0] eq $neighbours->[1][1] && ( $neighbours->[0][1] eq
+ $neighbours->[1][0] )) {
+ return 1 ;
+ }
+ else {
+ return 0 ;
+ }
+}
+
+say "Enter two words separated by space!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @words = split( /\s/ , $line ) ;
+#the condition can't be true if the words do not contain the same letters
+my %firstHash ;
+my %secondHash ;
+map { $firstHash{$_}++ } split( // , $words[0] ) ;
+map { $secondHash{$_}++ } split( // , $words[1] ) ;
+#the condition can't be true if the length of the words is not equal
+if ( length( $words[0] ) == length( $words[1] ) && keys( %firstHash ) ==
+ keys( %secondHash ) ) {
+#the idea is: if the words have the same letters and are equally long, then
+#zip their letters and form pairs of neighbouring zip pairs. Then the above
+#condition of being swapped must be fulfilled in exactly one
+ my @firstLetters = split( // , $words[0] ) ;
+ my @secondLetters = split( // , $words[1] ) ;
+ my @zipped = zip( \@firstLetters , \@secondLetters ) ;
+ my @neighbours ;
+ for my $pos( 0..length( $words[0] ) - 2 ) {
+ push( @neighbours , [$zipped[$pos] , $zipped[$pos + 1]] ) ;
+ }
+ if ( scalar( grep { isSwapped( $_ ) } @neighbours ) == 1 ) {
+ say "true" ;
+ }
+ else {
+ say "false" ;
+ }
+}
+else {
+ say "false" ;
+}
diff --git a/challenge-331/ulrich-rieke/raku/ch-1.raku b/challenge-331/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..bd36631738 --- /dev/null +++ b/challenge-331/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,5 @@ +say "Enter a string!" ;
+my $word = $*IN.get ;
+$word .= trim ;
+my @words = $word.split( /\s+/ ) ;
+say @words[*-1].chars ;
diff --git a/challenge-331/ulrich-rieke/raku/ch-2.raku b/challenge-331/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..986a09b85a --- /dev/null +++ b/challenge-331/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,34 @@ +use v6 ;
+
+sub condition( $firstWord , $secondWord ) {
+ my %firstHash ;
+ my %secondHash ;
+ $firstWord.comb.map( {%firstHash{$_}++} ) ;
+ $secondWord.comb.map( {%secondHash{$_}++} ) ;
+ return %firstHash.keys == %secondHash.keys ;
+}
+
+sub isSwapped( $aPair ) {
+ my $firstPair = $aPair[0] ;
+ my $secondPair = $aPair[1] ;
+ return $firstPair[ 0 ] eq $secondPair[1] && $firstPair[1] eq $secondPair[0] ;
+}
+
+say "Enter two words separated by a space!" ;
+my $line = $*IN.get ;
+my @parts = $line.words ;
+if ( @parts[0].chars == @parts[1].chars && condition( @parts[0] , @parts[1] ) ) {
+ my $first = @parts[0] ;
+ my $second = @parts[1] ;
+ my @pairs = $first.comb Z, $second.comb ;
+ my @neighbours = @pairs.rotor( 2 => -1 ) ;
+ if ( @neighbours.grep( {isSwapped( $_ )} ).elems == 1 ) {
+ say "true" ;
+ }
+ else {
+ say "false" ;
+ }
+}
+else {
+ say "false" ;
+}
diff --git a/challenge-331/ulrich-rieke/rust/ch-1.rs b/challenge-331/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..0b430577d0 --- /dev/null +++ b/challenge-331/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,15 @@ +use std::io ; + +fn main() { + println!("Enter a string!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let input : &str = inline.trim( ) ; + let words : Vec<&str> = input.split(char::is_whitespace).collect( ) ; + let non_empty : Vec<&str> = words.into_iter( ).filter( |&w| + w.chars( ).count( ) > 0 && w.chars( ).all( |c| c.is_alphanumeric( ))) + .collect( ) ; + println!("{:?}" , non_empty ) ; + let len : usize = non_empty.len( ) ; + println!("{}" , non_empty[len - 1].chars( ).count( ) ) ; +} diff --git a/challenge-331/ulrich-rieke/rust/ch-2.rs b/challenge-331/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..d11d6f3b42 --- /dev/null +++ b/challenge-331/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,54 @@ +use std::io ; +use std::collections::HashSet ; + +fn is_swapped( pair : &[(char , char)]) -> bool { + let first = pair[0] ; + let second = pair[1] ; + first.0 == second.1 && first.1 == second.0 +} + +//same number of characters +fn condition_1( words : &Vec<&str> ) -> bool { + words[0].chars( ).count( ) == words[1].chars( ).count( ) +} + +//same characters +fn condition_2( words : &Vec<&str> ) -> bool { + let mut first_letters = HashSet::new( ) ; + let mut second_letters = HashSet::new( ) ; + let first_word = words[0] ; + let second_word = words[1] ; + for c in first_word.chars( ) { + first_letters.insert( c ) ; + } + for c in second_word.chars( ) { + second_letters.insert( c ) ; + } + first_letters == second_letters +} + +fn main() { + println!("Enter two strings separated by a space!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let words : Vec<&str> = inline.trim( ).split_whitespace( ).collect( ) ; + if condition_1( &words ) && condition_2( &words ) { + let first_word = words[0] ; + let second_word = words[1] ; + let pairs : Vec<(char , char)> = first_word.chars( ) + .zip( second_word.chars( ) ).collect( ) ; + let pair_slice = pairs.as_slice( ) ; + let neighbour_pairs : Vec<&[(char,char)]> = pair_slice.windows( 2 ) + .collect( ) ; + if neighbour_pairs.into_iter( ).filter( |&v| is_swapped( v )).count( ) + == 1 { + println!("true") ; + } + else { + println!("false") ; + } + } + else { + println!("false") ; + } +} diff --git a/stats/pwc-challenge-330.json b/stats/pwc-challenge-330.json new file mode 100644 index 0000000000..ca3e7375a3 --- /dev/null +++ b/stats/pwc-challenge-330.json @@ -0,0 +1,668 @@ +{ + "chart" : { + "type" : "column" + }, + "drilldown" : { + "series" : [ + { + "data" : [ + [ + "Perl", + 1 + ] + ], + "id" : "Alexander Karelas", + "name" : "Alexander Karelas" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Ali Moradi", + "name" : "Ali Moradi" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Andreas Mahnke", + "name" : "Andreas Mahnke" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Andrew Shitov", + "name" : "Andrew Shitov" + }, + { + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Arne Sommer", + "name" : "Arne Sommer" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "id" : "Athanasius", + "name" : "Athanasius" + }, + { + "data" : [ + [ + "Raku", + 1 + ] + ], + "id" : "BarrOff", + "name" : "BarrOff" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Bob Lied", + "name" : "Bob Lied" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "David Ferrone", + "name" : "David Ferrone" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "E. Choroba", + "name" : "E. Choroba" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Fabio Valeri", + "name" : "Fabio Valeri" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Feng Chang", + "name" : "Feng Chang" + }, + { + "data" : [ + [ + "Perl", + 1 + ] + ], + "id" : "Harry Wozniak", + "name" : "Harry Wozniak" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Humberto Massa", + "name" : "Humberto Massa" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Jan Krnavek", + "name" : "Jan Krnavek" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Kjetil Skotheim", + "name" : "Kjetil Skotheim" + }, + { + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 10 + ] + ], + "id" : "Luca Ferrari", + "name" : "Luca Ferrari" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Mark Anderson", + "name" : "Mark Anderson" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Matthias Muth", + "name" : "Matthias Muth" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "mauke", + "name" : "mauke" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Niels van Dijke", + "name" : "Niels van Dijke" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Packy Anderson", + "name" : "Packy Anderson" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Peter Meszaros", + "name" : "Peter Meszaros" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Robbie |
