aboutsummaryrefslogtreecommitdiff
path: root/challenge-055
diff options
context:
space:
mode:
authorandrezgz <andrezgz@gmail.com>2020-04-10 00:41:36 -0300
committerandrezgz <andrezgz@gmail.com>2020-04-10 00:41:36 -0300
commit459bea7d31265a35d91f02711a94e5a0ea58c1b4 (patch)
tree513a3ab57637c71fae383b9c0a546f6916501696 /challenge-055
parent22845059c735a6dbe0f34da2b3642728d7176999 (diff)
downloadperlweeklychallenge-club-459bea7d31265a35d91f02711a94e5a0ea58c1b4.tar.gz
perlweeklychallenge-club-459bea7d31265a35d91f02711a94e5a0ea58c1b4.tar.bz2
perlweeklychallenge-club-459bea7d31265a35d91f02711a94e5a0ea58c1b4.zip
challenge-055 andrezgz solution
Diffstat (limited to 'challenge-055')
-rw-r--r--challenge-055/andrezgz/perl/ch-1.pl72
-rw-r--r--challenge-055/andrezgz/perl/ch-2.pl77
2 files changed, 149 insertions, 0 deletions
diff --git a/challenge-055/andrezgz/perl/ch-1.pl b/challenge-055/andrezgz/perl/ch-1.pl
new file mode 100644
index 0000000000..8a1c7bd1ec
--- /dev/null
+++ b/challenge-055/andrezgz/perl/ch-1.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-055/
+# Task #1
+#
+# Flip Binary
+# You are given a binary number B, consisting of N binary digits 0 or 1: s0, s1, ..., s(N-1).
+#
+# Choose two indices L and R such that 0 <= L <= R < N and flip the digits s(L), s(L+1), ..., s(R).
+# By flipping, we mean change 0 to 1 and vice-versa.
+#
+# For example, given the binary number 010, the possible flip pair results are listed below:
+#
+# L=0, R=0 the result binary: 110
+# L=0, R=1 the result binary: 100
+# L=0, R=2 the result binary: 101
+# L=1, R=1 the result binary: 000
+# L=1, R=2 the result binary: 001
+# L=2, R=2 the result binary: 011
+# Write a script to find the indices (L,R) that results in a binary number with maximum number of 1s.
+# If you find more than one maximal pair L,R then print all of them.
+#
+# Continuing our example, note that we had three pairs (L=0, R=0), (L=0, R=2),
+# and (L=2, R=2) that resulted in a binary number with two 1s, which was the maximum.
+# So we would print all three pairs.
+
+use strict;
+use warnings;
+
+die "USAGE: $0 <binary-number>" unless @ARGV == 1 && $ARGV[0] =~ /^[01]+$/;
+
+my $binary = shift;
+my $d = (length $binary) - 1;
+
+my $max = 0;
+my @solution;
+
+for my $l (0 .. $d) {
+ my @digits = split //,$binary;
+ for my $r ($l .. $d) {
+
+ $digits[$r] = $digits[$r] ? 0 : 1; #invert
+
+ my $ones = grep { $_ } @digits;
+ next if $ones < $max;
+
+ if ($ones > $max) {
+ $max = $ones;
+ @solution = { l => $l, r => $r };
+ }
+ else {
+ push @solution, { l => $l, r => $r };
+ }
+ }
+}
+
+printf '(L=%d, R=%d)'.$/, $_->{l}, $_->{r} for @solution;
+
+__END__
+
+./ch-1.pl 010
+(L=0, R=0)
+(L=0, R=2)
+(L=2, R=2)
+
+./ch-1.pl 101
+(L=1, R=1)
+
+./ch-1.pl 010
+(L=0, R=0)
+(L=0, R=2)
+(L=2, R=2)
diff --git a/challenge-055/andrezgz/perl/ch-2.pl b/challenge-055/andrezgz/perl/ch-2.pl
new file mode 100644
index 0000000000..71f7ee18d8
--- /dev/null
+++ b/challenge-055/andrezgz/perl/ch-2.pl
@@ -0,0 +1,77 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-055/
+# Task #2
+#
+# Wave Array
+# Any array N of non-unique, unsorted integers can be arranged into a wave-like array such that
+# n1 >= n2 <= n3 >= n4 <= n5 and so on.
+#
+# For example, given the array [1, 2, 3, 4], possible wave arrays include [2, 1, 4, 3] or [4, 1, 3, 2],
+# since 2 >= 1 <= 4 >= 3 and 4 >= 1 <= 3 >= 2. This is not a complete list.
+#
+# Write a script to print all possible wave arrays for an integer array N of arbitrary length.
+#
+# Notes:
+# When considering N of any length, note that the first element is always greater than or equal to the second,
+# and then the <=, >=, <=, ... sequence alternates until the end of the array.
+
+use strict;
+use warnings;
+
+my @initial = grep { /^\d+$/ } @ARGV;
+die "USAGE: $0 <n1> <n2> ...\n" if @initial != @ARGV || @initial < 2;
+
+my %waves;#hash for unique solutions when array has non-unique numbers
+populate_waves( \@initial );
+for (sort keys %waves) {
+ print '[', join( ',', @{ $waves{$_} } ) ,']' . $/;
+}
+
+sub populate_waves {
+ my $numbers = shift;
+ my $perm = shift // [];
+
+ if (!@$numbers){
+ my $key = join '-',@$perm;
+ $waves{$key} = [@$perm] if is_wave($perm);
+ return;
+ }
+
+ foreach (0 .. @$numbers-1) {
+ my $c = splice @$numbers, $_, 1;
+ push @$perm, $c;
+ populate_waves($numbers, $perm);
+ pop @$perm;
+ splice @$numbers, $_, 0, $c;
+ }
+ return;
+}
+
+sub is_wave {
+ my $p = shift;
+ for (1 .. @$p -1) {
+ if ($_ % 2 == 1) {
+ return if $p->[$_] > $p->[$_-1];
+ }
+ else {
+ return if $p->[$_] < $p->[$_-1];
+ }
+ }
+ return 1;
+}
+
+__END__
+
+./ch-2.pl 1 2 3 4
+[2,1,4,3]
+[3,1,4,2]
+[3,2,4,1]
+[4,1,3,2]
+[4,2,3,1]
+
+./ch-2.pl 1 3 3 4
+[3,1,4,3]
+[3,3,4,1]
+[4,1,3,3]
+[4,3,3,1]