aboutsummaryrefslogtreecommitdiff
path: root/challenge-285
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-285')
-rw-r--r--challenge-285/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-285/jaldhar-h-vyas/perl/ch-1.pl25
-rwxr-xr-xchallenge-285/jaldhar-h-vyas/perl/ch-2.pl32
-rwxr-xr-xchallenge-285/jaldhar-h-vyas/raku/ch-1.sh3
-rwxr-xr-xchallenge-285/jaldhar-h-vyas/raku/ch-2.raku33
-rw-r--r--challenge-285/lubos-kolouch/perl/ch-1.pl51
-rw-r--r--challenge-285/lubos-kolouch/perl/ch-2.pl57
-rw-r--r--challenge-285/lubos-kolouch/python/ch-1.py52
-rw-r--r--challenge-285/lubos-kolouch/python/ch-2.py56
-rw-r--r--challenge-285/santiago-leyva/perl/ch-1.pl58
-rw-r--r--challenge-285/santiago-leyva/perl/ch-2.pl66
11 files changed, 434 insertions, 0 deletions
diff --git a/challenge-285/jaldhar-h-vyas/blog.txt b/challenge-285/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..3a342f4330
--- /dev/null
+++ b/challenge-285/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2024/09/perl_weekly_challenge_week_285.html
diff --git a/challenge-285/jaldhar-h-vyas/perl/ch-1.pl b/challenge-285/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..48f35569c3
--- /dev/null
+++ b/challenge-285/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+use v5.38;
+
+sub setDifference($arr1, $arr2) {
+ my %difference = map { $_ => 0 } @{$arr1};
+
+ for my $elem (@{$arr2}) {
+ if (exists $difference{$elem}) {
+ $difference{$elem}++;
+ }
+ }
+
+ return sort grep { !$difference{$_} } keys %difference;
+}
+
+my @first;
+my @second;
+
+for my $arg (@ARGV) {
+ @_ = split /\s+/, $arg;
+ push @first, $_[0];
+ push @second, $_[1];
+}
+
+say join q{ }, setDifference(\@second, \@first); \ No newline at end of file
diff --git a/challenge-285/jaldhar-h-vyas/perl/ch-2.pl b/challenge-285/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..f394ead681
--- /dev/null
+++ b/challenge-285/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+use v5.38;
+no warnings qw/ recursion /;
+use constant PENNY => 1;
+use constant NICKEL => 5;
+use constant DIME => 10;
+use constant QUARTER => 25;
+use constant HALFDOLLAR => 50;
+
+sub changeCombinations($amount, $largestCoin){
+ state @coins=(PENNY, NICKEL, DIME, QUARTER, HALFDOLLAR);
+ if ($largestCoin > $amount) {
+ return 0;
+ }
+
+ if ($largestCoin == $amount) {
+ return 1;
+ }
+
+ my $total;
+
+ for my $coin (grep {$_ <= $largestCoin} @coins) {
+ $total += changeCombinations($amount - $largestCoin, $coin);
+ }
+
+ return $total;
+}
+
+my ($amount) = @ARGV;
+my $largestCoin = HALFDOLLAR;
+
+say changeCombinations($amount + $largestCoin, $largestCoin)
diff --git a/challenge-285/jaldhar-h-vyas/raku/ch-1.sh b/challenge-285/jaldhar-h-vyas/raku/ch-1.sh
new file mode 100755
index 0000000000..4e779662a6
--- /dev/null
+++ b/challenge-285/jaldhar-h-vyas/raku/ch-1.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+raku -e 'my @a=@*ARGS.words.pairup;(@a.map({$_.value})∖@a.map({$_.key})).keys.join.say' "$@" \ No newline at end of file
diff --git a/challenge-285/jaldhar-h-vyas/raku/ch-2.raku b/challenge-285/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..6c53fe4b65
--- /dev/null
+++ b/challenge-285/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/raku
+
+constant PENNY = 1;
+constant NICKEL = 5;
+constant DIME = 10;
+constant QUARTER = 25;
+constant HALFDOLLAR = 50;
+
+sub changeCombinations($amount, $largestCoin){
+ constant @coins = (HALFDOLLAR, QUARTER, DIME, NICKEL, PENNY);
+
+ if $largestCoin > $amount {
+ return 0;
+ }
+
+ if $largestCoin == $amount {
+ return 1;
+ }
+
+ my $total;
+
+ for @coins.grep({ $_ <= $largestCoin }) -> $coin {
+ $total += changeCombinations($amount - $largestCoin, $coin);
+ }
+
+ return $total;
+}
+
+sub MAIN(
+ Int $amount
+) {
+ say changeCombinations($amount + HALFDOLLAR, HALFDOLLAR);
+} \ No newline at end of file
diff --git a/challenge-285/lubos-kolouch/perl/ch-1.pl b/challenge-285/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..63d294742a
--- /dev/null
+++ b/challenge-285/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+=pod
+
+=head1 DESCRIPTION
+
+This script finds the destination with no further outgoing connection from a given list of routes.
+
+=head1 FUNCTIONS
+
+=head2 find_destination(\@routes)
+
+Given an array reference of routes, where each route is an array reference containing a source and a destination city, this function returns the city that is a destination but not a source in any route.
+
+=over 4
+
+=item * C<\@routes> - Reference to an array of routes (array references of source and destination cities).
+
+=back
+
+Returns the destination city with no outgoing connections.
+
+=cut
+
+sub find_destination {
+ my ($routes_ref) = @_;
+ my %sources;
+ my %destinations;
+
+ foreach my $route (@$routes_ref) {
+ my ($source, $destination) = @$route;
+ $sources{$source} = 1;
+ $destinations{$destination} = 1;
+ }
+
+ foreach my $city (keys %destinations) {
+ unless (exists $sources{$city}) {
+ return $city;
+ }
+ }
+ return undef; # In case there is no such city
+}
+
+# Unit Tests
+is(find_destination([["B","C"], ["D","B"], ["C","A"]]), "A", 'Example 1');
+is(find_destination([["A","Z"]]), "Z", 'Example 2');
+
+done_testing();
diff --git a/challenge-285/lubos-kolouch/perl/ch-2.pl b/challenge-285/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..4ca1821de2
--- /dev/null
+++ b/challenge-285/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+=pod
+
+=head1 DESCRIPTION
+
+This script computes the number of distinct ways to make change for a given amount in cents using US coins.
+
+Coins available:
+- Penny (P): 1 cent
+- Nickel (N): 5 cents
+- Dime (D): 10 cents
+- Quarter (Q): 25 cents
+- Half-dollar (HD): 50 cents
+
+Order of coin selection does not matter.
+
+=head1 FUNCTIONS
+
+=head2 making_change($amount)
+
+Computes the number of ways to make change for the given amount.
+
+=over 4
+
+=item * C<$amount> - The amount in cents (non-negative integer).
+
+=back
+
+Returns the number of distinct ways to make change.
+
+=cut
+
+sub making_change {
+ my ($amount) = @_;
+ my @coins = (1, 5, 10, 25, 50);
+ my @dp = (0) x ($amount + 1);
+ $dp[0] = 1;
+
+ foreach my $coin (@coins) {
+ for my $i ($coin .. $amount) {
+ $dp[$i] += $dp[$i - $coin];
+ }
+ }
+
+ return $dp[$amount];
+}
+
+# Unit Tests
+is(making_change(9), 2, 'Example 1');
+is(making_change(15), 6, 'Example 2');
+is(making_change(100), 292, 'Example 3');
+
+done_testing();
diff --git a/challenge-285/lubos-kolouch/python/ch-1.py b/challenge-285/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..be24d53a49
--- /dev/null
+++ b/challenge-285/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,52 @@
+from typing import List
+import unittest
+
+
+def find_destination(routes: List[List[str]]) -> str:
+ """
+ Finds the destination with no further outgoing connection from a given list of routes.
+
+ Each route is represented as a list containing a source and a destination city.
+
+ Args:
+ routes (List[List[str]]): A list of routes.
+
+ Returns:
+ str: The destination city with no outgoing connections.
+
+ """
+ sources = set()
+ destinations = set()
+
+ for route in routes:
+ source, destination = route
+ sources.add(source)
+ destinations.add(destination)
+
+ no_outgoing = destinations - sources
+
+ if no_outgoing:
+ # Assuming there is only one such city
+ return no_outgoing.pop()
+ else:
+ return None # In case there is no such city
+
+
+# Unit Tests
+class TestNoConnection(unittest.TestCase):
+
+ def test_example1(self):
+ routes = [["B", "C"], ["D", "B"], ["C", "A"]]
+ self.assertEqual(find_destination(routes), "A", 'Example 1')
+
+ def test_example2(self):
+ routes = [["A", "Z"]]
+ self.assertEqual(find_destination(routes), "Z", 'Example 2')
+
+ def test_no_destination(self):
+ routes = [["A", "B"], ["B", "A"]]
+ self.assertIsNone(find_destination(routes), 'No unique destination')
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/challenge-285/lubos-kolouch/python/ch-2.py b/challenge-285/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..60182732c2
--- /dev/null
+++ b/challenge-285/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,56 @@
+from typing import List
+import unittest
+
+
+def making_change(amount: int) -> int:
+ """
+ Compute the number of ways to make change for the given amount in cents using US coins.
+
+ Coins available:
+ - Penny (P): 1 cent
+ - Nickel (N): 5 cents
+ - Dime (D): 10 cents
+ - Quarter (Q): 25 cents
+ - Half-dollar (HD): 50 cents
+
+ Order of coin selection does not matter.
+
+ Args:
+ amount (int): The amount in cents (non-negative integer).
+
+ Returns:
+ int: The number of distinct ways to make change.
+ """
+ coins = [1, 5, 10, 25, 50]
+ dp = [0] * (amount + 1)
+ dp[0] = 1 # There is one way to make 0 cents
+
+ for coin in coins:
+ for i in range(coin, amount + 1):
+ dp[i] += dp[i - coin]
+
+ return dp[amount]
+
+
+# Unit Tests
+class TestMakingChange(unittest.TestCase):
+
+ def test_example1(self):
+ self.assertEqual(making_change(9), 2, 'Example 1')
+
+ def test_example2(self):
+ self.assertEqual(making_change(15), 6, 'Example 2')
+
+ def test_example3(self):
+ self.assertEqual(making_change(100), 292, 'Example 3')
+
+ def test_zero_amount(self):
+ self.assertEqual(making_change(0), 1, 'Zero amount')
+
+ def test_negative_amount(self):
+ with self.assertRaises(IndexError):
+ making_change(-1)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/challenge-285/santiago-leyva/perl/ch-1.pl b/challenge-285/santiago-leyva/perl/ch-1.pl
new file mode 100644
index 0000000000..3b1b1ce918
--- /dev/null
+++ b/challenge-285/santiago-leyva/perl/ch-1.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-2.pl b/challenge-285/santiago-leyva/perl/ch-2.pl
new file mode 100644
index 0000000000..6ebc42be82
--- /dev/null
+++ b/challenge-285/santiago-leyva/perl/ch-2.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