diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-23 17:05:59 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-23 17:05:59 +0100 |
| commit | 9256e733b486670d4e25a504fa526b757a9e455b (patch) | |
| tree | 9e9cceae7a44f39a36e95771992fb01962f41e63 | |
| parent | f3c4b47674dd0ee451af7649527665b943eb6165 (diff) | |
| parent | cc539e8bbd63e0aa0caed8f8f12dafc1db1ee2c8 (diff) | |
| download | perlweeklychallenge-club-9256e733b486670d4e25a504fa526b757a9e455b.tar.gz perlweeklychallenge-club-9256e733b486670d4e25a504fa526b757a9e455b.tar.bz2 perlweeklychallenge-club-9256e733b486670d4e25a504fa526b757a9e455b.zip | |
Merge pull request #10298 from ntovar/branch-274
Challenge 274. Add Perl(ch-1) and Bash(ch-1) solutions. By Nelo Tovar
| -rwxr-xr-x | challenge-274/nelo-tovar/bash/ch-1.sh | 45 | ||||
| -rw-r--r-- | challenge-274/nelo-tovar/perl/ch-1.pl | 39 |
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-274/nelo-tovar/bash/ch-1.sh b/challenge-274/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..ae450ed558 --- /dev/null +++ b/challenge-274/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 274 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-274/ +# +# Task 1 : Goat Latin + +repeat(){ + for i in $(seq 1 $2); do echo -n "$1"; done +} + +function goat_latin() { + local sentence="$@" + local gl='' + local words=() + for i in $sentence; do + words+=($i) + done + local length=${#words[@]} + + for (( i = 0; i < $length; i++ )); do + if [[ ${words[$i]} =~ ^[AEIOUaeiou] ]]; then + gl+=${words[$i]} + echo "Processing ${words[$i]} - $gl" >&2 + else + gl+="${words[$i]:1}${words[$i]:0:1}" + fi + + ((b=i+1)) + gl+="ma$(repeat a $b) " + done + + echo $gl +} + +examples=('I love Perl' 'Perl and Raku are friends' 'The Weekly Challenge') + +for e in "${examples[@]}"; do + gl=$(goat_latin "$e") + echo "Input : sentence = $e" + echo "Output : $gl" + echo "" +done + diff --git a/challenge-274/nelo-tovar/perl/ch-1.pl b/challenge-274/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..b1ff68cd1c --- /dev/null +++ b/challenge-274/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 274 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-274/ +# +# Task 1 - Goat Latin +# + +use strict; +use warnings; +use v5.28; + +my @examples = ('I love Perl', 'Perl and Raku are friends', 'The Weekly Challenge'); + +sub goat_latin { + my $sentence = shift; + my @words = split ' ', $sentence; + my $len = scalar @words; + my $gl; + + for (my $i = 0; $i < $len; $i++) { + if ($words[$i] =~ /^[AEIOUaeiou]/) { + $gl .= ${words[$i]} + }else { + $gl .= substr($words[$i], 1) . substr($words[$i], 0, 1) + } + $gl .= 'ma' . 'a' x ($i+1) . ' '; + } + + return $gl; +} + +for my $element (@examples) { + my $gl = goat_latin $element; + + printf "Input : sentence ='%s'\n" , $element; + printf "Output : '%s'\n\n", $gl; +} |
