aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-17 17:56:05 +0100
committerGitHub <noreply@github.com>2020-08-17 17:56:05 +0100
commit6b8d69023d5a31fe088bb589c529405b315119a0 (patch)
tree5a2b9faf212d7d0be093fe09b18b23ee9aaa40e3
parent995da1697ffec854a2794aad4c40ef73e19b6f82 (diff)
parentd4ae043c3471d42a48b0e5f680ec36824c2e6dfb (diff)
downloadperlweeklychallenge-club-6b8d69023d5a31fe088bb589c529405b315119a0.tar.gz
perlweeklychallenge-club-6b8d69023d5a31fe088bb589c529405b315119a0.tar.bz2
perlweeklychallenge-club-6b8d69023d5a31fe088bb589c529405b315119a0.zip
Merge pull request #2094 from fluca1978/pwc74
Pwc74
-rw-r--r--challenge-074/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-074/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-074/luca-ferrari/raku/ch-1.p620
-rw-r--r--challenge-074/luca-ferrari/raku/ch-2.p633
4 files changed, 55 insertions, 0 deletions
diff --git a/challenge-074/luca-ferrari/blog-1.txt b/challenge-074/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..babe89e945
--- /dev/null
+++ b/challenge-074/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/08/17/PerlWeeklyChallenge74.html#task1
diff --git a/challenge-074/luca-ferrari/blog-2.txt b/challenge-074/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..d1a50f274b
--- /dev/null
+++ b/challenge-074/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/08/17/PerlWeeklyChallenge74.html#task2
diff --git a/challenge-074/luca-ferrari/raku/ch-1.p6 b/challenge-074/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..64bf58939d
--- /dev/null
+++ b/challenge-074/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,20 @@
+#!raku
+
+# % raku ch-1.p6 1 2 2 3 2 4 2
+# 2
+#
+# % raku ch-1.p6 1 3 1 2 4 5
+# -1
+
+sub MAIN( *@array where { @array.grep( * ~~ Int ).elems == @array.elems } ) {
+ my $N = @array.elems;
+ my $majority = floor( $N / 2 );
+
+ my %counting;
+ %counting{ $_ }++ for @array;
+
+ given %counting.pairs.map( { .value >= $majority ?? $_ !! Nil } ).grep( * ~~ Pair ).unique.head {
+ when .so { .key.say; }
+ default { '-1'.say; }
+ }
+}
diff --git a/challenge-074/luca-ferrari/raku/ch-2.p6 b/challenge-074/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..e8ea2d376f
--- /dev/null
+++ b/challenge-074/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,33 @@
+#!raku
+
+sub MAIN( Str $S where { $S.chars > 2 } ) {
+
+ my @result;
+ my %counting;
+ my @chars = $S.comb( '', :skip-empty );
+
+ for 0 ..^ @chars.elems -> $index {
+ my $current-char = @chars[ $index ];
+
+
+
+ # if the result array is empty
+ # or the char is not in the array, it is
+ # ok to push
+ @result.push( $current-char ) && next if ! @result || ! @result.grep( * ~~ $current-char );
+
+
+ # if here I need to search for the first rightmost
+ # not repeating char so far
+ %counting = %();
+ %counting{ $_ }++ for $S.substr( 0 .. $index ).comb( '', :skip-empty );
+ my $fnr = $S.substr( 0 .. $index )
+ .comb( '', :skip-empty )
+ .first( { %counting{ $_ }:exists && %counting{ $_ } == 1 }, :end )
+ // '#';
+
+ @result.push: $fnr;
+ }
+
+ @result.join.say;
+}