From d2c65435bbae2d980155bee8d4b2572e404e05b5 Mon Sep 17 00:00:00 2001 From: DevSanti12 Date: Sat, 14 Sep 2024 09:49:18 -0500 Subject: Added challenges for #285 --- challenge-285/santiago-leyva/perl/ch-01.pl | 58 ++++++++++++++++++++++++++ challenge-285/santiago-leyva/perl/ch-02.pl | 66 ++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 challenge-285/santiago-leyva/perl/ch-01.pl create mode 100644 challenge-285/santiago-leyva/perl/ch-02.pl diff --git a/challenge-285/santiago-leyva/perl/ch-01.pl b/challenge-285/santiago-leyva/perl/ch-01.pl new file mode 100644 index 0000000000..3b1b1ce918 --- /dev/null +++ b/challenge-285/santiago-leyva/perl/ch-01.pl @@ -0,0 +1,58 @@ +=begin +You are given a list of routes, @routes. + +Write a script to find the destination with no further outgoing connection. + +Example 1 +Input: @routes = (["B","C"], ["D","B"], ["C","A"]) +Output: "A" + +"D" -> "B" -> "C" -> "A". +"B" -> "C" -> "A". +"C" -> "A". +"A". +Example 2 +Input: @routes = (["A","Z"]) +Output: "Z" +=cut + +use strict; +use Data::Dumper; + +my @input = ([["B","C"], ["D","B"], ["C","A"]],[["A","Z"]]); + +foreach(@input){ + my $arr = $_; + my @A = @$arr; + my $dead_end = findDeadEnd(\@A); + print $dead_end."\n"; +} + +sub findDeadEnd { + my $array = shift; + my @route = @$array; + my @origin; + my @destination; + my @deadend; + for(0..scalar @route-1){ + push @origin, $route[$_][0]; + } + for(0..scalar @route-1){ + push @destination, $route[$_][1]; + } + + foreach my $i (@destination){ + if( !(grep( /^$i$/, @origin)) ){ + push @deadend, $i; + } + + } + + if(scalar @deadend > 1){ + return "There are multiple routes with no outgoing connection"; + }elsif(scalar @deadend == 0){ + return "All routes have an outgoing connection"; + } + return $deadend[0]; + +} diff --git a/challenge-285/santiago-leyva/perl/ch-02.pl b/challenge-285/santiago-leyva/perl/ch-02.pl new file mode 100644 index 0000000000..6ebc42be82 --- /dev/null +++ b/challenge-285/santiago-leyva/perl/ch-02.pl @@ -0,0 +1,66 @@ +=begin +Compute the number of ways to make change for given amount in cents. By using the coins e.g. Penny, Nickel, Dime, Quarter and Half-dollar, in how many distinct ways can the total value equal to the given amount? Order of coin selection does not matter. + +A penny (P) is equal to 1 cent. +A nickel (N) is equal to 5 cents. +A dime (D) is equal to 10 cents. +A quarter (Q) is equal to 25 cents. +A half-dollar (HD) is equal to 50 cents. +Example 1 +Input: $amount = 9 +Ouput: 2 + +1: 9P +2: N + 4P +Example 2 +Input: $amount = 15 +Ouput: 6 + +1: D + 5P +2: D + N +3: 3N +4: 2N + 5P +5: N + 10P +6: 15P +Example 3 +Input: $amount = 100 +Ouput: 292 +=cut + +use strict; +use Data::Dumper; + +use Test::More tests => 3; + +is(makeChange(9),2,'Test 1'); +is(makeChange(15),6,'Test 2'); +is(makeChange(100),292,'Test 3'); + + +my @amount = (9,15,100); + +foreach(@amount){ + my $_last_coin; + my $combination = makeChange($_,$_last_coin); + print "Number of Combinations for $_ is: $combination\n"; +} + +sub makeChange { + my ($reamaining,$last_coin) = @_; + my $combinations = 0; + my @denomination = (1,5,10,25,50); + #print "$reamaining,$last_coin\n"; + foreach my $coin (@denomination){ + if($last_coin and $last_coin < $coin){ + #print "$last_coin < $coin next\n"; + last; + }elsif($coin == $reamaining){ + $combinations += 1; + }elsif($coin < $reamaining){ + $combinations += makeChange($reamaining-$coin,$coin); + } + } + + return $combinations; + +} \ No newline at end of file -- cgit