aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorntovar <tovar.nelo@gmail.com>2024-03-15 10:47:19 -0500
committerntovar <tovar.nelo@gmail.com>2024-03-15 10:47:19 -0500
commit10bcbdcb5340a84a89069af5ca1bf69093ad9469 (patch)
tree34ddb24ac690ba33128f575bd0c4f4fbbf1aa931
parentba075f782a014191064106f82660f756098b50be (diff)
downloadperlweeklychallenge-club-10bcbdcb5340a84a89069af5ca1bf69093ad9469.tar.gz
perlweeklychallenge-club-10bcbdcb5340a84a89069af5ca1bf69093ad9469.tar.bz2
perlweeklychallenge-club-10bcbdcb5340a84a89069af5ca1bf69093ad9469.zip
Challenge 260. Add Perl and Bash solutions. By Nelo Tovar.
-rwxr-xr-xchallenge-260/nelo-tovar/bash/ch-1.sh52
-rwxr-xr-xchallenge-260/nelo-tovar/bash/ch-2.sh66
-rw-r--r--challenge-260/nelo-tovar/perl/ch-1.pl39
-rw-r--r--challenge-260/nelo-tovar/perl/ch-2.pl45
4 files changed, 202 insertions, 0 deletions
diff --git a/challenge-260/nelo-tovar/bash/ch-1.sh b/challenge-260/nelo-tovar/bash/ch-1.sh
new file mode 100755
index 0000000000..bcca9463c6
--- /dev/null
+++ b/challenge-260/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,52 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 260 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/
+#
+# Task 1 : Unique Occurrences
+
+function unique_occurrences() {
+ local nums=("$@")
+ local length=${#nums[@]}
+ declare -A counter
+
+# for (( i = 0; i < $length; i++ )); do
+# count=0
+# for (( j = 0 ; j < $length; j++ )); do
+# if [[ ${nums[$j]} -lt ${nums[$i]} ]]; then
+# ((count++))
+# fi
+# done
+# smallers+=($count)
+# done
+
+ for i in ${nums[@]}; do
+ counter[$i]=$((${counter[$i]} + 1))
+ done
+
+ frequencies=(${counter[@]})
+ length=${#frequencies[@]}
+ for (( i = 0; i < $length - 1; i++ )); do
+ for (( j = i + 1; j < $length; j++ )); do
+ if [[ ${frequencies[$i]} -eq ${frequencies[$j]} ]]; then
+ echo 0
+ return
+ fi
+ done
+ done
+ echo 1
+}
+
+example1='1 2 2 1 1 3'
+example2=' 1 2 3'
+example3='-2 0 1 -2 1 1 0 1 -2 9'
+
+for e in "$example1" "$example2" "$example3"; do
+ array=($e)
+ uo=$(unique_occurrences "${array[@]}")
+ echo "Input : ints = (${array[@]})"
+ echo "Output : $uo"
+ echo ""
+done
+
diff --git a/challenge-260/nelo-tovar/bash/ch-2.sh b/challenge-260/nelo-tovar/bash/ch-2.sh
new file mode 100755
index 0000000000..47b61c3c9d
--- /dev/null
+++ b/challenge-260/nelo-tovar/bash/ch-2.sh
@@ -0,0 +1,66 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 260 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/
+#
+# Task 2 : Dictionary Rank
+
+function swap() {
+ local string=$1
+ local len=${#string}
+ local from=$2
+ local to=$3
+ local i=0
+ local s=""
+ while [[ $i -lt $len ]]; do
+ if [[ $i -eq $from ]]; then
+ s=$s${string:$to:1}
+ elif [[ $i -eq $to ]]; then
+ s=$s${string:$from:1}
+ else
+ s=$s${string:$i:1}
+ fi
+ i=$(($i+1))
+ done
+ echo $s
+}
+
+function perm() {
+ local string=$1
+ local len=${#string}
+ local idx=$2
+ if [[ $idx -ge $len ]]; then
+ echo $string
+ else
+ local i=$idx
+ while [[ $i -lt $len ]]; do
+ perm $(swap $string $i $idx) $((idx+1))
+ i=$((i+1))
+ done
+ fi
+}
+
+function dictionary_rank() {
+ local word=$1
+ local rank=0
+ local dictionary=($(perm $word | sort))
+
+ for i in "${!dictionary[@]}"; do
+ if [[ "$word" = "${dictionary[$i]}" ]]; then
+ rank=$(($i + 1))
+ break
+ fi
+ done
+
+ echo $rank
+}
+
+example1='CAT GOOGLE SECRET'
+
+for e in $example1; do
+ dr=$(dictionary_rank "$e")
+ echo "Input : word = ($e)"
+ echo -e "Output : $dr\n"
+done
+
diff --git a/challenge-260/nelo-tovar/perl/ch-1.pl b/challenge-260/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..233e36fdf2
--- /dev/null
+++ b/challenge-260/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 260 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/
+#
+# Task 1 - Unique Occurrences
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::Util qw/uniqint/;
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ 1,2,2,1,1,3 ],
+ [ 1, 2, 3 ],
+ [ -2,0,1,-2,1,1,0,1,-2,9 ],
+);
+
+sub unique_occurrences {
+ my $nums = shift;
+ my %counter;
+
+ $counter{$_}++ foreach (@$nums);
+ my $frequencies = values %counter;
+ my $unique = uniqint(values %counter);
+
+ return $frequencies eq $unique ? 1 : 0;
+}
+
+for my $elements (@examples) {
+ my $uo = unique_occurrences $elements;
+
+ say 'Input : @ints = ', dump(@$elements);
+ say 'Output : ', $uo;
+ say ' ';
+}
diff --git a/challenge-260/nelo-tovar/perl/ch-2.pl b/challenge-260/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..d469124c60
--- /dev/null
+++ b/challenge-260/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 260 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/
+#
+# Task 2 - Dictionary Rank
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::MoreUtils qw (bsearch_index);
+use Algorithm::Combinatorics qw(permutations);
+
+my @examples = (
+ 'CAT',
+ 'GOOGLE',
+ 'SECRET',
+);
+
+sub dictionary_rank {
+ my $word = shift;
+ my $rank = 0;
+ my @letters = split //, $word;
+ my @dictionary;
+
+ my $iter = permutations(\@letters);
+ while (my $c = $iter->next) {
+ push @dictionary, join '', @$c;
+ }
+
+ @dictionary = sort @dictionary;
+ $rank = bsearch_index {$word eq $_} @dictionary;
+
+ return $rank;
+}
+
+for my $elements (@examples) {
+ my $dr = dictionary_rank $elements;
+
+ say 'Input : word = ', $elements;
+ say 'Output : ', $dr;
+ say ' ';
+}