aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-23 09:14:21 +0100
committerGitHub <noreply@github.com>2020-08-23 09:14:21 +0100
commite9c3159b46ae6002301c74903d621348104d4b09 (patch)
tree4b10b2c25658b86baf3594e8e630ad7b8cbe8af6
parented3b7b57825b46ece27745e1c07c129daff01d20 (diff)
parent09f81d31bdefdb08317b3e2fc5fd615ee5b45af4 (diff)
downloadperlweeklychallenge-club-e9c3159b46ae6002301c74903d621348104d4b09.tar.gz
perlweeklychallenge-club-e9c3159b46ae6002301c74903d621348104d4b09.tar.bz2
perlweeklychallenge-club-e9c3159b46ae6002301c74903d621348104d4b09.zip
Merge pull request #2120 from lancew/branch--1
Challenge-074 tasks 1 and 2
-rw-r--r--challenge-074/lance-wicks/perl/ch-1.pl15
-rw-r--r--challenge-074/lance-wicks/perl/ch-2.pl14
-rw-r--r--challenge-074/lance-wicks/perl/lib/FNR.pm36
-rw-r--r--challenge-074/lance-wicks/perl/lib/Majority.pm27
-rw-r--r--challenge-074/lance-wicks/perl/t/FNR.t35
-rw-r--r--challenge-074/lance-wicks/perl/t/Majority-basic.t22
6 files changed, 149 insertions, 0 deletions
diff --git a/challenge-074/lance-wicks/perl/ch-1.pl b/challenge-074/lance-wicks/perl/ch-1.pl
new file mode 100644
index 0000000000..989912da5e
--- /dev/null
+++ b/challenge-074/lance-wicks/perl/ch-1.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use lib './lib';
+use Majority;
+
+my @A = @ARGV;
+
+my $maj = Majority::element(@A);
+
+print $maj;
+
+
diff --git a/challenge-074/lance-wicks/perl/ch-2.pl b/challenge-074/lance-wicks/perl/ch-2.pl
new file mode 100644
index 0000000000..8b1fac125b
--- /dev/null
+++ b/challenge-074/lance-wicks/perl/ch-2.pl
@@ -0,0 +1,14 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use lib './lib';
+use FNR;
+
+my @A = @ARGV;
+my $fnr = FNR->new();
+
+my $output = $fnr->from_string($A[0]);
+
+print $output;
diff --git a/challenge-074/lance-wicks/perl/lib/FNR.pm b/challenge-074/lance-wicks/perl/lib/FNR.pm
new file mode 100644
index 0000000000..d76b60b3f7
--- /dev/null
+++ b/challenge-074/lance-wicks/perl/lib/FNR.pm
@@ -0,0 +1,36 @@
+package FNR;
+
+use strict;
+use warnings;
+
+use List::MoreUtils 'frequency';
+use Moo;
+
+sub from_string {
+ my ( $self, $string ) = @_;
+ my $sequence = '';
+
+ my $end = length $string;
+
+ for my $offset ( 1 .. $end ) {
+ my $chars = substr $string, 0, $offset;
+ $sequence .= $self->_fnr($chars);
+ }
+
+ return $sequence;
+}
+
+sub _fnr {
+ my ( $self, $chars ) = @_;
+ my @characters = split '', $chars;
+ my %frequencies = frequency( @characters );
+
+ for my $key ( reverse @characters ) {
+ return $key if $frequencies{$key} == 1;
+ }
+
+ return '#';
+}
+
+1;
+
diff --git a/challenge-074/lance-wicks/perl/lib/Majority.pm b/challenge-074/lance-wicks/perl/lib/Majority.pm
new file mode 100644
index 0000000000..a2cca93bda
--- /dev/null
+++ b/challenge-074/lance-wicks/perl/lib/Majority.pm
@@ -0,0 +1,27 @@
+package Majority;
+
+use strict;
+use warnings;
+
+use List::MoreUtils 'frequency';
+
+sub element {
+# Majority element in the list is the one that appears more than floor(size_of_list/2).
+ my ( $self, @A ) = @_;
+ my $majority_element = -1;
+
+ my $floor = @A / 2;
+
+ my %frequencies = frequency @A;
+
+ for my $element ( @A ) {
+ if ( $frequencies{$element} > $floor ) {
+ $majority_element = $element;
+ last;
+ }
+ }
+
+ return $majority_element;
+}
+
+1;
diff --git a/challenge-074/lance-wicks/perl/t/FNR.t b/challenge-074/lance-wicks/perl/t/FNR.t
new file mode 100644
index 0000000000..8bb52b5133
--- /dev/null
+++ b/challenge-074/lance-wicks/perl/t/FNR.t
@@ -0,0 +1,35 @@
+use Test2::V0 -target => 'FNR';
+
+subtest 'from_string()' => sub {
+ subtest 'Example 1' => sub {
+ note "Input: ‘ababc’";
+ note "Output: ‘abb#c’";
+ is $CLASS->from_string('ababc'), 'abb#c',
+ 'The first example is correct';
+ };
+
+ subtest 'Example 2' => sub {
+ note "Input: ‘xyzzyx’";
+ note "Output: ‘xyzyx#’";
+
+ is $CLASS->from_string('xyzzyx'), 'xyzyx#',
+ 'The second example is correct';
+
+ };
+};
+
+subtest '_fnr()' => sub {
+ my %tests = (
+ 'a' => 'a',
+ 'ab' => 'b',
+ 'aba' => 'b',
+ 'abab' => '#',
+ 'ababc' => 'c'
+ );
+
+ while ( my ( $input, $expected ) = each %tests ) {
+ is $CLASS->_fnr($input), $expected, "$input should return $expected";
+ }
+};
+
+done_testing;
diff --git a/challenge-074/lance-wicks/perl/t/Majority-basic.t b/challenge-074/lance-wicks/perl/t/Majority-basic.t
new file mode 100644
index 0000000000..966ea37a7f
--- /dev/null
+++ b/challenge-074/lance-wicks/perl/t/Majority-basic.t
@@ -0,0 +1,22 @@
+use Test2::V0 -target => 'Majority';
+
+ok 1;
+#Example 1
+#Input: @A = (1, 2, 2, 3, 2, 4, 2)
+#Output: 2, as 2 appears 4 times in the list which is more than floor(7/2).
+subtest 'Example 1' => sub {
+ my @A = ( 1, 2, 2, 3, 2, 4, 2 );
+ is $CLASS->element(@A), 2;
+
+};
+
+#Example 2
+#Input: @A = (1, 3, 1, 2, 4, 5)
+#Output: -1 as none of the elements appears more than floor(6/2).
+subtest 'Example 2' => sub {
+ my @A = ( 1, 3, 1, 2, 4, 5 );
+ is $CLASS->element(@A), -1;
+
+};
+
+done_testing;