aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-02 22:05:58 +0100
committerGitHub <noreply@github.com>2024-09-02 22:05:58 +0100
commit5700092f38ceae57dc000f4f9538207779dcf643 (patch)
tree84eb759cbb8d26315023462c8655750ec5dfe8e7
parent94a85756a38dfc0ef9538b85712345b848e0b780 (diff)
parentba7bd85b4ae5c65a9d91f94e650f211753363694 (diff)
downloadperlweeklychallenge-club-5700092f38ceae57dc000f4f9538207779dcf643.tar.gz
perlweeklychallenge-club-5700092f38ceae57dc000f4f9538207779dcf643.tar.bz2
perlweeklychallenge-club-5700092f38ceae57dc000f4f9538207779dcf643.zip
Merge pull request #10761 from pauloscustodio/master
Add solutions
-rw-r--r--challenge-001/paulo-custodio/stats.pl3
-rw-r--r--challenge-051/paulo-custodio/python/ch-1.py33
-rw-r--r--challenge-051/paulo-custodio/python/ch-2.py28
-rw-r--r--challenge-285/paulo-custodio/Makefile2
-rw-r--r--challenge-285/paulo-custodio/perl/ch-1.pl43
-rw-r--r--challenge-285/paulo-custodio/perl/ch-2.pl63
-rw-r--r--challenge-285/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-285/paulo-custodio/t/test-2.yaml15
8 files changed, 196 insertions, 1 deletions
diff --git a/challenge-001/paulo-custodio/stats.pl b/challenge-001/paulo-custodio/stats.pl
index 6e8b69719d..1ff3a450ce 100644
--- a/challenge-001/paulo-custodio/stats.pl
+++ b/challenge-001/paulo-custodio/stats.pl
@@ -40,11 +40,12 @@ for my $chall_dir (path(".")->children(qr/challenge-\d+/)) {
}
}
-# challenge 232 did not happen
+# challenge 232 did not exist
for my $lang (sort keys %LANG) {
$sols[232]{$lang} = 2;
}
+
# output
for my $chall (1 .. $#sols) {
if (($chall) % 10 == 1) {
diff --git a/challenge-051/paulo-custodio/python/ch-1.py b/challenge-051/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..41d49dfb04
--- /dev/null
+++ b/challenge-051/paulo-custodio/python/ch-1.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+
+# Challenge 051
+#
+# TASK #1
+# 3 Sum
+# Given an array @Lof integers. Write a script to find all unique triplets such
+# that a + b + c is same as the given target T. Also make sure a <= b <= c.
+#
+# Here is wiki page for more information.
+#
+# Example:
+#
+# @L = (-25, -10, -7, -3, 2, 4, 8, 10);
+#
+# One such triplet for target 0 i.e. -10 + 2 + 8 = 0.
+
+import sys
+
+TARGET = 0
+
+def _3sum(nums):
+ for i in range(len(nums)-2):
+ outi = "+"+str(nums[i]) if nums[i] >= 0 else str(nums[i])
+ for j in range(i+1, len(nums)-1):
+ outj = "+"+str(nums[j]) if nums[j] >= 0 else str(nums[j])
+ for k in range(j+1, len(nums)):
+ outk = "+"+str(nums[k]) if nums[k] >= 0 else str(nums[k])
+ if sum([nums[i], nums[j], nums[k]]) == TARGET:
+ return outi+outj+outk+"="+str(TARGET)
+ return ""
+
+print(_3sum([int(x) for x in sys.argv[1:]]))
diff --git a/challenge-051/paulo-custodio/python/ch-2.py b/challenge-051/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..5a120fe9de
--- /dev/null
+++ b/challenge-051/paulo-custodio/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python3
+
+# Challenge 051
+#
+# TASK #2
+# Colourful Number
+# Write a script to display all Colorful Number with 3 digits.
+#
+# A number can be declare Colorful Number where all the products of consecutive
+# subsets of digit are different.
+#
+# For example, 263 is a Colorful Number since 2, 6, 3, 2x6, 6x3, 2x6x3 are
+# unique.
+
+def uniq(mylist):
+ return list(dict.fromkeys(mylist))
+
+def colorful():
+ out = []
+ for a in range (1, 10):
+ for b in range(10):
+ for c in range(10):
+ prods = [a, b, c, a*b, b*c, a*b*c]
+ if len(uniq(prods)) == 6:
+ out.append(a*100+b*10+c)
+ return out
+
+print(", ".join([str(x) for x in colorful()]))
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