aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-074/athanasius/perl/ch-1.pl82
-rw-r--r--challenge-074/athanasius/perl/ch-2.pl105
-rw-r--r--challenge-074/athanasius/raku/ch-1.raku79
-rw-r--r--challenge-074/athanasius/raku/ch-2.raku104
4 files changed, 370 insertions, 0 deletions
diff --git a/challenge-074/athanasius/perl/ch-1.pl b/challenge-074/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..82913c51f9
--- /dev/null
+++ b/challenge-074/athanasius/perl/ch-1.pl
@@ -0,0 +1,82 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 074
+=========================
+
+Task #1
+-------
+*Majority Element*
+
+Submitted by: Mohammad S Anwar
+
+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
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2020 PerlMonk Athanasius #
+#--------------------------------------#
+
+use strict;
+use warnings;
+use Const::Fast; # Exports const()
+use List::Util qw( max );
+use POSIX qw( floor );
+use Regexp::Common qw( number ); # Exports %RE{num}
+
+const my $USAGE =>
+"Usage:
+ perl $0 [<A> ...]
+
+ [<A> ...] Non-empty integer sequence\n";
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 074, Task #1: Majority Element (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ my @A = @ARGV;
+ (my $N = scalar @A) > 0 or die $USAGE;
+ /\A$RE{num}{int}\z/ or die $USAGE for @A;
+
+ my $mean_fl = floor($N / 2);
+ my $output = -1;
+ my %count;
+ ++$count{$_} for @A;
+
+ if ((my $max = max values %count) > $mean_fl)
+ {
+ my %rev = reverse %count;
+ $output = $rev{$max};
+ }
+
+ printf "Input: \@A = (%s)\nOutput: %d\n", join(', ', @A), $output;
+}
+
+################################################################################
diff --git a/challenge-074/athanasius/perl/ch-2.pl b/challenge-074/athanasius/perl/ch-2.pl
new file mode 100644
index 0000000000..e0bd48338e
--- /dev/null
+++ b/challenge-074/athanasius/perl/ch-2.pl
@@ -0,0 +1,105 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 074
+=========================
+
+Task #2
+-------
+*FNR Character*
+
+Submitted by: Mohammad S Anwar
+
+You are given a string _$S_.
+
+Write a script to print the series of first non-repeating character (left ->
+right) 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
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2020 PerlMonk Athanasius #
+#--------------------------------------#
+
+use strict;
+use warnings;
+use Const::Fast; # Exports const()
+
+const my $USAGE =>
+"Usage:
+ perl $0 <S>
+
+ <S> A non-empty string\n";
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 074, Task #2: FNR Character (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ scalar @ARGV == 1 or die $USAGE;
+
+ my $S = $ARGV[0];
+ my @chars = split //, $S;
+ my $FNR = '';
+ my %count;
+
+ for my $i (0 .. $#chars)
+ {
+ my $fnr = '#';
+
+ if (++$count{ my $chr = $chars[$i] } == 1)
+ {
+ $fnr = $chr;
+ }
+ else
+ {
+ INNER: for my $j (reverse 0 .. $i - 1)
+ {
+ if ($count{ $chr = $chars[$j] } == 1)
+ {
+ $fnr = $chr;
+ last INNER;
+ }
+ }
+ }
+
+ $FNR .= $fnr;
+ }
+
+ print "Input: \$S = '$S'\nOutput: '$FNR'\n";
+}
+
+################################################################################
diff --git a/challenge-074/athanasius/raku/ch-1.raku b/challenge-074/athanasius/raku/ch-1.raku
new file mode 100644
index 0000000000..68ce495930
--- /dev/null
+++ b/challenge-074/athanasius/raku/ch-1.raku
@@ -0,0 +1,79 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 074
+=========================
+
+Task #1
+-------
+*Majority Element*
+
+Submitted by: Mohammad S Anwar
+
+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).
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2020 PerlMonk Athanasius #
+#--------------------------------------#
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 074, Task #1: Majority Element (Raku)\n".put;
+}
+
+#===============================================================================
+sub MAIN
+(
+ *@A where { @A.elems > 0 && @A.all ~~ Int:D } #= Non-empty integer sequence
+)
+#===============================================================================
+{
+ my UInt $N = @A.elems;
+ my UInt $mean-fl = floor($N / 2);
+ my Int $output = -1;
+ my UInt %count;
+
+ ++%count{$_} for @A;
+
+ if (my UInt $max = %count.values.max) > $mean-fl
+ {
+ my %rev = %count.invert;
+ $output = %rev{$max}.Int;
+ }
+
+ "Input: \@A = (%s)\nOutput: %d\n".printf: @A.join(', '), $output;
+}
+
+#-------------------------------------------------------------------------------
+sub USAGE()
+#-------------------------------------------------------------------------------
+{
+ my Str $usage = $*USAGE;
+
+ $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/;
+ $usage.put;
+}
+
+################################################################################
diff --git a/challenge-074/athanasius/raku/ch-2.raku b/challenge-074/athanasius/raku/ch-2.raku
new file mode 100644
index 0000000000..ad391204f2
--- /dev/null
+++ b/challenge-074/athanasius/raku/ch-2.raku
@@ -0,0 +1,104 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 074
+=========================
+
+Task #2
+-------
+*FNR Character*
+
+Submitted by: Mohammad S Anwar
+
+You are given a string _$S_.
+
+Write a script to print the series of first non-repeating character (left ->
+right) 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 '#'
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2020 PerlMonk Athanasius #
+#--------------------------------------#
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 074, Task #2: FNR Character (Raku)\n".put;
+}
+
+##==============================================================================
+sub MAIN
+(
+ Str:D $S where { $S.chars > 0 } #= A non-empty string
+)
+##==============================================================================
+{
+ my Str @chars = $S.split: '', :skip-empty;
+ my Str $FNR = '';
+ my UInt %count;
+
+ for 0 .. @chars.end -> UInt $i
+ {
+ my Str $fnr = '#';
+
+ if ++%count{ my Str $chr = @chars[$i] } == 1
+ {
+ $fnr = $chr;
+ }
+ else
+ {
+ INNER: for (0 .. $i - 1).reverse -> UInt $j
+ {
+ if %count{ $chr = @chars[$j] } == 1
+ {
+ $fnr = $chr;
+ last INNER;
+ }
+ }
+ }
+
+ $FNR ~= $fnr;
+ }
+
+ "Input: \$S = '$S'\nOutput: '$FNR'".put;
+}
+
+#-------------------------------------------------------------------------------
+sub USAGE()
+#-------------------------------------------------------------------------------
+{
+ my Str $usage = $*USAGE;
+
+ $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/;
+ $usage.put;
+}
+
+################################################################################