diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-11-14 20:13:23 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-11-14 20:13:23 +0000 |
| commit | 53324ed8af4d2e2211d4ce6cc51d1c0471d30c3a (patch) | |
| tree | 211b76647896984d2aaa749e6dff28ac9d393de5 /challenge-191 | |
| parent | bd2f6928838230547dc8de83d54a6a708f86461a (diff) | |
| download | perlweeklychallenge-club-53324ed8af4d2e2211d4ce6cc51d1c0471d30c3a.tar.gz perlweeklychallenge-club-53324ed8af4d2e2211d4ce6cc51d1c0471d30c3a.tar.bz2 perlweeklychallenge-club-53324ed8af4d2e2211d4ce6cc51d1c0471d30c3a.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-191')
| -rw-r--r-- | challenge-191/ulrich-rieke/cpp/ch-1.cpp | 33 | ||||
| -rw-r--r-- | challenge-191/ulrich-rieke/cpp/ch-2.cpp | 38 | ||||
| -rw-r--r-- | challenge-191/ulrich-rieke/haskell/ch-1.hs | 17 | ||||
| -rw-r--r-- | challenge-191/ulrich-rieke/haskell/ch-2.hs | 25 | ||||
| -rw-r--r-- | challenge-191/ulrich-rieke/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-191/ulrich-rieke/perl/ch-2.pl | 37 | ||||
| -rw-r--r-- | challenge-191/ulrich-rieke/raku/ch-1.raku | 17 | ||||
| -rw-r--r-- | challenge-191/ulrich-rieke/raku/ch-2.raku | 28 | ||||
| -rw-r--r-- | challenge-191/ulrich-rieke/rust/ch-1.rs | 20 | ||||
| -rw-r--r-- | challenge-191/ulrich-rieke/rust/ch-2.rs | 28 |
10 files changed, 260 insertions, 0 deletions
diff --git a/challenge-191/ulrich-rieke/cpp/ch-1.cpp b/challenge-191/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..35a1da15bf --- /dev/null +++ b/challenge-191/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,33 @@ +#include <iostream> +#include <vector> +#include <string> +#include <algorithm> + +std::vector<std::string> split( const std::string & startline , const std::string & sep ) { + std::vector<std::string> separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr(start , pos - start )) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +int main( ) { + std::cout << "Please enter a number of integers, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector<std::string> numberstrings( split( line , " " )) ; + std::vector<int> numbers ; + for ( auto & s : numberstrings ) + numbers.push_back( std::stoi( s )) ; + int maximum = *std::max_element( numbers.begin( ) , numbers.end( )) ; + if ( std::any_of( numbers.begin( ) , numbers.end( ) , [maximum]( const int n ) { + return ( n != maximum && maximum < 2 * n ) ; })) + std::cout << -1 << std::endl ; + else + std::cout << 1 << std::endl ; + return 0 ; +} diff --git a/challenge-191/ulrich-rieke/cpp/ch-2.cpp b/challenge-191/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..0cb6d01f46 --- /dev/null +++ b/challenge-191/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <vector> +#include <algorithm> +#include <utility> +#include <numeric> + +std::vector<std::pair<int , int>> createPairs( const std::vector<int> + & numbers ) { + std::vector<std::pair<int , int>> allPairs ; + int count = 1 ; + for ( int n : numbers ) { + allPairs.push_back( std::make_pair( count , n )) ; + count++ ; + } + return allPairs ; +} + +bool isCute( const std::vector<std::pair<int , int>> & allPairs ) { + return std::all_of( allPairs.begin( ) , allPairs.end( ) , []( const + auto & p) { return p.first % p.second == 0 || p.second % p.first == 0 ;}) ; +} + +int main( ) { + std::cout << "Please enter an integer greater than 0 and up to 15 inclusive!\n" ; + int number ; + std::cin >> number ; + std::vector<int> numbers ( number ) ; + std::iota( numbers.begin( ) , numbers.end( ) , 1 ) ; + int count = 0 ; + do { + std::vector<std::pair<int , int>> thePairs ( createPairs( numbers )) ; + if ( isCute( thePairs )) { + count++ ; + } + } while ( std::next_permutation( numbers.begin( ) , numbers.end( ))) ; + std::cout << count << std::endl ; + return 0 ; +} diff --git a/challenge-191/ulrich-rieke/haskell/ch-1.hs b/challenge-191/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..f734f80923 --- /dev/null +++ b/challenge-191/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,17 @@ +module Challenge191 + where +import Data.List ( delete ) + +solution :: [Int] -> Int +solution list = if all (\i -> max >= 2 * i ) theRest then 1 else -1 +where + max :: Int + max = maximum list + theRest :: [Int] + theRest = delete max list + +main :: IO ( ) +main = do + putStrLn "Please enter a numbers of integers separated by blanks!" ; + allNumbers <- getLine + print $ solution $ map read $ words allNumbers diff --git a/challenge-191/ulrich-rieke/haskell/ch-2.hs b/challenge-191/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..272a65097a --- /dev/null +++ b/challenge-191/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,25 @@ +module Challenge191_2 + where +import Data.List ( permutations ) + +isCute :: [Int] -> Bool +isCute list = all (\p -> (mod ( fst p ) ( snd p ) == 0) ||( mod ( snd p ) ( fst p ) +== 0) ) $ zip [1 , 2 ..] list + +solution :: Int -> Int +solution n = length $ filter isCute $ permutations [1..n] + +keepAskingForInput :: IO Int +keepAskingForInput = do + putStrLn "Please enter a number greater than 0 and up to and including 15!" + number <- getLine + let num = read number + if num > 0 && num < 16 + then + return num + else keepAskingForInput + +main :: IO ( ) +main = do + number <- keepAskingForInput + print $ solution number diff --git a/challenge-191/ulrich-rieke/perl/ch-1.pl b/challenge-191/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..01efcde55f --- /dev/null +++ b/challenge-191/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( max any ) ; + +say "Enter some integers, separated by blanks!" ; +my $line = <STDIN> ; +chomp $line ; +my @numbers = split( /\s/ , $line ) ; +my $maximum = max( @numbers ) ; +if ( any { ($_ != $maximum) && ($maximum < 2 * $_) } @numbers ) { + say -1 ; +} +else { + say 1 ; +} diff --git a/challenge-191/ulrich-rieke/perl/ch-2.pl b/challenge-191/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..6b348b6e39 --- /dev/null +++ b/challenge-191/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use Algorithm::Combinatorics qw ( permutations ) ; +use List::Util qw ( all ) ; + +sub createPairs { + my $array = shift ; + my @pairs ; + my $num = 1 ; + for my $i ( @$array ) { + push @pairs , [ $num , $i ] ; + $num++ ; + } + return @pairs ; +} + +sub isCute { + my $pairarray = shift ; + return all { ($_->[0] % $_->[1] == 0) || ($_->[1] % $_->[0] == 0) } + @$pairarray ; +} + +say "Enter a number greater than 0 and up to and including 15!" ; +my $n = <STDIN> ; +chomp $n ; +my @array = (1 .. $n ) ; +my $count = 0 ; +my $iter = permutations( \@array ) ; +while ( my $p = $iter->next ) { + my @currentPairs = createPairs( $p ) ; + if ( isCute( \@currentPairs ) ) { + $count++ ; + } +} +say $count ; diff --git a/challenge-191/ulrich-rieke/raku/ch-1.raku b/challenge-191/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..d5aa5d6447 --- /dev/null +++ b/challenge-191/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,17 @@ +use v6 ; + +say "Enter a number of integers, separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.split( /\s/ ).map( {.Int} ) ; +my $maximum = @numbers.max ; +my $retval ; +for @numbers -> $n { + if ( $maximum != $n && $maximum < 2 * $n ) { + $retval = -1 ; + last ; + } +} +unless $retval { + $retval = 1 ; +} +say $retval ; diff --git a/challenge-191/ulrich-rieke/raku/ch-2.raku b/challenge-191/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..59f2bccefb --- /dev/null +++ b/challenge-191/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,28 @@ +use v6 ; + +sub createPairs( $seq ) { + my @pairs ; + my $num = 1 ; + for ( 0 .. $seq.elems - 1 ) -> $i { + @pairs.push( [$num , $seq[ $i ]] ) ; + $num++ ; + } + return @pairs ; +} + +sub isCute( @array ) { + my $len = @array.elems ; + return @array.grep( {($_[0] %% $_[1]) || ($_[1] %% $_[0] ) } ).elems == $len ; +} + +say "Enter an positive integer up to and including 15!" ; +my $line = $*IN.get ; +my $number = +$line ; +my $count = 0 ; +for (1..$number).permutations -> $aSeq { + my @pairs = createPairs( $aSeq ) ; + if ( isCute( @pairs ) ) { + $count++ ; + } +} +say $count ; diff --git a/challenge-191/ulrich-rieke/rust/ch-1.rs b/challenge-191/ulrich-rieke/rust/ch-1.rs new file mode 100644 index 0000000000..b37bf2f510 --- /dev/null +++ b/challenge-191/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,20 @@ +use std::io ; + +fn main() { + let mut input : String = String::new( ) ; + println!("Enter a list of integers, separated by blanks!" ) ; + io::stdin( ).read_line( &mut input).unwrap( ) ; + let entered_line: &str = &*input ; + let numbers : Vec<i32> = entered_line.split_whitespace( ). + map( | s | s.trim( ).parse::<i32>( ).unwrap( ) ).collect( ) ; + let mut maximum : i32 = 0 ; + if let Some( n ) = numbers.iter( ).max( ) { + maximum = *n ; + } + if numbers.iter( ).any( | n | *n != maximum && maximum < 2 * *n ) { + println!("-1") ; + } + else { + println!("1") ; + } +} diff --git a/challenge-191/ulrich-rieke/rust/ch-2.rs b/challenge-191/ulrich-rieke/rust/ch-2.rs new file mode 100644 index 0000000000..d8cb41ab04 --- /dev/null +++ b/challenge-191/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,28 @@ +use itertools::Itertools ; +use std::io ; + +fn is_cute( a_vec: &Vec<i32> ) -> bool { + let c = | t : ( usize , &i32 ) | { + *t.1 % ( (t.0 + 1) as i32 ) == 0 || + ( (t.0 + 1) as i32 ) % *t.1 == 0 + } ; + a_vec.iter().enumerate( ).all( c ) +} + +fn main() { + println!("Please enter a number between 2 and 15!") ; + let mut input : String = String::new( ) ; + io::stdin().read_line( &mut input).unwrap( ) ; + let mut entered_line : &str = &*input ; + entered_line = entered_line.trim( ) ; + let limit : i32 = entered_line.parse::<i32>( ).unwrap( ) ; + let l : usize = limit as usize ; + let perms = (1..=limit).permutations( l ) ; + let mut count : i32 = 0 ; + for v in perms { + if is_cute( & v ) { + count += 1 ; + } + } + println!("{}" , count ) ; +} |
