aboutsummaryrefslogtreecommitdiff
path: root/challenge-074
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-074')
-rw-r--r--challenge-074/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-074/arne-sommer/raku/ch-1.raku17
-rwxr-xr-xchallenge-074/arne-sommer/raku/ch-2.raku39
-rwxr-xr-xchallenge-074/arne-sommer/raku/fnr-character39
-rwxr-xr-xchallenge-074/arne-sommer/raku/fnr-character-wrongish39
-rwxr-xr-xchallenge-074/arne-sommer/raku/majority-element17
-rwxr-xr-xchallenge-074/arne-sommer/raku/majority-element-wrongish15
-rw-r--r--challenge-074/ash/cpp/ch-1.cpp (renamed from challenge-074/ash/raku/ch-1.cpp)0
-rw-r--r--challenge-074/brtastic/blog.txt1
-rw-r--r--challenge-074/brtastic/perl/ch-1.pl28
-rw-r--r--challenge-074/brtastic/perl/ch-2.pl43
-rw-r--r--challenge-074/cheok-yin-fung/blog.txt1
-rw-r--r--challenge-074/cheok-yin-fung/perl/ch-1.pl49
-rw-r--r--challenge-074/cheok-yin-fung/perl/ch-2.pl48
-rw-r--r--challenge-074/colin-crain/blog.txt1
-rw-r--r--challenge-074/colin-crain/perl/ch-1.pl55
-rw-r--r--challenge-074/colin-crain/perl/ch-2.pl102
-rw-r--r--challenge-074/colin-crain/raku/ch-1.raku40
-rw-r--r--challenge-074/colin-crain/raku/ch-2.raku86
-rwxr-xr-xchallenge-074/cristian-heredia/perl/ch-1.pl48
-rw-r--r--challenge-074/jason-messer/README2
-rw-r--r--challenge-074/jason-messer/raku/ch-1.p627
-rw-r--r--challenge-074/jason-messer/raku/ch-2.p628
-rw-r--r--challenge-074/jeongoon/elm/elm.json24
-rw-r--r--challenge-074/jeongoon/elm/src/Ch1.elm22
-rw-r--r--challenge-074/jeongoon/elm/src/Ch2.elm38
-rw-r--r--challenge-074/jeongoon/elm/src/Main.elm106
-rw-r--r--challenge-074/jeongoon/perl/ch-1.pl2
-rw-r--r--challenge-074/jeongoon/perl/ch-2.pl30
-rw-r--r--challenge-074/jeongoon/raku/ch-2.raku8
-rw-r--r--challenge-074/lance-wicks/blog.txt1
-rw-r--r--challenge-074/lance-wicks/blog1.txt1
-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
-rw-r--r--challenge-074/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-074/laurent-rosenfeld/perl/ch-1.pl13
-rw-r--r--challenge-074/laurent-rosenfeld/perl/ch-2.pl19
-rw-r--r--challenge-074/laurent-rosenfeld/raku/ch-1.raku11
-rw-r--r--challenge-074/laurent-rosenfeld/raku/ch-2.raku17
-rw-r--r--challenge-074/lubos-kolouch/perl/ch-1.pl39
-rw-r--r--challenge-074/lubos-kolouch/perl/ch-2.pl56
-rw-r--r--challenge-074/lubos-kolouch/python/ch-1.py37
-rw-r--r--challenge-074/lubos-kolouch/python/ch-2.py43
-rw-r--r--challenge-074/markus-holzer/raku/ch-1.raku6
-rw-r--r--challenge-074/markus-holzer/raku/ch-2.raku5
-rw-r--r--challenge-074/mohammad-anwar/blog.txt1
-rw-r--r--challenge-074/mohammad-anwar/blog1.txt1
-rw-r--r--challenge-074/mohammad-anwar/blog2.txt1
-rw-r--r--challenge-074/nunovieira220/README1
-rw-r--r--challenge-074/nunovieira220/perl/ch-1.pl21
-rw-r--r--challenge-074/nunovieira220/perl/ch-2.pl36
-rw-r--r--challenge-074/pete-houston/perl/ch-1.pl29
-rw-r--r--challenge-074/pete-houston/perl/ch-2.pl45
-rw-r--r--challenge-074/richard-park/apl/FNRCharacter.aplf9
-rw-r--r--challenge-074/richard-park/apl/MajorityElement.aplf5
-rw-r--r--challenge-074/richard-park/apl/ch-1.aplf5
-rw-r--r--challenge-074/richard-park/apl/ch-2.aplf9
-rw-r--r--challenge-074/richard-park/blog.txt1
-rw-r--r--challenge-074/roger-bell-west/blog.txt1
-rw-r--r--challenge-074/sgreen/README.md41
-rw-r--r--challenge-074/sgreen/blog.txt1
-rwxr-xr-xchallenge-074/sgreen/perl/ch-1.pl28
-rwxr-xr-xchallenge-074/sgreen/perl/ch-2.pl34
-rw-r--r--challenge-074/shahed-nooshmand/blog.txt1
-rw-r--r--challenge-074/shahed-nooshmand/raku/ch-1.raku1
-rw-r--r--challenge-074/shahed-nooshmand/raku/ch-2.raku1
-rw-r--r--challenge-074/tyler-wardhaugh/README1
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/.gitignore11
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/LICENSE214
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/README.md37
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/deps.edn15
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/pom.xml38
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/c74.clj12
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj20
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj33
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/test/tw/weekly/c74_test.clj14
-rw-r--r--challenge-074/walt-mankowski/blog.txt1
-rw-r--r--challenge-074/walt-mankowski/cpp/Makefile23
-rw-r--r--challenge-074/walt-mankowski/cpp/ch-1.cpp24
-rw-r--r--challenge-074/walt-mankowski/cpp/ch-2.cpp36
-rw-r--r--challenge-074/walt-mankowski/perl/ch-1.pl36
-rw-r--r--challenge-074/walt-mankowski/perl/ch-2.pl63
-rw-r--r--challenge-074/walt-mankowski/python/ch-1.py15
-rw-r--r--challenge-074/walt-mankowski/python/ch-2.py23
-rw-r--r--challenge-074/wambash/raku/ch-1.raku14
-rw-r--r--challenge-074/wambash/raku/ch-2.raku17
-rw-r--r--challenge-074/wanderdoc/perl/ch-1.pl33
-rw-r--r--challenge-074/wanderdoc/perl/ch-2.pl52
92 files changed, 2329 insertions, 29 deletions
diff --git a/challenge-074/arne-sommer/blog.txt b/challenge-074/arne-sommer/blog.txt
new file mode 100644
index 0000000000..e2c5b888ac
--- /dev/null
+++ b/challenge-074/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/majority-character.html
diff --git a/challenge-074/arne-sommer/raku/ch-1.raku b/challenge-074/arne-sommer/raku/ch-1.raku
new file mode 100755
index 0000000000..cde35bbc87
--- /dev/null
+++ b/challenge-074/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,17 @@
+#! /usr/bin/env raku
+
+subset NonNegativeInt of Int where * >= 0;
+
+unit sub MAIN (*@A where @A.elems >= 1 && all(@A) ~~ NonNegativeInt);
+
+my $N = @A.elems;
+
+my %count;
+
+my $floor = floor($N / 2);
+
+@A.map({ %count{$_}++ });
+
+my $value = %count.keys.grep({ %count{$_} > $floor })[0];
+
+say $value // '-1';
diff --git a/challenge-074/arne-sommer/raku/ch-2.raku b/challenge-074/arne-sommer/raku/ch-2.raku
new file mode 100755
index 0000000000..a253fa6dd4
--- /dev/null
+++ b/challenge-074/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,39 @@
+#! /usr/bin/env raku
+
+unit sub MAIN ($S where $S.chars >= 1 && ! $S.contains('#'), :v(:$verbose));
+
+my $length = $S.chars;
+
+my $result;
+
+for 1 .. $length -> $pass
+{
+ my $substring = $S.substr(0, $pass);
+
+ my %count;
+ my @characters;
+
+ for $substring.comb.reverse -> $character
+ {
+ @characters.push($character) unless %count{$character};
+ %count{$character}++;
+ }
+
+ my $found = '#';
+
+ for @characters -> $character
+ {
+ if %count{$character} == 1
+ {
+ $found = $character;
+ last;
+ }
+ }
+
+ $result ~= $found;
+
+ say ": Pass $pass: \"$substring\" FNR: $found" if $verbose;
+}
+
+say $result;
+
diff --git a/challenge-074/arne-sommer/raku/fnr-character b/challenge-074/arne-sommer/raku/fnr-character
new file mode 100755
index 0000000000..a253fa6dd4
--- /dev/null
+++ b/challenge-074/arne-sommer/raku/fnr-character
@@ -0,0 +1,39 @@
+#! /usr/bin/env raku
+
+unit sub MAIN ($S where $S.chars >= 1 && ! $S.contains('#'), :v(:$verbose));
+
+my $length = $S.chars;
+
+my $result;
+
+for 1 .. $length -> $pass
+{
+ my $substring = $S.substr(0, $pass);
+
+ my %count;
+ my @characters;
+
+ for $substring.comb.reverse -> $character
+ {
+ @characters.push($character) unless %count{$character};
+ %count{$character}++;
+ }
+
+ my $found = '#';
+
+ for @characters -> $character
+ {
+ if %count{$character} == 1
+ {
+ $found = $character;
+ last;
+ }
+ }
+
+ $result ~= $found;
+
+ say ": Pass $pass: \"$substring\" FNR: $found" if $verbose;
+}
+
+say $result;
+
diff --git a/challenge-074/arne-sommer/raku/fnr-character-wrongish b/challenge-074/arne-sommer/raku/fnr-character-wrongish
new file mode 100755
index 0000000000..035cda37cb
--- /dev/null
+++ b/challenge-074/arne-sommer/raku/fnr-character-wrongish
@@ -0,0 +1,39 @@
+#! /usr/bin/env raku
+
+unit sub MAIN ($S where $S.chars >= 1, :v(:$verbose));
+
+my $length = $S.chars;
+
+my $result;
+
+for 1 .. $length -> $pass
+{
+ my $substring = $S.substr(0, $pass);
+
+ my %count;
+ my @characters;
+
+ for $substring.comb.reverse -> $character
+ {
+ @characters.push($character) unless %count{$character};
+ %count{$character}++;
+ }
+
+ my $found = '#';
+
+ for @characters -> $character
+ {
+ if %count{$character} == 1
+ {
+ $found = $character;
+ last;
+ }
+ }
+
+ $result ~= $found;
+
+ say ": Pass $pass: \"$substring\" FNR: $found" if $verbose;
+}
+
+say $result;
+
diff --git a/challenge-074/arne-sommer/raku/majority-element b/challenge-074/arne-sommer/raku/majority-element
new file mode 100755
index 0000000000..cde35bbc87
--- /dev/null
+++ b/challenge-074/arne-sommer/raku/majority-element
@@ -0,0 +1,17 @@
+#! /usr/bin/env raku
+
+subset NonNegativeInt of Int where * >= 0;
+
+unit sub MAIN (*@A where @A.elems >= 1 && all(@A) ~~ NonNegativeInt);
+
+my $N = @A.elems;
+
+my %count;
+
+my $floor = floor($N / 2);
+
+@A.map({ %count{$_}++ });
+
+my $value = %count.keys.grep({ %count{$_} > $floor })[0];
+
+say $value // '-1';
diff --git a/challenge-074/arne-sommer/raku/majority-element-wrongish b/challenge-074/arne-sommer/raku/majority-element-wrongish
new file mode 100755
index 0000000000..73153a18d7
--- /dev/null
+++ b/challenge-074/arne-sommer/raku/majority-element-wrongish
@@ -0,0 +1,15 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@A where @A.elems >= 1 && all(@A) ~~ Int);
+
+my $N = @A.elems;
+
+my %count;
+
+my $floor = floor($N / 2);
+
+@A.map({ %count{$_}++ });
+
+my $value = %count.keys.grep({ %count{$_} > $floor })[0];
+
+say $value // '-1';
diff --git a/challenge-074/ash/raku/ch-1.cpp b/challenge-074/ash/cpp/ch-1.cpp
index 74d78a32c3..74d78a32c3 100644
--- a/challenge-074/ash/raku/ch-1.cpp
+++ b/challenge-074/ash/cpp/ch-1.cpp
diff --git a/challenge-074/brtastic/blog.txt b/challenge-074/brtastic/blog.txt
new file mode 100644
index 0000000000..786fe5c2a5
--- /dev/null
+++ b/challenge-074/brtastic/blog.txt
@@ -0,0 +1 @@
+https://brtastic.xyz/blog/article/perl-weekly-74
diff --git a/challenge-074/brtastic/perl/ch-1.pl b/challenge-074/brtastic/perl/ch-1.pl
new file mode 100644
index 0000000000..0bc8072171
--- /dev/null
+++ b/challenge-074/brtastic/perl/ch-1.pl
@@ -0,0 +1,28 @@
+use v5.30;
+use warnings;
+
+# the solution is based on my own module Quantum::Superpositions::Lazy from CPAN
+use Quantum::Superpositions::Lazy;
+
+sub get_majority
+{
+ # a superposition will automate all the counting for us
+ my $list = superpos(@_);
+
+ # superpositions have a built in statistics module
+ # the result of most_probable is actually a new superposition, it can have multiple states
+ my ($state) = $list->stats->most_probable->states->@*;
+
+ # we now know that this element is certainly most frequent, but does it have proper weight?
+ return $state->weight > 0.5 ? $state->value : -1;
+}
+
+use Test::More;
+
+is get_majority(1, 1, 1, 2), 1;
+is get_majority(1, 1, 2, 2), -1;
+is get_majority(1, 1, 2, 2, 2), 2;
+is get_majority(1, 2, 3, 4), -1;
+is get_majority(6), 6;
+
+done_testing;
diff --git a/challenge-074/brtastic/perl/ch-2.pl b/challenge-074/brtastic/perl/ch-2.pl
new file mode 100644
index 0000000000..d1a93a1ffd
--- /dev/null
+++ b/challenge-074/brtastic/perl/ch-2.pl
@@ -0,0 +1,43 @@
+use v5.30;
+use warnings;
+use List::Util qw(first);
+
+# the solution is based on my own module Quantum::Superpositions::Lazy from CPAN
+use Quantum::Superpositions::Lazy;
+
+sub first_non_repeating
+{
+ my sub find_fnt
+ {
+ my @split = split "", shift;
+
+ # a superposition will automate some of the counting for us
+ my $split_pos = superpos(@split);
+
+ # the default weight for an element is 1, and weights are merged by values
+ my @non_repeating = grep { $_->weight == 1 } $split_pos->states->@*;
+
+ return "#" if @non_repeating == 0;
+ return (shift @non_repeating)->value if @non_repeating == 1;
+
+ # since we have a couple of non-repeating characters, we get the first one
+ # (the superposition here helps so that we solve this by a simple eq)
+ my $alternatives = superpos(@non_repeating);
+ return first { $_ eq $alternatives } @split;
+ }
+
+ my ($string) = @_;
+ my $result = "";
+ for (1 .. length $string) {
+ $result .= find_fnt(substr $string, 0, $_);
+ }
+ return $result;
+}
+
+use Test::More;
+
+is first_non_repeating("ababc"), "aab#c";
+is first_non_repeating("xyzzyx"), "xxxxx#";
+is first_non_repeating("geeksforgeeksandgeeksquizfor"), "ggggggggkkksfffffffffffffora";
+
+done_testing;
diff --git a/challenge-074/cheok-yin-fung/blog.txt b/challenge-074/cheok-yin-fung/blog.txt
new file mode 100644
index 0000000000..840701c3dc
--- /dev/null
+++ b/challenge-074/cheok-yin-fung/blog.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/c_y_fung/2020/08/self-challenge-and-weekly-challenge.html
diff --git a/challenge-074/cheok-yin-fung/perl/ch-1.pl b/challenge-074/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..0b67899143
--- /dev/null
+++ b/challenge-074/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+# ref:
+# https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
+# Perl Weekly Challenge #074 Task 1 Majority Element
+# task statement:
+# Write a script to find the majority element.
+# If none found then print -1.
+# Usage: ch-1.pl [ARRAY]
+use strict;
+use warnings;
+#use Test::More tests => 3;
+
+
+sub verify {
+ my @array = @{$_[0]};
+ my $m = $_[1];
+ my $c = 0;
+ for (@array) {
+ $c++ if $m==$_;
+ }
+ return ($c > (scalar @array)/2.0 ? 1 : undef);
+}
+
+sub bm_majority_vote_alg {
+ my @array = @{$_[0]};
+ my $i = 0;
+ my $m;
+ for (@array) {
+ if ($i == 0) {