aboutsummaryrefslogtreecommitdiff
path: root/challenge-260
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2024-03-11 19:15:51 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2024-03-11 19:15:51 +0000
commitdfe459e065d51e41d8cc7feb00a5f53b5080d79e (patch)
treef48f9ce587f341608185577f8d7232418e3a14f8 /challenge-260
parent9ab95dda4f101c63ac38b810849665103b75f983 (diff)
downloadperlweeklychallenge-club-dfe459e065d51e41d8cc7feb00a5f53b5080d79e.tar.gz
perlweeklychallenge-club-dfe459e065d51e41d8cc7feb00a5f53b5080d79e.tar.bz2
perlweeklychallenge-club-dfe459e065d51e41d8cc7feb00a5f53b5080d79e.zip
- 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.
Diffstat (limited to 'challenge-260')
-rwxr-xr-xchallenge-260/ulrich-rieke/cpp/ch-1.cpp26
-rwxr-xr-xchallenge-260/ulrich-rieke/haskell/ch-1.hs28
-rwxr-xr-xchallenge-260/ulrich-rieke/haskell/ch-2.hs9
-rwxr-xr-xchallenge-260/ulrich-rieke/perl/ch-1.pl23
-rwxr-xr-xchallenge-260/ulrich-rieke/perl/ch-2.pl21
-rwxr-xr-xchallenge-260/ulrich-rieke/raku/ch-1.raku19
-rwxr-xr-xchallenge-260/ulrich-rieke/raku/ch-2.raku14
-rwxr-xr-xchallenge-260/ulrich-rieke/rust/ch-1.rs26
-rwxr-xr-xchallenge-260/ulrich-rieke/rust/ch-2.rs35
9 files changed, 201 insertions, 0 deletions
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 <iostream>
+#include <vector>
+#include <set>
+#include <iterator>
+#include <algorithm>
+
+int main( ) {
+ std::cout << "Enter some integers, separated by blanks, e to end!\n" ;
+ std::vector<int> numbers {std::istream_iterator<int>{std::cin} ,
+ std::istream_iterator<int>{}} ;
+ std::set<int> uniques( numbers.begin( ) , numbers.end( ) );
+ std::vector<int> frequencies ;
+ for ( auto i : uniques ) {
+ frequencies.push_back( std::count( numbers.begin( ) ,
+ numbers.end( ) , i ) ) ;
+ }
+ std::set<int> 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 = <STDIN> ;
+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 = <STDIN> ;
+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<i32> = entered_line.split_whitespace( ).map( | s |
+ s.trim( ).parse::<i32>( ).unwrap( ) ).collect( ) ;
+ let mut uniques : HashSet<i32> = HashSet::new( ) ;
+ for n in &numbers {
+ uniques.insert( *n ) ;
+ }
+ let mut frequencies : HashSet<usize> = 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<char> ) -> 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<char> = 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<String> = 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) ;
+}