aboutsummaryrefslogtreecommitdiff
path: root/challenge-182
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-09-19 03:51:25 +0100
committerGitHub <noreply@github.com>2022-09-19 03:51:25 +0100
commita552bd481d217b371bcbe7411a3b5236332a7274 (patch)
treede93d873d01243d752f7d4f6daa8b870257d3074 /challenge-182
parentdda42dd6770ab47c022bf0a838b66a998436bbc3 (diff)
parent2b76372a73bebab52be6cb9a3ae8edddec15225c (diff)
downloadperlweeklychallenge-club-a552bd481d217b371bcbe7411a3b5236332a7274.tar.gz
perlweeklychallenge-club-a552bd481d217b371bcbe7411a3b5236332a7274.tar.bz2
perlweeklychallenge-club-a552bd481d217b371bcbe7411a3b5236332a7274.zip
Merge pull request #6767 from adamcrussell/challenge-182
initial commit
Diffstat (limited to 'challenge-182')
-rw-r--r--challenge-182/adam-russell/blog.txt1
-rw-r--r--challenge-182/adam-russell/perl/ch-1.pl20
-rw-r--r--challenge-182/adam-russell/perl/ch-2.pl37
3 files changed, 58 insertions, 0 deletions
diff --git a/challenge-182/adam-russell/blog.txt b/challenge-182/adam-russell/blog.txt
new file mode 100644
index 0000000000..e350d9f424
--- /dev/null
+++ b/challenge-182/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/09/18
diff --git a/challenge-182/adam-russell/perl/ch-1.pl b/challenge-182/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..08c1857330
--- /dev/null
+++ b/challenge-182/adam-russell/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use v5.36;
+use strict;
+use warnings;
+##
+# You are given a list of integers.
+# Write a script to find the index of the first biggest number in the list.
+##
+sub index_biggest{
+ my(@numbers) = @_;
+ my @sorted = sort {$b <=> $a} @numbers;
+ map { return $_ if $numbers[$_] == $sorted[0] } 0 .. @numbers - 1;
+}
+
+MAIN:{
+ my @n;
+ @n = (5, 2, 9, 1, 7, 6);
+ print index_biggest(@n) . "\n";
+ @n = (4, 2, 3, 1, 5, 0);
+ print index_biggest(@n) . "\n";
+}
diff --git a/challenge-182/adam-russell/perl/ch-2.pl b/challenge-182/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..6901252e01
--- /dev/null
+++ b/challenge-182/adam-russell/perl/ch-2.pl
@@ -0,0 +1,37 @@
+use v5.36;
+use strict;
+use warnings;
+##
+# Given a list of absolute Linux file paths, determine the
+# deepest path to the directory that contains all of them.
+##
+sub deepest_path{
+ my(@paths) = @_;
+ my @sub_paths = map { [split(/\//, $_)] } @paths;
+ my @path_lengths_sorted = sort { $a <=> $b } map { 0 + @{$_} } @sub_paths;
+ my $deepest_path = q//;
+ for my $i (0 .. $path_lengths_sorted[0] - 1){
+ my @column = map { $_->[$i] } @sub_paths;
+ my %h;
+ map { $h{$_} = undef } @column;
+ $deepest_path .= (keys %h)[0] . q#/# if 1 == keys %h;
+ }
+ chop $deepest_path;
+ return $deepest_path;
+}
+
+MAIN:{
+ my $data = do{
+ local $/;
+ <DATA>;
+ };
+ my @paths = split(/\n/, $data);
+ print deepest_path(@paths) . "\n";
+}
+
+__DATA__
+/a/b/c/1/x.pl
+/a/b/c/d/e/2/x.pl
+/a/b/c/d/3/x.pl
+/a/b/c/4/x.pl
+/a/b/c/d/5/x.pl