diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-02-03 17:29:40 +0000 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-02-03 17:29:40 +0000 |
| commit | aaab417272f7ae13ade34c68a033d2b1214886d3 (patch) | |
| tree | faa6dbd23c6f83d75adb7d17ae31402afcb1dc8e | |
| parent | 22771160a2fd5e5893342b5f28ee039872842852 (diff) | |
| download | perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.tar.gz perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.tar.bz2 perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.zip | |
- Added solutions by Ulrich Rieke.
33 files changed, 3429 insertions, 3132 deletions
diff --git a/challenge-307/ulrich-rieke/cpp/ch-1.cpp b/challenge-307/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..df6d096722 --- /dev/null +++ b/challenge-307/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,45 @@ +#include <string>
+#include <vector>
+#include <iostream>
+#include <sstream>
+#include <utility>
+#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 ;
+}
+
+int main( ) {
+ std::cout << "Enter some integers separated by whitespace!\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 ) ) ;
+ std::vector<std::pair<int , int>> before_sort , after_sort ;
+ int len = numbers.size( ) ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ before_sort.push_back( std::make_pair( i , numbers[i] ) ) ;
+ }
+ std::sort( numbers.begin( ) , numbers.end( ) ) ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ after_sort.push_back( std::make_pair( i , numbers[i] ) ) ;
+ }
+ std::vector<int> changed_indices ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ if ( (before_sort.begin( ) + i)->second != (after_sort.begin( ) + i)->second )
+ changed_indices.push_back( (before_sort.begin( ) + i)->first ) ;
+ }
+ std::cout << "( " ;
+ for ( int i : changed_indices )
+ std::cout << i << ' ' ;
+ std::cout << ")\n" ;
+ return 0 ;
+}
diff --git a/challenge-307/ulrich-rieke/cpp/ch-2.cpp b/challenge-307/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..8d8a8dac18 --- /dev/null +++ b/challenge-307/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,40 @@ +#include <string>
+#include <iostream>
+#include <vector>
+#include <sstream>
+#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 areAnagrams( std::string firstWord , std::string secondWord ) {
+ std::sort( firstWord.begin( ) , firstWord.end( ) ) ;
+ std::sort( secondWord.begin( ) , secondWord.end( ) ) ;
+ return (firstWord == secondWord ) ;
+}
+
+int count_anagrams( const std::vector<std::string> & words ) {
+ int len = words.size( ) ;
+ int anagramcount = 0 ;
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ if ( areAnagrams( words[i] , words[i + 1] ) )
+ anagramcount++ ;
+ }
+ return anagramcount ;
+}
+
+int main( ) {
+ std::cout << "Enter some words separated by whitespace!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::cout << ( tokens.size( ) - count_anagrams( tokens ) ) << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-307/ulrich-rieke/haskell/ch-1.hs b/challenge-307/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..e3aee66ff9 --- /dev/null +++ b/challenge-307/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,16 @@ +module Challenge307
+ where
+import Data.List ( sort )
+
+solution :: [Int] -> [Int]
+solution list =
+ let sorted = sort list
+ compared = zip list sorted
+ indexed = zip [0, 1 ..] compared
+ in map fst $ filter (\p -> (fst $ snd p) /= (snd $ snd p)) indexed
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by whitespace!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-307/ulrich-rieke/haskell/ch-2.hs b/challenge-307/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..faa82e4637 --- /dev/null +++ b/challenge-307/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,20 @@ +module Challenge307_2
+ where
+import Data.List( sort )
+import Data.List.Split( divvy )
+
+areAnagrams :: String -> String -> Bool
+areAnagrams str1 str2 = sort str1 == sort str2
+
+solution :: [String] -> Int
+solution input =
+ let neighbours = divvy 2 1 input
+ anacount = length $ filter (\subli -> areAnagrams (head subli) (last subli) )
+ neighbours
+ in length input - anacount
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some words separated by whitespace!"
+ input <- getLine
+ print $ solution $ words input
diff --git a/challenge-307/ulrich-rieke/perl/ch-1.pl b/challenge-307/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..f059f8fc9c --- /dev/null +++ b/challenge-307/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( zip ) ;
+
+say "Enter some integers separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my $len = scalar( @numbers ) ;
+my @before_pairs = zip [0..$len - 1] , [@numbers] ;
+my @sorted = sort { $a <=> $b } @numbers ;
+my @after_pairs = zip [0..$len - 1] , [@sorted] ;
+my @changed_indices ;
+for my $pos (0..$len - 1) {
+ if ( $before_pairs[$pos]->[1] != $after_pairs[$pos]->[1] ) {
+ push( @changed_indices , $pos ) ;
+ }
+}
+say '(' . join( ',' , @changed_indices ) . ')' ;
diff --git a/challenge-307/ulrich-rieke/perl/ch-2.pl b/challenge-307/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..f02c3415e7 --- /dev/null +++ b/challenge-307/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub findAnagramNumber {
+ my $words = shift ;
+ my $anagramCount = 0 ;
+ my $len = scalar( @$words ) ;
+ for my $pos( 0..$len - 2 ) {
+ if ( areAnagrams( $words->[$pos] , $words->[$pos + 1] ) ) {
+ $anagramCount++ ;
+ }
+ }
+ return $anagramCount ;
+}
+
+sub areAnagrams {
+ my $first = shift ;
+ my $second = shift ;
+ my $firstOrdered = join( '' , sort ( split (// , $first )) ) ;
+ my $secondOrdered = join( '' , sort ( split (// , $second ))) ;
+ if ( $firstOrdered eq $secondOrdered ) {
+ return 1 ;
+ }
+ else {
+ return 0 ;
+ }
+}
+
+say "Enter some words separated by whitespace!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @words = split( /\s/ , $line ) ;
+my $ananumber = findAnagramNumber( \@words ) ;
+say ( scalar( @words ) - $ananumber ) ;
diff --git a/challenge-307/ulrich-rieke/raku/ch-1.raku b/challenge-307/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..a2bed3ee17 --- /dev/null +++ b/challenge-307/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,16 @@ +use v6 ;
+
+say "Enter some integers separated by whitespace!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my $len = @numbers.elems ;
+my @before_pairs = ((0..$len - 1) Z, @numbers) ;
+my @sorted = @numbers.sort( {$^a <=> $^b} ) ;
+my @after_pairs = ((0..$len - 1) Z, @sorted ) ;
+my @changed_indices ;
+for (0..$len - 1) -> $pos {
+ if ( @before_pairs[$pos][1] != @after_pairs[$pos][1] ) {
+ @changed_indices.push( $pos ) ;
+ }
+}
+say '(' ~ @changed_indices.join( ',' ) ~ ')' ;
diff --git a/challenge-307/ulrich-rieke/raku/ch-2.raku b/challenge-307/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..58a72e7afc --- /dev/null +++ b/challenge-307/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,25 @@ +use v6 ;
+
+sub find_anagrams( @array ) {
+ my $len = @array.elems ;
+ my $anagramcount = 0 ;
+ for (0..$len - 2) -> $pos {
+ if ( areAnagrams( @array[$pos] , @array[$pos + 1] )) {
+ $anagramcount++ ;
+ }
+ }
+ return $anagramcount ;
+}
+
+sub areAnagrams( $word1 , $word2 ) {
+ my @firstLetters = $word1.comb.sort ;
+ my @secondLetters = $word2.comb.sort ;
+ return ( @firstLetters == @secondLetters ) ;
+}
+
+say "Enter some words separated by whitespace!" ;
+my $line = $*IN.get ;
+my @words = $line.words ;
+my $anacount = find_anagrams( @words ) ;
+say ( @words.elems - $anacount ) ;
+
diff --git a/challenge-307/ulrich-rieke/rust/ch-1.rs b/challenge-307/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..832e1f3476 --- /dev/null +++ b/challenge-307/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,19 @@ +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( ) ; + let numbers : Vec<i32> = inline.trim( ).split_whitespace( ).map( |s| + s.parse::<i32>( ).unwrap( ) ).collect( ) ; + let original_positions : Vec<(usize, &i32)> = numbers.iter( ).enumerate( ). + collect( ) ; + let mut sorted : Vec<i32> = numbers.clone( ) ; + sorted.sort( ) ; + let new_positions : Vec<(usize, i32)> = sorted.into_iter( ).enumerate( ). + collect( ) ; + let mut result : Vec<usize> = Vec::new( ) ; + original_positions.into_iter().zip( new_positions.into_iter( )).filter( + |&(a , b)| *a.1 != b.1 ).for_each( |p| result.push( p.0.0 )) ; + println!("{:?}" , result) ; +} diff --git a/challenge-307/ulrich-rieke/rust/ch-2.rs b/challenge-307/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..4cb571dd3e --- /dev/null +++ b/challenge-307/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,40 @@ +use std::io ; + +//find the indices of those elements that can be deleted , if any +fn find_indices( words : &Vec<&str> ) -> Vec<usize> { + let enumerated : Vec<(usize, &&str)> = words.iter( ).enumerate( ). + collect( ) ; + let enu_slice = &enumerated[..] ; + let mut iter = enu_slice.windows( 2 ) ; + let mut positions : Vec<usize> = Vec::new( ) ; + while let Some( wi ) = iter.next( ) { + if are_anagrams( wi[0].1 , wi[1].1 ) { + positions.push( wi[0].0 ) ; + } + } + positions +} + +fn are_anagrams( word1 : &str , word2 : &str ) -> bool { + let mut first_letters : Vec<char> = Vec::new( ) ; + let mut second_letters : Vec<char> = Vec::new( ) ; + for c in word1.chars( ) { + first_letters.push( c ) ; + } + for c in word2.chars( ) { + second_letters.push( c ) ; + } + first_letters.sort( ) ; + second_letters.sort( ) ; + first_letters == second_letters +} + +fn main() { + println!("Enter some words separated by whitespace!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let words : Vec<&str> = inline.trim( ).split_whitespace( ). + collect( ) ; + let positions : Vec<usize> = find_indices( &words ) ; + println!("{}" , words.len( ) - positions.len( )) ; +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index f1585243d7..db33a923cd 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,111 +1,41 @@ { - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "legend" : { - "enabled" : 0 - }, - "chart" : { - "type" : "column" - }, - "plotOptions" : { - "series" : { - "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - }, - "borderWidth" : 0 - } - }, - "tooltip" : { - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", - "followPointer" : 1 - }, - "series" : [ - { - "name" : "The Weekly Challenge - 307", - "colorByPoint" : 1, - "data" : [ - { - "name" : "E. Choroba", - "drilldown" : "E. Choroba", - "y" : 2 - }, - { - "y" : 2, - "drilldown" : "Lubos Kolouch", - "name" : "Lubos Kolouch" - }, - { - "y" : 2, - "drilldown" : "Mark Anderson", - "name" : "Mark Anderson" - }, - { - "drilldown" : "Peter Campbell Smith", - "name" : "Peter Campbell Smith", - "y" : 3 - }, - { - "y" : 2, - "drilldown" : "Peter Meszaros", - "name" : "Peter Meszaros" - }, - { - "y" : 3, - "drilldown" : "W. Luis Mochan", - "name" : "W. Luis Mochan" - } - ] - } - ], - "xAxis" : { - "type" : "category" - }, - "title" : { - "text" : "The Weekly Challenge - 307" - }, "subtitle" : { - "text" : "[Champions: 6] Last updated at 2025-02-03 17:21:29 GMT" + "text" : "[Champions: 7] Last updated at 2025-02-03 17:29:14 GMT" }, "drilldown" : { "series" : [ { - "name" : "E. Choroba", + "id" : "E. Choroba", "data" : [ [ "Perl", 2 ] ], - "id" : "E. Choroba" + "name" : "E. Choroba" }, { - "id" : "Lubos Kolouch", "data" : [ [ "Perl", 2 ] ], - "name" : "Lubos Kolouch" + "name" : "Lubos Kolouch", + "id" : "Lubos Kolouch" }, { - "name" : "Mark Anderson", "data" : [ [ "Raku", 2 ] ], - "id" : "Mark Anderson" + "id" : "Mark Anderson", + "name" : "Mark Anderson" }, { "name" : "Peter Campbell Smith", - "id" : "Peter Campbell Smith", "data" : [ [ "Perl", @@ -115,21 +45,34 @@ "Blog", 1 ] - ] + ], + "id" : "Peter Campbell Smith" }, { + "id" : "Peter Meszaros", "data" : [ [ "Perl", 2 ] ], - "id" : "Peter Meszaros", "name" : "Peter Meszaros" }, { - "name" : "W. Luis Mochan", - "id" : "W. Luis Mochan", + "id" : "Ulrich Rieke", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "name" : "Ulrich Rieke" + }, + { "data" : [ [ "Perl", @@ -139,8 +82,84 @@ "Blog", 1 ] - ] + ], + "name" : "W. Luis Mochan", + "id" : "W. Luis Mochan" } ] + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "plotOptions" : { + "series" : { + "borderWidth" : 0, + "dataLabels" : { + "format" : "{point.y}", + "enabled" : 1 + } + } + }, + "series" : [ + { + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 307", + "data" : [ + { + "y" : 2, + "drilldown" : "E. Choroba", + "name" : "E. Choroba" + }, + { + "name" : "Lubos Kolouch", + "y" : 2, + "drilldown" : "Lubos Kolouch" + }, + { + "drilldown" : "Mark Anderson", + "y" : 2, + "name" : "Mark Anderson" + }, + { + "drilldown" : "Peter Campbell Smith", + "y" : 3, + "name" : "Peter Campbell Smith" + }, + { + "name" : "Peter Meszaros", + "y" : 2, + "drilldown" : "Peter Meszaros" + }, + { + "y" : 4, + "drilldown" : "Ulrich Rieke", + "name" : "Ulrich Rieke" + }, + { + "y" : 3, + "drilldown" : "W. Luis Mochan", + "name" : "W. Luis Mochan" + } + ] + } + ], + "xAxis" : { + "type" : "category" + }, + "title" : { + "text" : "The Weekly Challenge - 307" + }, + "tooltip" : { + "followPointer" : 1, + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" + }, + "legend" : { + "enabled" : 0 + }, + "chart" : { + "type" : "column" } } diff --git a/stats/pwc-language-breakdown-2019.json b/stats/pwc-language-breakdown-2019.json index ede989d2f8..dbfab7effe 100644 --- a/stats/pwc-language-breakdown-2019.json +++ b/stats/pwc-language-breakdown-2019.json @@ -1,4 +1,7 @@ { + "subtitle" : { + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-02-03 17:29:14 GMT" + }, "drilldown" : { "series" : [ { @@ -16,10 +19,11 @@ 9 ] ], - "id" : "041", - "name" : "041" + "name" : "041", + "id" : "041" }, { + "id" : "040", "data" : [ [ "Perl", @@ -34,11 +38,9 @@ 10 ] ], - "id" : "040", "name" : "040" }, { - "name" : "039", "data" : [ [ "Perl", @@ -53,10 +55,10 @@ 12 ] ], + "name" : "039", "id" : "039" }, { - "name" : "038", "data" : [ [ "Perl", @@ -71,10 +73,11 @@ 12 ] ], + "name" : "038", "id" : "038" }, { - "name" : "037", + "id" : "037", "data" : [ [ "Perl", @@ -89,11 +92,10 @@ 9 ] ], - "id" : "037" + "name" : "037" }, { "name" : "036", - "id" : "036", "data" : [ [ "Perl", @@ -107,7 +109,8 @@ "Blog", 11 ] - ] + ], + "id" : "036" }, { "data" : [ @@ -128,7 +131,7 @@ "name" : "035" }, { - "id" : "034", + "name" : "034", "data" : [ [ "Perl", @@ -143,10 +146,9 @@ 11 ] ], - "name" : "034" + "id" : "034" }, { - "name" : "033", "id" : "033", "data" : [ [ @@ -161,9 +163,11 @@ "Blog", 10 ] - ] + ], + "name" : "033" }, { + "name" : "032", "data" : [ [ "Perl", @@ -178,8 +182,7 @@ 10 ] ], - "id" : "032", - "name" : "032" + "id" : "032" }, { "id" : "031", @@ -200,7 +203,6 @@ "name" : "031" }, { - "id" : "030", "data" : [ [ "Perl", @@ -215,11 +217,11 @@ 10 ] ], + "id" : "030", "name" : "030" }, { "name" : "029", - "id" : "029", "data" : [ [ "Perl", @@ -233,11 +235,10 @@ "Blog", 12 ] - ] + ], + "id" : "029" }, { - "name" : "028", - "id" : "028", "data" : [ [ "Perl", @@ -251,7 +252,9 @@ "Blog", 9 ] - ] + ], + "name" : "028", + "id" : "028" }, { "data" : [ @@ -272,8 +275,6 @@ "name" : "027" }, { - "name" : "026", - "id" : "026", "data" : [ [ "Perl", @@ -287,10 +288,11 @@ "Blog", 10 ] - ] + ], + "name" : "026", + "id" : "026" }, { - "name" : "025", "id" : "025", "data" : [ [ @@ -305,11 +307,10 @@ "Blog", 12 ] - ] + ], + "name" : "025" }, { - "name" : "024", - "id" : "024", "data" : [ [ "Perl", @@ -323,11 +324,11 @@ "Blog", 11 ] - ] + ], + "name" : "024", + "id" : "024" }, { - "name" : "023", - "id" : "023", "data" : [ [ "Perl", @@ -341,10 +342,11 @@ "Blog", 12 ] - ] + ], + "name" : "023", + "id" : "023" }, { - "name" : "022", "id" : "022", "data" : [ [ @@ -359,10 +361,11 @@ "Blog", 10 ] - ] + ], + "name" : "022" }, { - "name" : "021", + "id" : "021", "data" : [ [ "Perl", @@ -377,10 +380,9 @@ 10 ] ], - "id" : "021" + "name" : "021" }, { - "id" : "020", "data" : [ [ "Perl", @@ -395,11 +397,10 @@ 13 ] ], - "name" : "020" + "name" : "020", + "id" : "020" }, { - "name" : "019", - "id" : "019", "data" : [ [ "Perl", @@ -413,10 +414,11 @@ < |
