aboutsummaryrefslogtreecommitdiff
path: root/challenge-265
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-265')
-rwxr-xr-xchallenge-265/nelo-tovar/bash/ch-1.sh40
-rw-r--r--challenge-265/nelo-tovar/perl/ch-1.pl46
-rw-r--r--challenge-265/nelo-tovar/perl/ch-2.pl58
3 files changed, 144 insertions, 0 deletions
diff --git a/challenge-265/nelo-tovar/bash/ch-1.sh b/challenge-265/nelo-tovar/bash/ch-1.sh
new file mode 100755
index 0000000000..ce24e70c6d
--- /dev/null
+++ b/challenge-265/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,40 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 265 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-265/
+#
+# Task 1 : 33% Appearance
+
+function _33_percent_appearence() {
+ local nums=("$@")
+ local length=${#nums[@]}
+ local percent=$((($length * 33 + 100 -1) / 100))
+ local frequencies
+ local candidates
+
+ for i in "${nums[@]}"; do
+ ((frequencies[$i]++))
+ done
+
+ for key in ${!frequencies[@]}; do
+ if [[ ${frequencies[$key]} -ge $percent ]]; then
+ candidates+=($key)
+ fi
+ done
+
+ candidates=($(IFS=$'\n'; echo "${candidates[*]}" | sort -n))
+
+ echo ${candidates[0]}
+}
+
+examples=('1 2 3 3 3 3 4 2' '1 1' '1 2 3')
+
+for e in ${!examples[@]}; do
+ array=(${examples[$e]})
+ pa=$(_33_percent_appearence "${array[@]}")
+ echo "Input : inst = (${array[@]})"
+ echo "Output : $pa"
+ echo ""
+done
+
diff --git a/challenge-265/nelo-tovar/perl/ch-1.pl b/challenge-265/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..7edb5f137f
--- /dev/null
+++ b/challenge-265/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 265 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-265/
+#
+# Task 1 - 33% Appearance
+#
+
+use strict;
+use warnings;
+use v5.28;
+use Data::Dump qw(dump);
+use POSIX qw(ceil);
+
+my @examples = (
+ [ 1, 2, 3, 3, 3, 3, 4, 2 ],
+ [ 1, 1 ],
+ [ 1, 2, 3 ],
+);
+
+sub _33_percent_appearance {
+ my $nums = shift;
+ my $len = scalar @$nums;
+ my $percent = ceil($len * 0.33);
+ my %frequencies;
+ my @candidates;
+
+ $frequencies{$_}++ for (@$nums);
+
+ foreach my $k (keys %frequencies) {
+ push @candidates,$k if ($frequencies{$k} >= $percent );
+ }
+
+
+ return (sort {$a<=> $b} @candidates)[0];
+
+}
+
+for my $elements (@examples) {
+ my $pa = _33_percent_appearance $elements;
+
+ say 'Input : @ints = ', dump(@$elements);
+ say 'Output : ', $pa;
+ say ' ';
+}
diff --git a/challenge-265/nelo-tovar/perl/ch-2.pl b/challenge-265/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..2179fbca33
--- /dev/null
+++ b/challenge-265/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 265 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-265/
+#
+# Task 2 - Completing Word
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::Util qw (min max);
+use Algorithm::Combinatorics qw(combinations);
+use Data::Dump qw(dump);
+
+my @examples = (
+ { str => 'aBc 11c', words => ['accbbb', 'abc', 'abbc'] },
+ { str => 'Da2 abc', words => ['abcm', 'baacd', 'abaadc'] },
+ { str => 'JB 007', words => ['jj', 'bb', 'bjb'] },
+);
+
+sub completing_word {
+ my $string = lc(shift);
+ my $words = shift;
+ my %frequencies;
+ my @candidates = ();
+
+ foreach my $letter (split //, $string) {
+ $frequencies{$letter}++ if ($letter =~ /[a-zA-Z]/)
+ }
+
+ foreach my $word ($words->@*) {
+ $word = lc($word);
+ my $is_candidate = 1;
+ foreach my $k (keys %frequencies) {
+ my @letters = $word =~ m/[$k]/g;
+
+ $is_candidate = 0 if ($frequencies{$k} > scalar @letters);
+
+ last if (!$is_candidate);
+ }
+ push @candidates, $word if $is_candidate;
+ }
+
+ return (sort({length($a) <=> length($b)} @candidates))[0];
+}
+
+for my $elements (@examples) {
+ my $string = $elements->{str};
+ my $words = $elements->{words};
+ my $cw = completing_word $string, $words;
+
+ say "Input : \$str = '$string'";
+ say ' : @str = ', dump(@$words);
+ say 'Output : ', $cw;
+ say ' ';
+}