aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-15 22:27:16 +0100
committerGitHub <noreply@github.com>2024-09-15 22:27:16 +0100
commit7fba8944780a2b698a8f6cebcb679560f25a69c7 (patch)
tree9fcd487e73a89bbdfb827e13fd2560a1a5f1d670
parent7a1af87ec0c9db51638d794d08e7d0f0bcbcfa71 (diff)
parent9943b6972f2db86e651cfc5cc691641d6f44bc06 (diff)
downloadperlweeklychallenge-club-7fba8944780a2b698a8f6cebcb679560f25a69c7.tar.gz
perlweeklychallenge-club-7fba8944780a2b698a8f6cebcb679560f25a69c7.tar.bz2
perlweeklychallenge-club-7fba8944780a2b698a8f6cebcb679560f25a69c7.zip
Merge pull request #10843 from ntovar/branch-286
Challenge 286. Add Perl solutions. By Nelo Tovar
-rw-r--r--challenge-286/nelo-tovar/perl/ch-1.pl38
-rw-r--r--challenge-286/nelo-tovar/perl/ch-2.pl46
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-286/nelo-tovar/perl/ch-1.pl b/challenge-286/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..f9df4eeb21
--- /dev/null
+++ b/challenge-286/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 286 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-286/
+#
+# Task 1 - Self Spammer
+#
+
+use strict;
+use warnings;
+use v5.28;
+use Data::Dump qw(dump);
+
+my $filename = $0;
+
+sub self_spammer {
+ my $filename = shift;
+ my @lines;
+
+ open my $input, '<', $filename or die "Can not open $filename. !i\n";
+
+ while (my $line = <$input>) {
+ chomp $line;
+ push @lines, $line
+ }
+
+ close $input;
+
+ my @words = split /\s+/, $lines[rand scalar @lines];
+
+ return join ', ', @words
+}
+
+my $ss = self_spammer $filename;
+
+say 'Output : ', $ss;
+say ' ';
diff --git a/challenge-286/nelo-tovar/perl/ch-2.pl b/challenge-286/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..f3242ed9e9
--- /dev/null
+++ b/challenge-286/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 286 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-286/
+#
+# Task 2 - Order Game
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::Util qw (min max);
+use List::MoreUtils qw (minmax);
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ 2, 1, 4, 5, 6, 3, 0, 2 ],
+ [ 0, 5, 3, 2 ],
+ [ 9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8 ],
+);
+
+sub order_game {
+ my @ints = shift->@*;
+
+ while ($#ints > 1) {
+ my @temp;
+
+ while ($#ints > 0) {
+ push @temp, min((shift @ints, shift @ints));
+ push @temp, max((shift @ints, shift @ints));
+ }
+
+ @ints = @temp;
+ }
+
+ return $ints[0]
+}
+
+for my $elements (@examples) {
+ my $og = order_game $elements;
+
+ say 'Input : @ints = ', dump(@$elements);
+ say 'Output : ', $og;
+ say ' ';
+}