diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-25 10:14:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-25 10:14:06 +0100 |
| commit | 4a231f1885c6aff7af7e92cdc1c6363b8ced4db6 (patch) | |
| tree | d10f9071356e397c22326c5a25e569a393242fc7 | |
| parent | a5d98d2f9d85a2742f783e591f1386d0729d5595 (diff) | |
| parent | 545f941d0bb88b9a3561f12d47f6584e278a3a67 (diff) | |
| download | perlweeklychallenge-club-4a231f1885c6aff7af7e92cdc1c6363b8ced4db6.tar.gz perlweeklychallenge-club-4a231f1885c6aff7af7e92cdc1c6363b8ced4db6.tar.bz2 perlweeklychallenge-club-4a231f1885c6aff7af7e92cdc1c6363b8ced4db6.zip | |
Merge pull request #10689 from ntovar/branch-283
Challenge 283. Add Perl and Bash solutions. By Nelo Tovar
| -rwxr-xr-x | challenge-283/nelo-tovar/bash/ch-1.sh | 45 | ||||
| -rwxr-xr-x | challenge-283/nelo-tovar/bash/ch-2.sh | 44 | ||||
| -rw-r--r-- | challenge-283/nelo-tovar/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-283/nelo-tovar/perl/ch-2.pl | 39 |
4 files changed, 168 insertions, 0 deletions
diff --git a/challenge-283/nelo-tovar/bash/ch-1.sh b/challenge-283/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..4cd128407b --- /dev/null +++ b/challenge-283/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 283 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/ +# +# Task 1 - Unique Number + +function frequency() { + local array=("$@") + declare -A local frequency + + for i in ${array[@]}; do + temp=${frequency[$i]} + ((temp++)) + frequency[$i]=$temp + done + + echo "${frequency[@]@K}" +} + +function unique_number() { + local nums=("$@") + declare -A frequencies="($(frequency "${nums[@]}"))" + + for key in ${!frequencies[@]}; do + if [ ${frequencies[$key]} -eq 1 ]; then + echo $key + exit + fi + done + + echo '' +} + +examples=('3 3 1' '3 2 4 2 4' '1' '4 3 1 1 1 4') + +for e in "${examples[@]}"; do + array=($e) + un=($(unique_number "${array[@]}")) + echo "Input : ints = (${array[@]})" + echo "Output : $un" + echo "" +done + diff --git a/challenge-283/nelo-tovar/bash/ch-2.sh b/challenge-283/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..d83a756dba --- /dev/null +++ b/challenge-283/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 283 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/ +# +# Task 2 - Digit Count Value + +function frequency() { + local array=("$@") + declare -A local frequency + + for i in ${array[@]}; do + temp=${frequency[$i]} + ((temp++)) + frequency[$i]=$temp + done + + echo "${frequency[@]@K}" +} + +function digit_count_value() { + local nums=("$@") + declare -A frequencies="($(frequency "${nums[@]}"))" + + for i in ${!nums[@]}; do + if [[ ${frequencies[$i]} -ne ${nums[$i]} ]]; then + echo 'false' + exit + fi + done + + echo 'true' +} + +examples=('1 2 1 0' '0 3 0') + +for e in "${examples[@]}"; do + array=($e) + dcv=$(digit_count_value "${array[@]}") + echo "Input : ints = (${array[@]})" + echo -e "Output : $dcv\n" +done + diff --git a/challenge-283/nelo-tovar/perl/ch-1.pl b/challenge-283/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..af1bf52242 --- /dev/null +++ b/challenge-283/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 283 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/ +# +# Task 1 - Unique Number +# + +use strict; +use warnings; +use v5.28; +use List::MoreUtils qw(frequency); +use Data::Dump qw(dump); + +my @examples = ( + [ 3, 3, 1 ], + [ 3, 2, 4, 2, 4 ], + [ 1 ], + [ 4, 3, 1, 1, 1, 4] +); + +sub unique_number { + my $nums = shift; + my %frequencies = frequency $nums->@*; + + foreach my $key (keys %frequencies) { + return $key if ($frequencies{$key} eq 1) + } + + return '' +} + +for my $elements (@examples) { + my $un = unique_number $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', $un; + say ' '; +} diff --git a/challenge-283/nelo-tovar/perl/ch-2.pl b/challenge-283/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..2bf32a2664 --- /dev/null +++ b/challenge-283/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 283 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/ +# +# Task 2 - Digit Count Value +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 1, 2, 1, 0 ], + [ 0, 3, 0], +); + +sub digit_count_value { + my $nums = shift; + my $length = scalar @$nums; + + for (my $i = 0; $i < $length; $i++) { + my $count = scalar grep {$_ eq $i} @$nums; + + return 'false' if ($count ne @$nums[$i]) + } + + return 'true'; +} + +for my $elements (@examples) { + my $dgv = digit_count_value $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', $dgv; + say ' '; +} |
