aboutsummaryrefslogtreecommitdiff
path: root/challenge-294
diff options
context:
space:
mode:
authorDavid Ferrone <zapwai@gmail.com>2024-11-07 10:23:17 -0500
committerGitHub <noreply@github.com>2024-11-07 10:23:17 -0500
commitaa63f61f11ef65d05dd457a0f33b3118fe816761 (patch)
tree45ed912393bf25a24ea91d4b4cc2e809a85050e5 /challenge-294
parent2b0af08a6cbeeeee3595507b37913e3f9be35dd1 (diff)
downloadperlweeklychallenge-club-aa63f61f11ef65d05dd457a0f33b3118fe816761.tar.gz
perlweeklychallenge-club-aa63f61f11ef65d05dd457a0f33b3118fe816761.tar.bz2
perlweeklychallenge-club-aa63f61f11ef65d05dd457a0f33b3118fe816761.zip
Week 294
Diffstat (limited to 'challenge-294')
-rw-r--r--challenge-294/zapwai/perl/ch-1.pl37
-rw-r--r--challenge-294/zapwai/perl/ch-2.pl40
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-294/zapwai/perl/ch-1.pl b/challenge-294/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..c115a702b8
--- /dev/null
+++ b/challenge-294/zapwai/perl/ch-1.pl
@@ -0,0 +1,37 @@
+use v5.38;
+use List::Util qw( uniq );
+
+sub no_seq(@ints) {
+ for my $i (0 .. $#ints - 1) {
+ for my $j ($i + 1 .. $#ints) {
+ return 0 if (abs($ints[$i] - $ints[$j]) == 1);
+ }
+ }
+ return 1;
+}
+
+sub proc(@ints) {
+ say "Input: \@ints = @ints";
+ @ints = uniq(@ints);
+ return -1 if (no_seq(@ints) == 1);
+ my $len = 1;
+ my @chain;
+ for my $i (0 .. $#ints) {
+ for my $j (0 .. $#ints) {
+ next if ($i == $j);
+ if (abs($ints[$i] - $ints[$j]) == 1) {
+ push @chain, $ints[$i];
+ last;
+ }
+ }
+ }
+ @chain = sort @chain;
+ return scalar @chain;
+}
+
+my @ints = (10, 4, 20, 1, 3, 2);
+say "Output: ", proc(@ints);
+@ints = (0, 6, 1, 8, 5, 2, 4, 3, 0, 7);
+say "Output: ", proc(@ints);
+@ints = (10,30,20);
+say "Output: ", proc(@ints);
diff --git a/challenge-294/zapwai/perl/ch-2.pl b/challenge-294/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..93c3c0fd3d
--- /dev/null
+++ b/challenge-294/zapwai/perl/ch-2.pl
@@ -0,0 +1,40 @@
+use v5.38;
+
+sub perm(@l) {
+ return (\@l) if ($#l == 0);
+ my @L;
+ for my $i (0 .. $#l) {
+ my @new = @l;
+ splice @new, $i, 1;
+ for my $r (perm(@new)) {
+ my @curr_list = ($l[$i]);
+ push @curr_list, @$r;
+ push @L, \@curr_list;
+ }
+ }
+ return @L;
+}
+
+sub proc(@ints) {
+ say "Input: \@ints = @ints";
+ my @p = perm(sort @ints);
+ my $out;
+ my $flag = 0;
+ my $s1 = join "", @ints;
+ for my $r (@p) {
+ if ($flag == 1) {
+ $out = join "", @$r;
+ $flag = 0;
+ }
+ my $s2 = join "", @$r;
+ $flag = 1 if ($s1 eq $s2);
+ }
+ say "Output: $out";
+}
+
+my @ints = (1,2,3);
+proc(@ints);
+@ints = (2,1,3);
+proc(@ints);
+@ints = (3,1,2);
+proc(@ints);