From 25b6f89a6bbb4860b072d2f20ab72d6f4e4aa57d Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Mon, 2 Sep 2024 22:11:43 +0100 Subject: - Added solutions by Ulrich Rieke. - Added solutions by Paulo Custodio. - Added solutions by David Ferrone. - Added solutions by W. Luis Mochan. --- challenge-285/ulrich-rieke/cpp/ch-1.cpp | 40 ++++++++++++++++++++++++++++++ challenge-285/ulrich-rieke/cpp/ch-2.cpp | 26 +++++++++++++++++++ challenge-285/ulrich-rieke/haskell/ch-1.hs | 19 ++++++++++++++ challenge-285/ulrich-rieke/haskell/ch-2.hs | 16 ++++++++++++ challenge-285/ulrich-rieke/perl/ch-1.pl | 17 +++++++++++++ challenge-285/ulrich-rieke/perl/ch-2.pl | 25 +++++++++++++++++++ challenge-285/ulrich-rieke/raku/ch-1.raku | 12 +++++++++ challenge-285/ulrich-rieke/raku/ch-2.raku | 21 ++++++++++++++++ challenge-285/ulrich-rieke/rust/ch-1.rs | 21 ++++++++++++++++ challenge-285/ulrich-rieke/rust/ch-2.rs | 25 +++++++++++++++++++ 10 files changed, 222 insertions(+) create mode 100755 challenge-285/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-285/ulrich-rieke/cpp/ch-2.cpp create mode 100755 challenge-285/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-285/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-285/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-285/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-285/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-285/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-285/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-285/ulrich-rieke/rust/ch-2.rs (limited to 'challenge-285') diff --git a/challenge-285/ulrich-rieke/cpp/ch-1.cpp b/challenge-285/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..5055130792 --- /dev/null +++ b/challenge-285/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include + +std::vector split( std::string text , const char delimiter ) { + std::istringstream istr { text } ; + std::vector tokens ; + std::string word ; + while ( std::getline( istr , word , delimiter )) + tokens.push_back( word ) ; + return tokens ; +} + +int main( ) { + std::cout << "Enter some routes, separated by comma!\n" ; + std::cout << "For every route, separate start and destination by blank!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector routes { split( line, ',' ) } ; + std::vector> allRoutes ; + std::vector starts ;//for all start fields + for ( auto route : routes ) { + auto routepair = split( route , ' ' ) ;//first vector is start , then end + allRoutes.push_back( routepair ) ; + starts.push_back( routepair[0] ) ;//append start to collection of starts + } + std::vector solution ; + for ( auto p : allRoutes ) { + if ( std::find( starts.begin( ) , starts.end( ) , p[1] ) == + starts.end( ) ) //destination not found in starts + solution.push_back( p[1] ) ;//so part of solutin + } + std::copy( solution.begin( ) , solution.end( ) , + std::ostream_iterator( std::cout , " " ) ) ; + std::cout << '\n' ; + return 0 ; +} diff --git a/challenge-285/ulrich-rieke/cpp/ch-2.cpp b/challenge-285/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..e68be4c870 --- /dev/null +++ b/challenge-285/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,26 @@ +#include + +int main( ) { + int amount ; + std::cout << "Enter an amount of money in pennies!\n" ; + std::cin >> amount ; + int combis = 0 ; + for (int pennies = 0 ; pennies < amount + 1 ; pennies++ ) { + for ( int nickels = 0 ; nickels < (amount / 5 ) + 1 ; nickels++ ) { + for ( int dimes = 0 ; dimes < ( amount / 10 ) + 1 ; dimes++ ) { + for ( int quarters = 0 ; quarters < ( amount / 25 ) + 1 ; + quarters++ ) { + for ( int half_dollars = 0 ; half_dollars < ( amount / 50 ) + + 1 ; half_dollars++ ) { + if ( pennies + nickels * 5 + dimes * 10 + + quarters * 25 + half_dollars * 50 == amount ) + combis++ ; + } + } + } + } + } + std::cout << combis << '\n' ; + return 0 ; +} + diff --git a/challenge-285/ulrich-rieke/haskell/ch-1.hs b/challenge-285/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..0ddcd35d73 --- /dev/null +++ b/challenge-285/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,19 @@ +module Challenge285 + where +import Data.List.Split ( splitOn ) + +solution :: String -> String +solution input = + let routes = splitOn "," input + routepairs = map (\li -> (head li , last li ) ) $ map ( concat . + splitOn " " ) routes + starts = map fst routepairs + destinations = map snd routepairs + in filter (\dest -> notElem dest starts ) destinations + +main :: IO ( ) +main = do + putStrLn "Enter some routes, separated by ','!" + putStrLn "Separate start and destination by a blank!" + allRoutes <- getLine + print $ solution allRoutes diff --git a/challenge-285/ulrich-rieke/haskell/ch-2.hs b/challenge-285/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..e17bc8f81f --- /dev/null +++ b/challenge-285/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,16 @@ +module Challenge285_2 + where + +denominations :: [Int] +denominations = [50, 25 , 10 , 5 , 1] + +findCoinCombis :: [Int] -> Int -> [[Int]] +findCoinCombis [1] n = [[n]] +findCoinCombis ( den:rest ) n = [c:cs | c <- [0..div n den] , cs <- + findCoinCombis rest ( n - c * den )] + +main :: IO ( ) +main = do + putStrLn "Enter an amount of money in pennies!" + amount <- getLine + print $ length $ findCoinCombis denominations $ read amount diff --git a/challenge-285/ulrich-rieke/perl/ch-1.pl b/challenge-285/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..e66b2d5d88 --- /dev/null +++ b/challenge-285/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter some routes ( start and destination separated by blank)!" ; +say "Enter routes by ','!" ; +my $line = ; +chomp( $line ) ; +my @routes = split( /\,/ , $line ) ; +my %routepairs ; +for my $route ( @routes ) { + my ( $start , $destination ) = split( /\s/ , $route ) ; + $routepairs{$start} = $destination ; +} +say join( ',' , grep { not ( exists( $routepairs{$_} ) ) } values + %routepairs ) ; diff --git a/challenge-285/ulrich-rieke/perl/ch-2.pl b/challenge-285/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..ef36e70a0d --- /dev/null +++ b/challenge-285/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use POSIX ; + +say "Enter an amount of money in pennies!" ; +my $amount = ; +chomp $amount ; +my $combis = 0 ; +for my $pennies( 0..$amount) { + for my $nickels( 0..floor( $amount / 5 ) ) { + for my $dimes( 0..floor( $amount / 10 ) ) { + for my $quarters( 0..floor( $amount / 25 ) ) { + for my $half_dollars( 0..floor( $amount / 50 ) ) { + if ( $pennies + $nickels * 5 + $dimes * 10 + $quarters * 25 + + $half_dollars * 50 == $amount ) { + $combis++ ; + } + } + } + } + } +} +say $combis ; diff --git a/challenge-285/ulrich-rieke/raku/ch-1.raku b/challenge-285/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..3dd8e08e78 --- /dev/null +++ b/challenge-285/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,12 @@ +use v6 ; + +say "Enter some routes, start and destination separated by blanks!" ; +say "Separate routes by ','!" ; +my $line = $*IN.get ; +my @routes = $line.split( /','/ ) ; +my %pairs ; +for @routes -> $route { + my ($start , $destination) = $route.split( /\s/ ) ; + %pairs{$start} = $destination ; +} +say join( ',' , %pairs.values.grep( {not %pairs{$_}:exists } )) ; diff --git a/challenge-285/ulrich-rieke/raku/ch-2.raku b/challenge-285/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..b6b7421d7b --- /dev/null +++ b/challenge-285/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,21 @@ +use v6 ; + +say "Enter an amount of money in cents!" ; +my $line = $*IN.get ; +my $amount = $line.Int ; +my $combis = 0 ; +for (0..$amount) -> $pennies { + for (0..$amount div 5 ) -> $nickels { + for (0..$amount div 10 ) -> $dimes { + for ( 0..$amount div 25) -> $quarters { + for ( 0..$amount div 50 ) -> $half-dollars { + if ($pennies + $nickels * 5 + $dimes * 10 + + $quarters * 25 + $half-dollars * 50 == $amount ) { + $combis++ ; + } + } + } + } + } +} +say $combis ; diff --git a/challenge-285/ulrich-rieke/rust/ch-1.rs b/challenge-285/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..1de72ede46 --- /dev/null +++ b/challenge-285/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,21 @@ +use std::io ; + +fn main() { + println!("Enter some routes!"); + println!("Enter start and destination by blank, routes by ','!" ) ; + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = inline.as_str( ).trim( ) ; + let routes : Vec<&str> = entered_line.split( ",").collect( ) ; + let pairs : Vec> = routes.iter( ).map( | s | + s.split_whitespace( ).collect( ) ).collect( ) ; + let mut solution : Vec<&str> = Vec::new( ) ; + for p in &pairs { + let destination = p[1] ; + if pairs.iter( ).filter( | &a_pair | a_pair[0] == destination ). + count( ) == 0 { + solution.push( destination ) ; + } + } + println!("{:?}" , solution ) ; +} diff --git a/challenge-285/ulrich-rieke/rust/ch-2.rs b/challenge-285/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..fa78414e07 --- /dev/null +++ b/challenge-285/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,25 @@ +use std::io ; + +fn main() { + println!("Enter an amount of money in cents!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = inline.as_str().trim( ) ; + let amount : u32 = entered_line.parse::( ).unwrap( ) ; + let mut combis : u64 = 0 ; + for pennies in 0..=amount { + for nickels in 0..=amount/5 { + for dimes in 0..=amount / 10 { + for quarters in 0..=amount / 25 { + for half_dollars in 0..=amount / 50 { + if pennies + nickels * 5 + dimes * 10 + quarters * 25 + + half_dollars * 50 == amount { + combis += 1 ; + } + } + } + } + } + } + println!("{}" , combis ) ; +} -- cgit