diff options
| author | ntovar <tovar.nelo@gmail.com> | 2023-12-28 17:47:16 -0500 |
|---|---|---|
| committer | ntovar <tovar.nelo@gmail.com> | 2023-12-28 17:47:16 -0500 |
| commit | 8edee88892c327dcbcee0c0ccc3f19d61f3de86b (patch) | |
| tree | 7cc966c9ecd1be06dab1bcd326d406a3a9c0338d | |
| parent | 2fe9fe92abffa2fb01e876e42a07a8da80aacac9 (diff) | |
| download | perlweeklychallenge-club-8edee88892c327dcbcee0c0ccc3f19d61f3de86b.tar.gz perlweeklychallenge-club-8edee88892c327dcbcee0c0ccc3f19d61f3de86b.tar.bz2 perlweeklychallenge-club-8edee88892c327dcbcee0c0ccc3f19d61f3de86b.zip | |
Challenge 249. Add Perl and Bash solutions. By Nelo Tovar
| -rwxr-xr-x | challenge-249/nelo-tovar/bash/ch-1.sh | 40 | ||||
| -rwxr-xr-x | challenge-249/nelo-tovar/bash/ch-2.sh | 96 | ||||
| -rw-r--r-- | challenge-249/nelo-tovar/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-249/nelo-tovar/perl/ch-2.pl | 51 |
4 files changed, 233 insertions, 0 deletions
diff --git a/challenge-249/nelo-tovar/bash/ch-1.sh b/challenge-249/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..af52782c2e --- /dev/null +++ b/challenge-249/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 249 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-249/ +# +# Task 1 : Equal Pairs + +function equal_pairs() { + local numbers=("$@") + local pairs=() + + while [[ ${numbers[@]} > 0 ]]; do + number=${numbers[0]} + numbers=("${numbers[@]:1}") + length=${#numbers[@]} + + for (( i = 0; i < $length; i++ )); do + if [[ $number == ${numbers[$i]} ]]; then + pairs+=("($number, $number)") + unset numbers[$i] + break + fi + done + done + + echo ${pairs[@]} +} + +example1='3 2 3 2 2 2' +example2='1 2 3 4' + +for e in "$example1" "$example2"; do + array=($e) + ep=($(equal_pairs "${array[@]}")) + echo "Input : @ints = (${array[@]})" + echo "Output : (${ep[@]})" + echo "" +done + diff --git a/challenge-249/nelo-tovar/bash/ch-2.sh b/challenge-249/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..24ef6574d3 --- /dev/null +++ b/challenge-249/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 249 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-249/ +# +# Task 2 : DI String Match + +function combinations() { + local -a results=() + let idx=$1 + shift + local list=("$@") + local length=${#list[@]} + + for (( j = 0; j < $length; j++ )); do + if (( idx % 2 )); then + results=("${results[@]}" "${list[$j]}"); + fi + let idx\>\>=1 + done + echo "${results[@]}" +} + +function min() { + local array=("$@") + local min=${array[0]} + + for n in ${array[@]}; do + if [[ n -lt $min ]]; then + min=$n + fi + done + + echo $min +} + +function max() { + local array=("$@") + local max=${array[0]} + + for n in ${array[@]}; do + if [[ n -gt $max ]]; then + max=$n + fi + done + + echo $max +} + +function di_string_match() { + local s=$@ + local length=${#s} + local data=$(seq 0 $length) + + local R=1000 + local C_RANGE=$(($length + 1)) + for C in $C_RANGE; do + for I in $(seq 1 $R); do + ok=1 + perm="$(for X in "${data[@]}"; do echo "$X"; done | shuf -n $C | paste -s -d' ' | tr -d ' ')" + + for (( i = 0; i < $length; i++ )); do + char=${s:$i:1} + p1=${perm:$i:1} + p2=${perm:$i+1:1} + + if [[ $char == 'I' && "$p1" -ge "$p2" ]]; then + ok=0 + break + fi + if [[ $char == 'D' && "$p1" -le "$p2" ]]; then + ok=0 + break + fi + done + if [[ $ok -eq 1 ]]; then + echo $perm + return 0 + fi + done + done +} + +example1='IDID' +example2='III' +example3='DDI' + +for e in "$example1" "$example2" "$example3"; do + array=($e) + dsm=$(di_string_match "$e") + #di_string_match $e + echo "Input : @str = $e" + echo -e "Output : $dsm\n" +done + diff --git a/challenge-249/nelo-tovar/perl/ch-1.pl b/challenge-249/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..45cdcc4e05 --- /dev/null +++ b/challenge-249/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 249 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-249/ +# +# Task 1 - Equal Pairs +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 3, 2, 3, 2, 2, 2 ], + [ 1, 2, 3, 4 ], +); + +sub equal_pairs { + my $ints = shift; + my @numbers = @$ints; + my @pairs; + + while (my $number = shift(@numbers)) { + my $length = scalar @numbers; + + for (my $i = 0; $i < $length; $i++) { + if ($number == $numbers[$i]) { + push @pairs, [($number, $number)]; + splice @numbers, $i, 1; + last; + } + } + } + + return \@pairs; +} + +for my $elements (@examples) { + my $ep = equal_pairs $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', dump(@$ep); + say ' '; +} diff --git a/challenge-249/nelo-tovar/perl/ch-2.pl b/challenge-249/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..26703d33cd --- /dev/null +++ b/challenge-249/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 249 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-249/ +# +# Task 2 - DI String Match +# + +use strict; +use warnings; +use v5.28; +use Algorithm::Combinatorics qw(permutations); +use Data::Dump qw(dump); + +my @examples = ( + 'IDID', + 'III', + 'DDI', +); + +sub di_string_match { + my @s = split(//, shift); + my $length =scalar @s; + my @data = (0 .. $length); + + my $iter = permutations(\@data); + while (my $perm = $iter->next) { + my $ok = 1; + + for (my $i = 0; $i < $length; $i++) { + if ((($s[$i] eq 'I') and (@$perm[$i] >= @$perm[$i + 1])) or + (($s[$i] eq 'D') and (@$perm[$i] <= @$perm[$i + 1]))) { + $ok = 0; + last + } + } + + return $perm if ($ok) + } + + return ''; +} + +for my $elements (@examples) { + my $dsm = di_string_match $elements; + + say 'Input : @str = ', $elements; + say 'Output : ', dump(@$dsm); + say ' '; +} |
