From 77f7bf0e19793b899bcd01caf558c1c89d296e99 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Wed, 17 Jul 2024 23:17:58 +0100 Subject: - Added solutions by Dave Jacoby. - Added solutions by Roger Bell_West. - Added solutions by Lance Wicks. - Added solutions by Ulrich Rieke. - Added solutions by Reinier Maliepaard. --- challenge-278/laurent-rosenfeld/blog1.txt | 1 + challenge-278/laurent-rosenfeld/perl/ch-2.pl | 20 +++++++++++++ challenge-278/laurent-rosenfeld/raku/ch-2.raku | 12 ++++++++ challenge-278/reinier-maliepaard/blog.txt | 1 + challenge-278/reinier-maliepaard/perl/ch-1.pl | 26 ++++++++++++++++ challenge-278/reinier-maliepaard/perl/ch-2.pl | 31 +++++++++++++++++++ challenge-278/ulrich-rieke/cpp/ch-1.cpp | 41 ++++++++++++++++++++++++++ challenge-278/ulrich-rieke/cpp/ch-2.cpp | 26 ++++++++++++++++ challenge-278/ulrich-rieke/haskell/ch-1.hs | 18 +++++++++++ challenge-278/ulrich-rieke/haskell/ch-2.hs | 17 +++++++++++ challenge-278/ulrich-rieke/perl/ch-1.pl | 27 +++++++++++++++++ challenge-278/ulrich-rieke/perl/ch-2.pl | 24 +++++++++++++++ challenge-278/ulrich-rieke/raku/ch-1.raku | 20 +++++++++++++ challenge-278/ulrich-rieke/raku/ch-2.raku | 19 ++++++++++++ challenge-278/ulrich-rieke/rust/ch-1.rs | 19 ++++++++++++ challenge-278/ulrich-rieke/rust/ch-2.rs | 30 +++++++++++++++++++ 16 files changed, 332 insertions(+) create mode 100644 challenge-278/laurent-rosenfeld/blog1.txt create mode 100644 challenge-278/laurent-rosenfeld/perl/ch-2.pl create mode 100644 challenge-278/laurent-rosenfeld/raku/ch-2.raku create mode 100644 challenge-278/reinier-maliepaard/blog.txt create mode 100644 challenge-278/reinier-maliepaard/perl/ch-1.pl create mode 100644 challenge-278/reinier-maliepaard/perl/ch-2.pl create mode 100755 challenge-278/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-278/ulrich-rieke/cpp/ch-2.cpp create mode 100755 challenge-278/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-278/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-278/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-278/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-278/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-278/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-278/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-278/ulrich-rieke/rust/ch-2.rs (limited to 'challenge-278') diff --git a/challenge-278/laurent-rosenfeld/blog1.txt b/challenge-278/laurent-rosenfeld/blog1.txt new file mode 100644 index 0000000000..7ff6d0e286 --- /dev/null +++ b/challenge-278/laurent-rosenfeld/blog1.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/07/perl-weekly-challenge-278-reverse-word.html diff --git a/challenge-278/laurent-rosenfeld/perl/ch-2.pl b/challenge-278/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..70f9d65ca2 --- /dev/null +++ b/challenge-278/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; +use feature 'say'; + +sub shuffle_word { + my ($word, $char) = @_; + my $ind = index $word, $char; + return $word unless $ind; + my @prefix_letters = (split //, $word)[0..$ind]; + my $prefix = join "", sort @prefix_letters; + return $prefix . substr $word, $ind + 1; +} + +my @tests = ( [ qw ], + [ qw ], + [ qw ] ); +for my $test (@tests) { + printf "%-12s %-2s => ", @$test; + say shuffle_word @$test; +} diff --git a/challenge-278/laurent-rosenfeld/raku/ch-2.raku b/challenge-278/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..60300abc16 --- /dev/null +++ b/challenge-278/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,12 @@ +sub shuffle-word ($word is copy, $char) { + my $ind = index $word, $char; + return $word unless $ind; + my $prefix = join "", sort $word.comb[0..$ind]; + return $prefix ~ substr $word, $ind + 1; +} + +my @tests = , , ; +for @tests -> @test { + printf "%-12s %-2s => ", @test; + say shuffle-word @test[0], @test[1]; +} diff --git a/challenge-278/reinier-maliepaard/blog.txt b/challenge-278/reinier-maliepaard/blog.txt new file mode 100644 index 0000000000..481a974d09 --- /dev/null +++ b/challenge-278/reinier-maliepaard/blog.txt @@ -0,0 +1 @@ +https://reiniermaliepaard.nl/perl/pwc/index.php?id=pwc278 diff --git a/challenge-278/reinier-maliepaard/perl/ch-1.pl b/challenge-278/reinier-maliepaard/perl/ch-1.pl new file mode 100644 index 0000000000..3ef473f736 --- /dev/null +++ b/challenge-278/reinier-maliepaard/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub sort_string { + + return (join(" ", map { $_ =~ s/\d+//gr } + sort( { substr($a, -1, 1) <=> substr($b, -1, 1) } + split(/\s+/, $_[0]) ))) ; +}; + +# TESTS + +my $str; + +# Example 1 +$str = "and2 Raku3 cousins5 Perl1 are4"; +print(sort_string($str), "\n"); # Output: "Perl and Raku are cousins" + +# Example 2 +$str = "guest6 Python1 most4 the3 popular5 is2 language7"; +print(sort_string($str), "\n"); # Output: "Python is the most popular guest language" + +# Example 3 +$str = "Challenge3 The1 Weekly2"; +print(sort_string($str), "\n"); # Output: "The Weekly Challenge" diff --git a/challenge-278/reinier-maliepaard/perl/ch-2.pl b/challenge-278/reinier-maliepaard/perl/ch-2.pl new file mode 100644 index 0000000000..61925a948a --- /dev/null +++ b/challenge-278/reinier-maliepaard/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub reverse_word { + + my $index = index($_[0], $_[1]); + + return(join("", sort( split(//, substr($_[0], 0, $index + 1)) ), + substr($_[0], $index + 1), "\n")); +} + +# TESTS + +my $str; +my $char; + +# Example 1 +$str = "challenge"; +$char = "e"; +print(reverse_word($str, $char), "\n"); # Output: "acehllnge" + +# Example 2 +$str = "programming"; +$char = "a"; +print(reverse_word($str, $char), "\n"); # Output: "agoprrmming" + +# Example 3 +$str = "champion"; +$char = "b"; +print(reverse_word($str, $char), "\n"); # Output: "champion" diff --git a/challenge-278/ulrich-rieke/cpp/ch-1.cpp b/challenge-278/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..82f8aa0f97 --- /dev/null +++ b/challenge-278/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include + +std::vector tokenize( const std::string & text , char delimiter ) { + std::istringstream iss { text } ; + std::string token ; + std::vector words ; + while ( std::getline( iss, token , delimiter )) { + words.push_back( token ) ; + } + return words ; +} + +bool mySorter( const std::pair &p1 , const std::pair & p2 ) { + return p1.second < p2.second ; +} + +int main( ) { + std::cout << "Enter some words , ending in numbers!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + auto words { tokenize( line, ' ' ) } ; + std::vector> allPairs ; + for ( auto w : words ) { + auto pos = w.find_first_of("123456789") ; + std::string firstPart { w.substr(0 , pos ) } ; + allPairs.push_back( std::make_pair( firstPart , std::stoi( w.substr(pos ) + ))) ; + } + std::sort( allPairs.begin( ) , allPairs.end( ) , mySorter ) ; + for ( auto p : allPairs ) { + std::cout << p.first << ' ' ; + } + std::cout << '\n' ; + return 0 ; +} diff --git a/challenge-278/ulrich-rieke/cpp/ch-2.cpp b/challenge-278/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..a2c537edaf --- /dev/null +++ b/challenge-278/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +int main( ) { + std::cout << "Enter a word!\n" ; + std::string haystack ; + std::cin >> haystack ; + char needle ; + std::cout << "Enter a character!\n" ; + std::cin >> needle ; + auto pos = haystack.find( needle ) ; + if ( pos != std::string::npos ) { + std::string firstPart { haystack.substr( 0 , pos + 1 ) } ; + std::sort( firstPart.begin( ) , firstPart.end( ) ) ; + if ( pos + 1 < haystack.length( ) ) + std::cout << ( firstPart + haystack.substr( pos + 1 ) ) << '\n' ; + else + std::cout << firstPart << '\n' ; + } + else { + std::cout << haystack << '\n' ; + } + return 0 ; +} + diff --git a/challenge-278/ulrich-rieke/haskell/ch-1.hs b/challenge-278/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..8f5102a07e --- /dev/null +++ b/challenge-278/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,18 @@ +module Challenge278 + where +import Data.Char ( isDigit ) +import Data.List ( sortOn ) + +solution :: String -> String +solution str = + let wordlist = words str + allPairs = map (\w -> span (\c -> not $ isDigit c) w ) wordlist + sorted = sortOn (\p -> (read $ snd p :: Int) ) allPairs + firstWords = map fst sorted + in unwords firstWords + +main :: IO ( ) +main = do + putStrLn "Enter a line with words ending in numbers!" + myLine <- getLine + print $ solution myLine diff --git a/challenge-278/ulrich-rieke/haskell/ch-2.hs b/challenge-278/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..277eec9e4f --- /dev/null +++ b/challenge-278/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,17 @@ +module Challenge278_2 + where +import Data.List ( sort , findIndex ) +import Data.Maybe ( fromJust ) + +solution :: String -> Char -> String +solution haystack needle = if notElem needle haystack then haystack else ( sort + $ take ( pos + 1 ) haystack ) ++ drop ( pos + 1 ) haystack + where + pos = fromJust $ findIndex ( == needle ) haystack + +main :: IO ( ) +main = do + putStrLn "Enter a word and a character, separated by blanks!" + myLine <- getLine + let [haystack , needle] = words myLine + print $ solution haystack ( head needle ) diff --git a/challenge-278/ulrich-rieke/perl/ch-1.pl b/challenge-278/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..250227b4ef --- /dev/null +++ b/challenge-278/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +sub createPair { + my $word = shift ; + my @pair ; + if ( $word =~ /^(\D+)(\d+)$/ ) { + push( @pair , $1 , $2 ) ; + } + return @pair ; +} + +say "Enter some words, ended by numbers!" ; +my $line = ; +chomp $line ; +my @words = split( /\s/ , $line ) ; +my @allPairs ; +for my $w ( @words ) { + my @pair = createPair( $w ) ; + push ( @allPairs , \@pair ) ; +} +my @sorted = sort { $a->[1] <=> $b->[1] } @allPairs ; +my @firsts = map { $_->[0] } @sorted ; +say join( ' ' , @firsts ) ; + diff --git a/challenge-278/ulrich-rieke/perl/ch-2.pl b/challenge-278/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..3444fb3738 --- /dev/null +++ b/challenge-278/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter a word and a character, separated by a blank!" ; +my $line = ; +chomp $line ; +( my $haystack , my $needle ) = split( /\s+/ , $line ) ; +my $pos = index( $haystack , $needle ) ; +if ( $pos != -1 ) { + my $firstPart = substr( $haystack , 0 , $pos + 1 ) ; + my $sorted = join( '' , sort split( // , $firstPart ) ) ; + if ( $pos + 1 < length $haystack ) { + say ( $sorted . substr( $haystack , $pos + 1 ) ) ; + } + else { + say $sorted ; + } +} +else { + say $haystack ; +} + diff --git a/challenge-278/ulrich-rieke/raku/ch-1.raku b/challenge-278/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..969f9b0358 --- /dev/null +++ b/challenge-278/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,20 @@ +use v6 ; + +sub createPair( $word ) { + my @pair; + if ( $word ~~ /^(\D+)(\d+)$/ ) { + @pair = [ ~$/[0] , +$/[1] ] ; + } + return @pair ; +} + +say "Enter a line of words with ending numbers!" ; +my $line = $*IN.get ; +my @allWords = $line.words ; +my @pairs = @allWords.map( {createPair( $_ )} ) ; +my @sorted = @pairs.sort( *.[1] ) ; +my $output ; +for @sorted -> @p { + $output ~= @p[0] ~ " " ; +} +say $output ; diff --git a/challenge-278/ulrich-rieke/raku/ch-2.raku b/challenge-278/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..e22ebf81ec --- /dev/null +++ b/challenge-278/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,19 @@ +use v6 ; + +say "Enter a word and a character , separated by a blank!" ; +my $line = $*IN.get ; +( my $word , my $needle) = $line.words ; +my $pos = $word.index( $needle ) ; +if ( $pos.defined ) { + my $firstPart = $word.substr( 0 , $pos + 1 ) ; + my $sorted = $firstPart.comb.sort.join ; + if ( $pos + 1 < $word.chars ) { + say ( $sorted ~ $word.substr( $pos + 1 ) ) ; + } + else { + say $sorted ; + } +} +else { + say $word ; +} diff --git a/challenge-278/ulrich-rieke/rust/ch-1.rs b/challenge-278/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..13529e0bff --- /dev/null +++ b/challenge-278/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,19 @@ +use std::io ; + +fn main() { + println!("Enter some words with ending numbers , separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = inline.as_str( ).trim( ) ; + let words : Vec<&str> = entered_line.split_whitespace( ).collect( ) ; + let mut all_pairs : Vec<(&str, u16)> = Vec::new( ) ; + words.into_iter( ).map( | w | { + let pos : usize = w.find( | c : char| c.is_ascii_digit( )).unwrap( ) ; + let first_part : &str = &w[0..pos] ; + let second_part : u16 = w[pos..].parse::( ).unwrap( ) ; + let p = (first_part , second_part ) ; + p } ).for_each( |pa| all_pairs.push( pa ) ) ; + all_pairs.sort_by( |a , b| a.1.cmp( &b.1 ) ); + all_pairs.into_iter().for_each( |p| print!("{:?} " , p.0)) ; + println!("") ; +} diff --git a/challenge-278/ulrich-rieke/rust/ch-2.rs b/challenge-278/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..00d30bbf51 --- /dev/null +++ b/challenge-278/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,30 @@ +use std::io ; + +fn main() { + println!("Enter a string and a single character , separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = inline.as_str( ).trim( ) ; + let words : Vec<&str> = entered_line.split_whitespace( ).collect( ) ; + let haystack : &str = words[0] ; + let needle : &str = words[1] ; + if haystack.contains( &needle ) { + let mut result : String = String::new( ) ; + let pos : usize = haystack.find( &needle ).unwrap( ) ; + let mut characters : Vec = Vec::new( ) ; + for c in haystack[0..=pos].chars( ) { + characters.push( c ) ; + } + characters.sort( ) ; + for c in characters { + result.push( c ) ; + } + if ( pos + 1 ) < haystack.len( ) { + result.push_str( &haystack[pos + 1 ..] ) ; + } + println!("{:?}" , result ) ; + } + else { + println!("{:?}" , haystack ) ; + } +} -- cgit