aboutsummaryrefslogtreecommitdiff
path: root/challenge-289
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-09-30 22:07:13 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-09-30 22:07:13 +0100
commit3114956979e812e8ca166cb84d8dea346cee22d3 (patch)
treec15e952faad2616c26483396368178c686903f6e /challenge-289
parent8c1a0beec1d0dac09dd376bedb36eb11cc56dfbb (diff)
downloadperlweeklychallenge-club-3114956979e812e8ca166cb84d8dea346cee22d3.tar.gz
perlweeklychallenge-club-3114956979e812e8ca166cb84d8dea346cee22d3.tar.bz2
perlweeklychallenge-club-3114956979e812e8ca166cb84d8dea346cee22d3.zip
- Added solutions by Ulrich Rieke.
- Added solutions by Paulo Custodio. - Added solutions by David Ferrone. - Added solutions by Thomas Kohler. - Added solutions by Torgny Lyon.
Diffstat (limited to 'challenge-289')
-rwxr-xr-xchallenge-289/ulrich-rieke/cpp/ch-1.cpp48
-rwxr-xr-xchallenge-289/ulrich-rieke/cpp/ch-2.cpp64
-rwxr-xr-xchallenge-289/ulrich-rieke/haskell/ch-1.hs20
-rwxr-xr-xchallenge-289/ulrich-rieke/haskell/ch-2.hs37
-rwxr-xr-xchallenge-289/ulrich-rieke/perl/ch-1.pl21
-rwxr-xr-xchallenge-289/ulrich-rieke/perl/ch-2.pl34
-rwxr-xr-xchallenge-289/ulrich-rieke/raku/ch-1.raku19
-rwxr-xr-xchallenge-289/ulrich-rieke/raku/ch-2.raku27
-rwxr-xr-xchallenge-289/ulrich-rieke/rust/ch-1.rs32
-rwxr-xr-xchallenge-289/ulrich-rieke/rust/ch-2.rs57
10 files changed, 359 insertions, 0 deletions
diff --git a/challenge-289/ulrich-rieke/cpp/ch-1.cpp b/challenge-289/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..0bdc6ca2ed
--- /dev/null
+++ b/challenge-289/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,48 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <sstream>
+#include <set>
+#include <algorithm>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::string word ;
+ std::istringstream istr { text } ;
+ while ( std::getline( istr , word , delimiter ))
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+int main( ) {
+ std::cout << "Enter some integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto w : tokens ) {
+ numbers.push_back( std::stoi( w ) ) ;
+ }
+ if ( numbers.size( ) < 3 )
+ std::cout << *std::max_element( numbers.begin( ) , numbers.end( ) ) <<
+ '\n' ;
+ else {
+ std::set<int> uniques { numbers.begin( ) , numbers.end( ) } ;
+ std::vector<int> for_sorting { uniques.begin( ) , uniques.end( ) } ;
+ if ( for_sorting.size( ) < 3 ) {
+ std::cout << *std::max_element( for_sorting.begin( ) , for_sorting.end( ) )
+ << '\n' ;
+ }
+ else {
+ std::sort( for_sorting.begin( ) , for_sorting.end( ) , []( int a , int b) {
+ return a > b ; } ) ;
+ std::cout << *(for_sorting.begin( ) + 2 ) << '\n' ;
+ }
+ }
+ return 0 ;
+}
+
+
+
+
+
diff --git a/challenge-289/ulrich-rieke/cpp/ch-2.cpp b/challenge-289/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..b03c0abd30
--- /dev/null
+++ b/challenge-289/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,64 @@
+#include <vector>
+#include <string>
+#include <random>
+#include <iostream>
+#include <sstream>
+#include <algorithm>
+
+std::vector<std::string> split( 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 ;
+}
+
+std::string do_shuffle( const std::string & word ) {
+ std::random_device rd ;
+ std::mt19937 g ( rd( ) ) ;
+ std::string central { word.substr( 1 , word.length( ) - 2 ) } ;
+ std::string output { word.substr( 0 , 1 ) } ;
+ std::shuffle( central.begin( ) , central.end( ) , g ) ;
+ output = output + central ;
+ output.push_back( word.back( ) ) ;
+ return output ;
+}
+
+int main( ) {
+ std::cout << "Enter some sentences, <return> to end!\n" ;
+ std::vector<std::string> block ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ while ( ! line.empty( ) ) {
+ block.push_back( line ) ;
+ std::getline( std::cin , line ) ;
+ }
+ std::vector<std::string> shuffled ;
+ for ( auto aLine : block ) {
+ std::vector<std::string> shuffled_line ;
+ auto tokens { split( aLine , ' ' ) } ;
+ for ( auto w : tokens ) {
+ if ( w.length( ) <= 3 ) {
+ shuffled_line.push_back( w ) ;
+ }
+ else {
+ shuffled_line.push_back( do_shuffle( w ) ) ;
+ }
+ }
+ std::string changed ;
+ for ( auto w : shuffled_line ) {
+ changed = changed + w + " " ;
+ }
+ changed.pop_back( ) ;
+ shuffled.push_back( changed ) ;
+ }
+ for ( auto w : shuffled ) {
+ std::cout << w << '\n' ;
+ }
+ return 0 ;
+}
+
+
+
diff --git a/challenge-289/ulrich-rieke/haskell/ch-1.hs b/challenge-289/ulrich-rieke/haskell/ch-1.hs
new file mode 100755
index 0000000000..3ab4f252ab
--- /dev/null
+++ b/challenge-289/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,20 @@
+module Challenge289
+ where
+import qualified Data.Set as S
+import Data.List ((!!) , sort )
+
+solution :: [Int] -> Int
+solution list
+ |length list < 3 = maximum list
+ |otherwise = if length uniques < 3 then maximum uniques else sorted !! ( length sorted - 3 )
+ where
+ uniques :: [Int]
+ uniques = S.toList $ S.fromList list
+ sorted :: [Int]
+ sorted = sort uniques
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by blanks!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-289/ulrich-rieke/haskell/ch-2.hs b/challenge-289/ulrich-rieke/haskell/ch-2.hs
new file mode 100755
index 0000000000..282e96d062
--- /dev/null
+++ b/challenge-289/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,37 @@
+module Challenge289_2
+ where
+import System.Random
+import Data.List (init , tail , (!!))
+import Control.Applicative
+
+keepRolling :: Int -> [Int]
+keepRolling n = snd $ until ( null . fst ) transferOne ([0..n - 1] , [] )
+ where
+ transferOne :: ([Int] , [Int] ) -> ([Int] , [Int])
+ transferOne ( fromList , toList ) = (take selected fromList ++ drop ( selected
+ + 1 ) fromList , toList ++ [( fromList !! selected )])
+ where
+ selected = fst $ uniformR (0 , length fromList - 1) (mkStdGen 137 )
+
+shuffle :: String -> String
+shuffle s
+ |length s <= 3 = s
+ |otherwise = [head s] ++ shuffled ++ [last s]
+ where
+ randomIndices :: [Int]
+ randomIndices = keepRolling ( length s - 2 )
+ shuffled :: String
+ shuffled = map (\i -> ( init $ tail s ) !! i ) randomIndices
+
+enterLines :: IO [String]
+enterLines = do
+ line <- getLine
+ if null line then return []
+ else (line : ) <$> enterLines
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some sentences, <return> to end!"
+ sentences <- enterLines
+ let shuffled = map(\s -> unwords $ map shuffle $ words s) sentences
+ mapM_ putStrLn shuffled
diff --git a/challenge-289/ulrich-rieke/perl/ch-1.pl b/challenge-289/ulrich-rieke/perl/ch-1.pl
new file mode 100755
index 0000000000..dfb4bfaa64
--- /dev/null
+++ b/challenge-289/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( max ) ;
+
+say "Enter some integers, separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my %frequencies ;
+map { $frequencies{$_}++ } @numbers ;
+my @uniques = keys %frequencies ;
+my $len = scalar( @uniques ) ;
+if ( $len < 3 ) {
+ say max( @uniques ) ;
+}
+else {
+ my @sorted = sort {$b <=> $a} @uniques ;
+ say $sorted[2] ;
+}
diff --git a/challenge-289/ulrich-rieke/perl/ch-2.pl b/challenge-289/ulrich-rieke/perl/ch-2.pl
new file mode 100755
index 0000000000..28f6f5f3dd
--- /dev/null
+++ b/challenge-289/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( shuffle ) ;
+
+say "Enter some strings , <return> to end!" ;
+my @strings ;
+my $line = <STDIN> ;
+chomp $line ;
+while ( $line ) {
+ push( @strings , $line ) ;
+ $line = <STDIN> ;
+ chomp $line ;
+}
+my @shuffled ;
+for my $inputline( @strings ) {
+ my @changed_words ;
+ my @words = split( /\s/ , $inputline ) ;
+ for my $word( @words ) {
+ if ( length $word <= 3 ) {
+ push( @changed_words , $word ) ;
+ }
+ else {
+ my @letters = split( // , $word ) ;
+ my $changed = $letters[0] ;
+ my @shuffledLetters = shuffle (@letters[1..(length( $word ) - 2)]) ;
+ $changed .= (join( '' , @shuffledLetters) . $letters[$#letters] ) ;
+ push( @changed_words, $changed ) ;
+ }
+ }
+ push( @shuffled, join( ' ' , @changed_words ) ) ;
+}
+map { say } @shuffled ;
diff --git a/challenge-289/ulrich-rieke/raku/ch-1.raku b/challenge-289/ulrich-rieke/raku/ch-1.raku
new file mode 100755
index 0000000000..e6dfb464f5
--- /dev/null
+++ b/challenge-289/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,19 @@
+use v6 ;
+
+say "Enter some integers, separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+if @numbers.elems < 3 {
+ say @numbers.max ;
+}
+else {
+ my $uniques = @numbers.Set ;
+ my @sorted = $uniques.keys.sort ;
+ my $len = @sorted.elems ;
+ if ( $len < 3 ) {
+ say @sorted.max ;
+ }
+ else {
+ say @sorted[$len - 3] ;
+ }
+}
diff --git a/challenge-289/ulrich-rieke/raku/ch-2.raku b/challenge-289/ulrich-rieke/raku/ch-2.raku
new file mode 100755
index 0000000000..b45d3b1ccb
--- /dev/null
+++ b/challenge-289/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,27 @@
+use v6 ;
+
+say "Enter some strings , <return> to end!" ;
+my $line = $*IN.get ;
+my @strings ;
+while ( $line ) {
+ @strings.push( $line ) ;
+ $line = $*IN.get ;
+}
+my @shuffled ;
+for @strings -> $inline {
+ my @shuffled_row ;
+ for ( $inline.split( /\s/ )) -> $word {
+ if ( $word.chars <= 3 ) {
+ @shuffled_row.push( $word ) ;
+ }
+ else {
+ my $shuffled = $word.substr( 0 , 1 ) ;
+ my @in_between = $word.substr( 1 , $word.chars - 2 ).comb ;
+ @in_between = @in_between.pick: * ;
+ $shuffled ~= ( @in_between.join ~ $word.substr( $word.chars - 1 , 1 ) ) ;
+ @shuffled_row.push( $shuffled ) ;
+ }
+ }
+ @shuffled.push( @shuffled_row.join( ' ' ) ) ;
+}
+@shuffled.map( {.say} ) ;
diff --git a/challenge-289/ulrich-rieke/rust/ch-1.rs b/challenge-289/ulrich-rieke/rust/ch-1.rs
new file mode 100755
index 0000000000..e461b90a89
--- /dev/null
+++ b/challenge-289/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,32 @@
+use std::io ;
+use std::collections::HashSet ;
+
+fn main() {
+ println!("Enter some integers, 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 numbers : Vec<i32> = entered_line.split_whitespace( ).map( |s|
+ s.parse::<i32>().unwrap( ) ).collect( ) ;
+ if numbers.len( ) < 3 {
+ println!("{}" , numbers.into_iter( ).max( ).unwrap( ) ) ;
+ }
+ else {
+ let mut uniques : HashSet<i32> = HashSet::new( ) ;
+ for n in numbers {
+ uniques.insert( n ) ;
+ }
+ let mut for_sorting : Vec<i32> = Vec::new( ) ;
+ for n in uniques {
+ for_sorting.push( n ) ;
+ }
+ let len = for_sorting.len( ) ;
+ if len < 3 {
+ println!("{}" , for_sorting.into_iter( ).max( ).unwrap( ) ) ;
+ }
+ else {
+ for_sorting.sort( ) ;
+ println!("{}" , for_sorting[len - 3]) ;
+ }
+ }
+}
diff --git a/challenge-289/ulrich-rieke/rust/ch-2.rs b/challenge-289/ulrich-rieke/rust/ch-2.rs
new file mode 100755
index 0000000000..466ec92ad6
--- /dev/null
+++ b/challenge-289/ulrich-rieke/rust/ch-2.rs
@@ -0,0 +1,57 @@
+use std::io ;
+use std::io::BufRead ;
+use rand::prelude::* ;
+
+fn shuffle_in_between( word : &str ) -> String {
+ let mut shuffled : String = String::new( ) ;
+ shuffled.push( word.chars( ).nth( 0 ).unwrap( ) ) ;
+ let mut rng = rand::thread_rng( ) ;
+ let mut letters : Vec<char> = Vec::new( ) ;
+ for n in 1..word.len( ) - 1 {
+ letters.push( word.chars( ).nth( n ).unwrap( ) ) ;
+ }
+ letters.shuffle( &mut rng ) ;
+ for l in letters {
+ shuffled.push( l ) ;
+ }
+ shuffled.push( word.chars( ).last( ).unwrap( ) ) ;
+ shuffled
+}
+
+fn main() -> io::Result<( )> {
+ println!("Enter some strings !");
+ println!("Enter <return> to end!" ) ;
+ let mut all_input : String = String::new( ) ;
+ let mut lines = io::stdin( ).lock( ).lines( ) ;
+ while let Some( line ) = lines.next( ) {
+ let last_input = line.unwrap( ) ;
+ if last_input.len( ) == 0 {
+ break ;
+ }
+ else {
+ all_input.push_str( &last_input ) ;
+ all_input.push_str( "\n" ) ;
+ }
+ }
+ let all_lines : &str = all_input.as_str( ).trim( ) ;
+ let rows : Vec<&str> = all_lines.split( "\n").collect( ) ;
+ let valid_rows : Vec<&str> = rows.into_iter( ).filter( |&s| s.len( )
+ > 0 ).collect( ) ;
+ let mut shuffled_rows : Vec<Vec<String>> = Vec::new( ) ;
+ for r in &valid_rows {
+ let mut shuffled_words : Vec<String> = Vec::new( ) ;
+ let words : Vec<&str> = r.split_whitespace( ).collect( ) ;
+ for w in &words {
+ if w.len( ) > 3 {
+ let shuffled : String = shuffle_in_between( w ) ;
+ shuffled_words.push( shuffled ) ;
+ }
+ else {
+ shuffled_words.push( w.to_string( ) ) ;
+ }
+ }
+ shuffled_rows.push( shuffled_words ) ;
+ }
+ println!("{:?}" , shuffled_rows ) ;
+ Ok(())
+}