aboutsummaryrefslogtreecommitdiff
path: root/challenge-078/bob-lied
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2020-09-21 14:20:42 +0800
committer冯昶 <seaker@qq.com>2020-09-21 14:20:42 +0800
commitbca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb (patch)
tree877181cfde26b706346d3468269e4674d75da772 /challenge-078/bob-lied
parentec09b571a6f2186fec8870a071a8d5d38596c850 (diff)
parent5ac16ac7e9826137e0da5597e954f4992c66205d (diff)
downloadperlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.tar.gz
perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.tar.bz2
perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-078/bob-lied')
-rw-r--r--challenge-078/bob-lied/README4
-rwxr-xr-xchallenge-078/bob-lied/perl/ch-1.pl42
-rwxr-xr-xchallenge-078/bob-lied/perl/ch-2.pl67
3 files changed, 111 insertions, 2 deletions
diff --git a/challenge-078/bob-lied/README b/challenge-078/bob-lied/README
index 9deeb88045..ac3ae94c19 100644
--- a/challenge-078/bob-lied/README
+++ b/challenge-078/bob-lied/README
@@ -1,3 +1,3 @@
-Solutions to weekly challenge 77 by Bob Lied.
+Solutions to weekly challenge 78 by Bob Lied.
-https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-078/
diff --git a/challenge-078/bob-lied/perl/ch-1.pl b/challenge-078/bob-lied/perl/ch-1.pl
new file mode 100755
index 0000000000..4ccee7959b
--- /dev/null
+++ b/challenge-078/bob-lied/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-1.pl
+#=============================================================================
+# Copyright (c) 2020, Bob Lied
+#=============================================================================
+# Perl Weekly Challenge 078 Task #1 > Leader Element
+#=============================================================================
+# You are given an array @A containing distinct integers.
+# Write a script to find all leader elements in the array @A. Print (0) if
+# none found.
+# An element is a leader if it is greater than all the elements to its right.
+# Example 1:
+# Input: @A = (9, 10, 7, 5, 6, 1)
+# Ouput: (10, 7, 6, 1)
+
+use strict;
+use warnings;
+use v5.30;
+
+use feature qw/ signatures /;
+no warnings qw/ experimental::signatures /;
+
+use List::Util qw / all /;
+
+sub Usage { "Usage: $0 args" };
+
+my @list = @ARGV;
+
+die Usage() unless @list;
+
+my @answer;
+
+while ( my $leader = shift(@list) )
+{
+ if ( all { $leader > $_ } @list )
+ {
+ push @answer, $leader;
+ }
+}
+say "( @answer )";
diff --git a/challenge-078/bob-lied/perl/ch-2.pl b/challenge-078/bob-lied/perl/ch-2.pl
new file mode 100755
index 0000000000..f3b49a6dcb
--- /dev/null
+++ b/challenge-078/bob-lied/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-2.pl
+#=============================================================================
+# Copyright (c) 2020, Bob Lied
+#=============================================================================
+# Perl Weekly Challenge 078 Task #2 > Left Rotation
+#=============================================================================
+# You are given an array @A containing positive numbers and @B containing one
+# or more indices from @A.
+# Write a script to left-rotate @A so that the number at the first index of @B
+# becomes the first element in the array. Similary, left rotate @A again so
+# that the number at the second index of @B becomes the first element in the
+# array.
+# Example 1:
+# Input: @A = (10 20 30 40 50)
+# @B = (3 4)
+# a) We left rotate the 3rd index element (40) in the @A to make it 0th index
+# member in the array.
+# [40 50 10 20 30]
+#
+# b) We left rotate the 4th index element (50) in the @A to make it 0th index
+# member in the array.
+# [50 10 20 30 40]
+#
+# Output:
+# [40 50 10 20 30]
+# [50 10 20 30 40]
+
+use strict;
+use warnings;
+use v5.30;
+
+use feature qw/ signatures /;
+no warnings qw/ experimental::signatures /;
+
+use Getopt::Long;
+
+sub Usage { qq(Usage: $0 "(a b c d e)" "(x y)" ) };
+
+my $Verbose = 0;
+GetOptions('verbose' => \$Verbose);
+
+my $A = shift; # In list form, "10,20,30"
+my $B = shift; # In list form, "3,4";
+
+die Usage() unless $A && $B;
+
+$A =~ s/[(),]/ /g;
+my @A = split(" ", $A);
+
+$B =~ s/[(),]/ /g;
+my @B = split(" ", $B);
+
+if ( $Verbose )
+{
+ say "\@A = (@A)\n\@B = (@B)";
+}
+
+my $lastA = $#A;
+
+for my $pivot ( @B )
+{
+ my @rot = ( @A[ $pivot .. $lastA], @A[0..$pivot-1] );
+ say "[ @rot ]";
+}