diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-13 14:00:25 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-13 14:00:25 +0100 |
| commit | dfbfa03284219f4ceb8fa1781f199165ebc5853f (patch) | |
| tree | 05e15a10804e7fe70f9957f82db276e0495d61d4 /challenge-055 | |
| parent | 7bb9ebdb66464c211d36fd2c737ccd611328f154 (diff) | |
| download | perlweeklychallenge-club-dfbfa03284219f4ceb8fa1781f199165ebc5853f.tar.gz perlweeklychallenge-club-dfbfa03284219f4ceb8fa1781f199165ebc5853f.tar.bz2 perlweeklychallenge-club-dfbfa03284219f4ceb8fa1781f199165ebc5853f.zip | |
Add Perl solution to challenge 055
Diffstat (limited to 'challenge-055')
| -rw-r--r-- | challenge-055/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-055/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-055/paulo-custodio/perl/ch-1.pl | 58 | ||||
| -rw-r--r-- | challenge-055/paulo-custodio/perl/ch-2.pl | 57 | ||||
| -rw-r--r-- | challenge-055/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-055/paulo-custodio/t/test-2.yaml | 10 |
6 files changed, 133 insertions, 0 deletions
diff --git a/challenge-055/paulo-custodio/Makefile b/challenge-055/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-055/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-055/paulo-custodio/README b/challenge-055/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-055/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-055/paulo-custodio/perl/ch-1.pl b/challenge-055/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..56668e4e45 --- /dev/null +++ b/challenge-055/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl + +# 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 Modern::Perl; + +my $bin = shift; +my $max_1s = 0; +my @max_1s_pairs; + +for my $l (0 .. length($bin)-1) { + for my $r ($l .. length($bin)-1) { + my @test = split //, $bin; + for my $i ($l .. $r) { + $test[$i] = 1-$test[$i]; + } + my $_1s = scalar(grep {$_} @test); + if ($_1s > $max_1s) { + $max_1s = $_1s; + @max_1s_pairs = ([$l,$r]); + } + elsif ($_1s == $max_1s) { + push @max_1s_pairs, [$l,$r]; + } + } +} + +my @out; +for (@max_1s_pairs) { + my($l,$r) = @$_; + push @out, "(L=$l, R=$r)"; +} +say join(", ", @out); diff --git a/challenge-055/paulo-custodio/perl/ch-2.pl b/challenge-055/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..bf28df9b78 --- /dev/null +++ b/challenge-055/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl + +# 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 Modern::Perl; + +my @n = @ARGV; + +show_waves([], [@n]); + +sub show_waves { + my($wave, $next) = @_; + my @wave = @$wave; + my @next = @$next; + if (@next==0) { + say "@wave"; + } + elsif (@wave==0) { + for my $i (0 .. @next-1) { + show_waves([@wave, $next[$i]], + [@next[0 .. $i-1], @next[$i+1 .. $#next]]); + } + } + elsif (scalar(@wave)%2 == 1) { # going down + for my $i (0 .. @next-1) { + if ($wave[-1] >= $next[$i]) { + show_waves([@wave, $next[$i]], + [@next[0 .. $i-1], @next[$i+1 .. $#next]]); + } + } + } + else { # going up + for my $i (0 .. @next-1) { + if ($wave[-1] <= $next[$i]) { + show_waves([@wave, $next[$i]], + [@next[0 .. $i-1], @next[$i+1 .. $#next]]); + } + } + } +} diff --git a/challenge-055/paulo-custodio/t/test-1.yaml b/challenge-055/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..dca9170331 --- /dev/null +++ b/challenge-055/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 010 + input: + output: (L=0, R=0), (L=0, R=2), (L=2, R=2) diff --git a/challenge-055/paulo-custodio/t/test-2.yaml b/challenge-055/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..aedd16f29a --- /dev/null +++ b/challenge-055/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1 2 3 4 + input: + output: | + |2 1 4 3 + |3 1 4 2 + |3 2 4 1 + |4 1 3 2 + |4 2 3 1 |
