diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-09-02 22:11:43 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-09-02 22:11:43 +0100 |
| commit | 25b6f89a6bbb4860b072d2f20ab72d6f4e4aa57d (patch) | |
| tree | 6c76cce36b5db283bc17c8e041453c8852c32d9b /challenge-285 | |
| parent | 5700092f38ceae57dc000f4f9538207779dcf643 (diff) | |
| download | perlweeklychallenge-club-25b6f89a6bbb4860b072d2f20ab72d6f4e4aa57d.tar.gz perlweeklychallenge-club-25b6f89a6bbb4860b072d2f20ab72d6f4e4aa57d.tar.bz2 perlweeklychallenge-club-25b6f89a6bbb4860b072d2f20ab72d6f4e4aa57d.zip | |
- Added solutions by Ulrich Rieke.
- Added solutions by Paulo Custodio.
- Added solutions by David Ferrone.
- Added solutions by W. Luis Mochan.
Diffstat (limited to 'challenge-285')
| -rwxr-xr-x | challenge-285/ulrich-rieke/cpp/ch-1.cpp | 40 | ||||
| -rwxr-xr-x | challenge-285/ulrich-rieke/cpp/ch-2.cpp | 26 | ||||
| -rwxr-xr-x | challenge-285/ulrich-rieke/haskell/ch-1.hs | 19 | ||||
| -rwxr-xr-x | challenge-285/ulrich-rieke/haskell/ch-2.hs | 16 | ||||
| -rwxr-xr-x | challenge-285/ulrich-rieke/perl/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-285/ulrich-rieke/perl/ch-2.pl | 25 | ||||
| -rwxr-xr-x | challenge-285/ulrich-rieke/raku/ch-1.raku | 12 | ||||
| -rwxr-xr-x | challenge-285/ulrich-rieke/raku/ch-2.raku | 21 | ||||
| -rwxr-xr-x | challenge-285/ulrich-rieke/rust/ch-1.rs | 21 | ||||
| -rwxr-xr-x | challenge-285/ulrich-rieke/rust/ch-2.rs | 25 |
10 files changed, 222 insertions, 0 deletions
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 <iostream>
+#include <string>
+#include <sstream>
+#include <algorithm>
+#include <vector>
+#include <iterator>
+
+std::vector<std::string> split( std::string text , const char delimiter ) {
+ std::istringstream istr { text } ;
+ std::vector<std::string> 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<std::string> routes { split( line, ',' ) } ;
+ std::vector<std::vector<std::string>> allRoutes ;
+ std::vector<std::string> 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<std::string> 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::string>( 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 <iostream>
+
+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 = <STDIN> ;
+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 = <STDIN> ;
+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<Vec<&str>> = 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::<u32>( ).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 ) ; +} |
