diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-02 22:05:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-02 22:05:58 +0100 |
| commit | 5700092f38ceae57dc000f4f9538207779dcf643 (patch) | |
| tree | 84eb759cbb8d26315023462c8655750ec5dfe8e7 /challenge-285 | |
| parent | 94a85756a38dfc0ef9538b85712345b848e0b780 (diff) | |
| parent | ba7bd85b4ae5c65a9d91f94e650f211753363694 (diff) | |
| download | perlweeklychallenge-club-5700092f38ceae57dc000f4f9538207779dcf643.tar.gz perlweeklychallenge-club-5700092f38ceae57dc000f4f9538207779dcf643.tar.bz2 perlweeklychallenge-club-5700092f38ceae57dc000f4f9538207779dcf643.zip | |
Merge pull request #10761 from pauloscustodio/master
Add solutions
Diffstat (limited to 'challenge-285')
| -rw-r--r-- | challenge-285/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-285/paulo-custodio/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-285/paulo-custodio/perl/ch-2.pl | 63 | ||||
| -rw-r--r-- | challenge-285/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-285/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 133 insertions, 0 deletions
diff --git a/challenge-285/paulo-custodio/Makefile b/challenge-285/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-285/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-285/paulo-custodio/perl/ch-1.pl b/challenge-285/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..c91b661a60 --- /dev/null +++ b/challenge-285/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 285 +# +# Task 1: No Connection +# Submitted by: Mohammad Sajid Anwar +# +# 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" + +use Modern::Perl; + +my @routes = map {[split ' ', $_]} split /,/, "@ARGV"; +say join ", ", endpoints(@routes); + +sub endpoints { + my(@routes) = @_; + my %endpoints; + for (@routes) { + my($a, $b) = @$_; + $endpoints{$b}=1; + } + for (@routes) { + my($a, $b) = @$_; + delete $endpoints{$a}; + } + return sort keys %endpoints; +} diff --git a/challenge-285/paulo-custodio/perl/ch-2.pl b/challenge-285/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..9bad9a6116 --- /dev/null +++ b/challenge-285/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +# Challenge 285 +# +# Task 2: Making Change +# Submitted by: David Ferrone +# +# 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 + +use Modern::Perl; + +my @COINS = (50,25,10,5,1); + +my $amount = shift//0; +my $num_ways = compute_num_ways($amount, @COINS); +say $num_ways; + +sub compute_num_ways { + my($amount, @coins) = @_; + while (@coins>1 && $coins[0]>$amount) { + shift @coins; + } + return 1 if @coins==1; # return in cents + my $count; + for (my $v = 0; $v <= $amount; $v += $coins[0]) { + $count += compute_num_ways($amount-$v, @coins[1..$#coins]) + } + return $count; +} diff --git a/challenge-285/paulo-custodio/t/test-1.yaml b/challenge-285/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..836f002f22 --- /dev/null +++ b/challenge-285/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: B C , D B , C A + input: + output: A +- setup: + cleanup: + args: A Z + input: + output: Z diff --git a/challenge-285/paulo-custodio/t/test-2.yaml b/challenge-285/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..70cfc904f2 --- /dev/null +++ b/challenge-285/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 9 + input: + output: 2 +- setup: + cleanup: + args: 15 + input: + output: 6 +- setup: + cleanup: + args: 100 + input: + output: 292 |
