From dfe459e065d51e41d8cc7feb00a5f53b5080d79e Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 11 Mar 2024 19:15:51 +0000 Subject: - Added solutions by Peter Meszaros. - Added solutions by Thomas Kohler. - Added solutions by Dave Jacoby. - Added solutions by Steven Wilson. - Added solutions by Ulrich Rieke. --- challenge-260/ulrich-rieke/cpp/ch-1.cpp | 26 ++++++++++++++++++++++ challenge-260/ulrich-rieke/haskell/ch-1.hs | 28 ++++++++++++++++++++++++ challenge-260/ulrich-rieke/haskell/ch-2.hs | 9 ++++++++ challenge-260/ulrich-rieke/perl/ch-1.pl | 23 ++++++++++++++++++++ challenge-260/ulrich-rieke/perl/ch-2.pl | 21 ++++++++++++++++++ challenge-260/ulrich-rieke/raku/ch-1.raku | 19 ++++++++++++++++ challenge-260/ulrich-rieke/raku/ch-2.raku | 14 ++++++++++++ challenge-260/ulrich-rieke/rust/ch-1.rs | 26 ++++++++++++++++++++++ challenge-260/ulrich-rieke/rust/ch-2.rs | 35 ++++++++++++++++++++++++++++++ 9 files changed, 201 insertions(+) create mode 100755 challenge-260/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-260/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-260/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-260/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-260/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-260/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-260/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-260/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-260/ulrich-rieke/rust/ch-2.rs (limited to 'challenge-260') diff --git a/challenge-260/ulrich-rieke/cpp/ch-1.cpp b/challenge-260/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..f9f90ce296 --- /dev/null +++ b/challenge-260/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +int main( ) { + std::cout << "Enter some integers, separated by blanks, e to end!\n" ; + std::vector numbers {std::istream_iterator{std::cin} , + std::istream_iterator{}} ; + std::set uniques( numbers.begin( ) , numbers.end( ) ); + std::vector frequencies ; + for ( auto i : uniques ) { + frequencies.push_back( std::count( numbers.begin( ) , + numbers.end( ) , i ) ) ; + } + std::set allFreq ( frequencies.begin( ) , frequencies.end( ) ) ; + if ( uniques.size( ) == allFreq.size( ) ) { + std::cout << 1 << '\n' ; + } + else { + std::cout << 0 << '\n' ; + } + return 0 ; +} + diff --git a/challenge-260/ulrich-rieke/haskell/ch-1.hs b/challenge-260/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..f490c07cb0 --- /dev/null +++ b/challenge-260/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,28 @@ +module Challenge260 + where +import qualified Data.Set as S + +count :: Eq a => a -> [a] -> Int +count _ [] = 0 +count d (x:xs) + |d == x = 1 + count d xs + |otherwise = count d xs + +solution :: [Int] -> Int +solution list = if l1 == l2 then 1 else 0 + where + uniques :: S.Set Int + uniques = S.fromList list + frequencies :: S.Set Int + frequencies = S.fromList $ map (\i -> count i list ) $ S.toList uniques + l1 :: Int + l1 = S.size uniques + l2 :: Int + l2 = S.size frequencies + +main :: IO ( ) +main = do + putStrLn "Enter some integers, separated by blanks!" + numberline <- getLine + print $ solution $ map read $ words numberline + diff --git a/challenge-260/ulrich-rieke/haskell/ch-2.hs b/challenge-260/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..62ef50a3ce --- /dev/null +++ b/challenge-260/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,9 @@ +module Challenge260_2 + where +import Data.List ( sort , findIndex , permutations ) +import qualified Data.Set as S +import Data.Maybe ( fromJust ) + +solution :: String -> Int +solution word = (fromJust $ findIndex ( == word ) $ sort $ S.toList $ + S.fromList $ permutations word) + 1 diff --git a/challenge-260/ulrich-rieke/perl/ch-1.pl b/challenge-260/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..5c7c8dba06 --- /dev/null +++ b/challenge-260/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter some integers, separated by blanks!" ; +my $line = ; +chomp $line ; +my %uniques ; +my %frequencies ; +my @numbers = split( /\s+/ , $line ) ; +for my $num ( @numbers ) { + $uniques{ $num }++ ; +} +for my $num ( keys %uniques ) { + $frequencies{ scalar( grep { $_ eq $num } @numbers ) }++ ; +} +if ( scalar( keys %frequencies ) == scalar( keys %uniques ) ) { + say 1 ; +} +else { + say 0 ; +} diff --git a/challenge-260/ulrich-rieke/perl/ch-2.pl b/challenge-260/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..cb2c5bccfe --- /dev/null +++ b/challenge-260/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use Algorithm::Combinatorics qw ( permutations ) ; + +say "Enter a word, preferably in capital letters only!" ; +my $word = ; +chomp $word ; +my %permuHash ; +my @letters = split( // , $word ) ; +my $iter = permutations( \@letters ) ; +while ( my $p = $iter->next ) { + $permuHash{ join( '' , @$p ) }++ ; +} +my @sorted = sort keys %permuHash ; +my $pos = 0 ; +do { + $pos++ ; +} while ( $sorted[ $pos - 1] ne $word ) ; +say $pos ; diff --git a/challenge-260/ulrich-rieke/raku/ch-1.raku b/challenge-260/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..3d4ec6c41e --- /dev/null +++ b/challenge-260/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} ) ; +my %uniques ; +my %frequencies ; +for @numbers -> $num { + %uniques{ $num }++ ; +} +for %uniques.keys -> $num { + %frequencies{ @numbers.grep( {$_ == $num} ).elems }++ ; +} +if ( %uniques.keys.elems == %frequencies.keys.elems) { + say 1 ; +} +else { + say 0 ; +} diff --git a/challenge-260/ulrich-rieke/raku/ch-2.raku b/challenge-260/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..2add9c778b --- /dev/null +++ b/challenge-260/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,14 @@ +use v6 ; + +say "Enter a word, preferably in capital letters only!" ; +my $word = $*IN.get ; +my %permuHash ; +for $word.comb.permutations -> $permu { + %permuHash{ $permu.join( '' )}++ ; +} +my @sorted = %permuHash.keys.sort ; +my $pos = 0 ; +repeat { + $pos++ ; +} until ( @sorted[ $pos - 1] eq $word ) ; +say $pos ; diff --git a/challenge-260/ulrich-rieke/rust/ch-1.rs b/challenge-260/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..54766093cf --- /dev/null +++ b/challenge-260/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,26 @@ +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 ; + let numbers : Vec = entered_line.split_whitespace( ).map( | s | + s.trim( ).parse::( ).unwrap( ) ).collect( ) ; + let mut uniques : HashSet = HashSet::new( ) ; + for n in &numbers { + uniques.insert( *n ) ; + } + let mut frequencies : HashSet = HashSet::new( ) ; + uniques.iter( ).map( | n | { + let fr = numbers.iter( ).filter( | &d | *d == *n ).count( ) ; + fr + }).for_each( |f| {frequencies.insert( f ) ;} ) ; + if uniques.len( ) == frequencies.len( ) { + println!("1") ; + } + else { + println!("0") ; + } +} diff --git a/challenge-260/ulrich-rieke/rust/ch-2.rs b/challenge-260/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..e12e8b5fd6 --- /dev/null +++ b/challenge-260/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,35 @@ +use std::io ; +use itertools::Itertools ; + +fn to_string( all_chars : &Vec ) -> String { + let mut output : String = String::new( ) ; + for c in all_chars { + output.push( *c ) ; + } + output +} + +fn main() { + println!("Enter a word, preferably in capital letters only!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let mut letters : Vec = Vec::new( ) ; + let changed = entered_line.trim( ) ; + let compared_to = changed.to_string( ) ; + for c in changed.chars( ) { + letters.push( c ) ; + } + let len : usize = letters.len( ) ; + let mut all_permu : Vec = Vec::new( ) ; + for ve in letters.into_iter().permutations( len ) { + let word : String = to_string( &ve ) ; + let found = all_permu.iter( ).find( | &st | *st == word ) ; + if found.is_none( ) { + all_permu.push( word ) ; + } + } + all_permu.sort( ) ; + println!("{}" , all_permu.iter( ).position( | w | *w == + compared_to ).unwrap( ) + 1) ; +} -- cgit