From fae3dea833fcea717e5cc24f0983b0784501e7cf Mon Sep 17 00:00:00 2001 From: brtastic Date: Sun, 23 Aug 2020 00:14:40 +0200 Subject: Challenge 74 refactoring and blog post link --- challenge-074/brtastic/blog.txt | 1 + challenge-074/brtastic/perl/ch-1.pl | 9 +++------ challenge-074/brtastic/perl/ch-2.pl | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) create mode 100644 challenge-074/brtastic/blog.txt 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 index 015c803bc6..0bc8072171 100644 --- a/challenge-074/brtastic/perl/ch-1.pl +++ b/challenge-074/brtastic/perl/ch-1.pl @@ -10,13 +10,10 @@ sub get_majority my $list = superpos(@_); # superpositions have a built in statistics module - my $majority = $list->stats->most_probable; + # the result of most_probable is actually a new superposition, it can have multiple states + my ($state) = $list->stats->most_probable->states->@*; - # majority is actually a new superposition, it can have multiple states - return -1 if $majority->states->@* != 1; - - # we now know that this element is certainly a majority, but does it have a proper weight? - my $state = $majority->states->[0]; + # we now know that this element is certainly most frequent, but does it have proper weight? return $state->weight > 0.5 ? $state->value : -1; } diff --git a/challenge-074/brtastic/perl/ch-2.pl b/challenge-074/brtastic/perl/ch-2.pl index 9464f0b80e..d1a93a1ffd 100644 --- a/challenge-074/brtastic/perl/ch-2.pl +++ b/challenge-074/brtastic/perl/ch-2.pl @@ -22,7 +22,7 @@ sub first_non_repeating # 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(map { $_->value } @non_repeating); + my $alternatives = superpos(@non_repeating); return first { $_ eq $alternatives } @split; } -- cgit