aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-12 16:42:14 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-12 16:42:14 +0100
commit46d572a502e02c2f16bf2bca4b198b57a38d2e8f (patch)
tree90f08e59ea7117ba9a67f15263edfa4caab75c65
parent2f4f8ac7670d802ff5a5bb76d0d3294725c59603 (diff)
downloadperlweeklychallenge-club-46d572a502e02c2f16bf2bca4b198b57a38d2e8f.tar.gz
perlweeklychallenge-club-46d572a502e02c2f16bf2bca4b198b57a38d2e8f.tar.bz2
perlweeklychallenge-club-46d572a502e02c2f16bf2bca4b198b57a38d2e8f.zip
Add Perl solution to challenge 051
-rw-r--r--challenge-051/paulo-custodio/Makefile2
-rw-r--r--challenge-051/paulo-custodio/README1
-rw-r--r--challenge-051/paulo-custodio/perl/ch-1.pl39
-rw-r--r--challenge-051/paulo-custodio/perl/ch-2.pl29
-rw-r--r--challenge-051/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-051/paulo-custodio/t/test-2.yaml5
6 files changed, 81 insertions, 0 deletions
diff --git a/challenge-051/paulo-custodio/Makefile b/challenge-051/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-051/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-051/paulo-custodio/README b/challenge-051/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-051/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-051/paulo-custodio/perl/ch-1.pl b/challenge-051/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..3dadaaf6de
--- /dev/null
+++ b/challenge-051/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+
+# 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.
+
+use Modern::Perl;
+use List::Util qw( sum );
+
+my $target = 0;
+say _3sum(@ARGV);
+
+sub _3sum {
+ my(@n) = @_;
+ for my $i (0 .. $#n-2) {
+ my $outi = $n[$i]<0 ? $n[$i] : "+".$n[$i];
+ for my $j ($i+1 .. $#n-1) {
+ my $outj = $n[$j]<0 ? $n[$j] : "+".$n[$j];
+ for my $k ($j+1 .. $#n) {
+ my $outk = $n[$k]<0 ? $n[$k] : "+".$n[$k];
+ if (sum(@n[$i,$j,$k])==$target) {
+ return "$outi$outj$outk=$target";
+ }
+ }
+ }
+ }
+ return "";
+}
diff --git a/challenge-051/paulo-custodio/perl/ch-2.pl b/challenge-051/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..571be93588
--- /dev/null
+++ b/challenge-051/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+
+# 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.
+
+use Modern::Perl;
+use List::Util qw( uniq );
+
+my @out;
+for my $a (1..9) {
+ for my $b (0..9) {
+ for my $c (0..9) {
+ my @prods = ($a, $b, $c, $a*$b, $b*$c, $a*$b*$c);
+ if (scalar(uniq(@prods))==6) {
+ push @out, "$a$b$c";
+ }
+ }
+ }
+}
+say join(", ", @out);
diff --git a/challenge-051/paulo-custodio/t/test-1.yaml b/challenge-051/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..80934bd6b9
--- /dev/null
+++ b/challenge-051/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: -25 -10 -7 -3 2 4 8 10
+ input:
+ output: -10+2+8=0
diff --git a/challenge-051/paulo-custodio/t/test-2.yaml b/challenge-051/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..7a580f2e5e
--- /dev/null
+++ b/challenge-051/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 234, 235, 237, 238, 239, 243, 245, 246, 247, 249, 253, 254, 256, 257, 258, 259, 263, 264, 265, 267, 268, 269, 273, 274, 275, 276, 278, 279, 283, 284, 285, 286, 287, 289, 293, 294, 295, 296, 297, 298, 324, 325, 327, 328, 329, 342, 345, 346, 347, 348, 349, 352, 354, 356, 357, 358, 359, 362, 364, 365, 367, 368, 369, 372, 374, 375, 376, 378, 379, 382, 384, 385, 386, 387, 389, 392, 394, 395, 396, 397, 398, 423, 425, 426, 427, 429, 432, 435, 436, 437, 438, 439, 452, 453, 456, 457, 458, 459, 462, 463, 465, 467, 468, 469, 472, 473, 475, 476, 478, 479, 482, 483, 485, 486, 487, 489, 492, 493, 495, 496, 497, 498, 523, 524, 526, 527, 528, 529, 532, 534, 536, 537, 538, 539, 542, 543, 546, 547, 548, 549, 562, 563, 564, 567, 568, 569, 572, 573, 574, 576, 578, 579, 582, 583, 584, 586, 587, 589, 592, 593, 594, 596, 597, 598, 624, 625, 627, 628, 629, 634, 635, 637, 638, 639, 642, 643, 645, 647, 648, 649, 652, 653, 654, 657, 658, 659, 672, 673, 674, 675, 678, 679, 682, 683, 684, 685, 687, 689, 692, 693, 694, 695, 697, 698, 723, 724, 725, 726, 728, 729, 732, 734, 735, 736, 738, 739, 742, 743, 745, 746, 748, 749, 752, 753, 754, 756, 758, 759, 762, 763, 764, 765, 768, 769, 782, 783, 784, 785, 786, 789, 792, 793, 794, 795, 796, 798, 823, 825, 826, 827, 829, 832, 834, 835, 836, 837, 839, 843, 845, 846, 847, 849, 852, 853, 854, 856, 857, 859, 862, 863, 864, 865, 867, 869, 872, 873, 874, 875, 876, 879, 892, 893, 894, 895, 896, 897, 923, 924, 925, 926, 927, 928, 932, 934, 935, 936, 937, 938, 942, 943, 945, 946, 947, 948, 952, 953, 954, 956, 957, 958, 962, 963, 964, 965, 967, 968, 972, 973, 974, 975, 976, 978, 982, 983, 984, 985, 986, 987