From b43d6f4bb09201b8497497b9f848d6c829876916 Mon Sep 17 00:00:00 2001 From: wanderdoc Date: Sat, 22 Aug 2020 15:44:45 +0200 Subject: Solutions to challenge-074. --- challenge-074/wanderdoc/perl/ch-1.pl | 33 +++++++++++++++++++++++ challenge-074/wanderdoc/perl/ch-2.pl | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 challenge-074/wanderdoc/perl/ch-1.pl create mode 100644 challenge-074/wanderdoc/perl/ch-2.pl diff --git a/challenge-074/wanderdoc/perl/ch-1.pl b/challenge-074/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..470da83fe1 --- /dev/null +++ b/challenge-074/wanderdoc/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given an array of integers of size $N. Write a script to find the majority element. If none found then print -1. +Majority element in the list is the one that appears more than floor(size_of_list/2). + +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). +Example 2: Input: @A = (1, 3, 1, 2, 4, 5) Output: -1 as none of the elements appears more than floor(6/2). +=cut + + + + + +use List::Util qw(first); +use Test::More; + + +sub majority +{ + my @array = @_; + my $level = int((scalar @array)/2); + my %count; + + $count{$_}++ for @array; + return (first { $count{$_} > $level } keys %count) // -1; +} + +is(majority(1, 2, 2, 3, 2, 4, 2), 2, 'Example 1'); +is(majority(1, 3, 1, 2, 4, 5), -1, 'Example 2'); +done_testing(); \ No newline at end of file diff --git a/challenge-074/wanderdoc/perl/ch-2.pl b/challenge-074/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..45cb2901cd --- /dev/null +++ b/challenge-074/wanderdoc/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string $S. Write a script to print the series of first non-repeating character for the given string. Print # if none found. +Example 1: Input: $S = 'ababc' Output: 'abb#c' +Pass 1: "a", the FNR character is 'a' +Pass 2: "ab", the FNR character is 'b' +Pass 3: "aba", the FNR character is 'b' +Pass 4: "abab", no FNR found, hence '#' +Pass 5: "ababc" the FNR character is 'c' + +Example 2: Input: $S = 'xyzzyx' Output: 'xyzyx#' +Pass 1: "x", the FNR character is "x" +Pass 2: "xy", the FNR character is "y" +Pass 3: "xyz", the FNR character is "z" +Pass 4: "xyzz", the FNR character is "y" +Pass 5: "xyzzy", the FNR character is "x" +Pass 6: "xyzzyx", no FNR found, hence '#' +=cut + + + +use Test::More; + + +sub fnr +{ + my $string = $_[0]; + my @letters = split(//,$string); + my $output; + + for my $i ( 0 .. $#letters ) + { + my @pass = @letters[0 .. $i]; + my %count; + $count{$_}++ for @pass; + + + my $fnr = (grep {1 == $count{$_}} @pass)[-1] || '#'; + $output .= $fnr + } + + return $output; +} + + + +is(fnr('ababc'), 'abb#c', 'Example 1'); +is(fnr('xyzzyx'), 'xyzyx#', 'Example 2'); +done_testing(); \ No newline at end of file -- cgit