diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-03 22:35:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-03 22:35:25 +0100 |
| commit | de8db1b652ee2ecc9a9356c2d3539aed42bb279c (patch) | |
| tree | 8c049c869702e5c9714f4d024194643c565f004d | |
| parent | 7991ef16315f4434698b0450703ed585e5431495 (diff) | |
| parent | 443e279a64a10765958ba06e231c776fe5a96387 (diff) | |
| download | perlweeklychallenge-club-de8db1b652ee2ecc9a9356c2d3539aed42bb279c.tar.gz perlweeklychallenge-club-de8db1b652ee2ecc9a9356c2d3539aed42bb279c.tar.bz2 perlweeklychallenge-club-de8db1b652ee2ecc9a9356c2d3539aed42bb279c.zip | |
Merge pull request #10534 from ntovar/branch-280
Challenge 280. Add Perl and Bash (ch-1) solutions. By Nelo Tovar
| -rwxr-xr-x | challenge-280/nelo-tovar/bash/ch-1.sh | 33 | ||||
| -rw-r--r-- | challenge-280/nelo-tovar/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-280/nelo-tovar/perl/ch-2.pl | 30 |
3 files changed, 98 insertions, 0 deletions
diff --git a/challenge-280/nelo-tovar/bash/ch-1.sh b/challenge-280/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..aecb3d56b3 --- /dev/null +++ b/challenge-280/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 280 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-280/ +# +# Task 1 - Twice Appearance + +function twice_appearance() { + local string=$1 + local length=${#string} + declare -A temp + + for (( i = 0; i < $length; i++ )); do + letter=${string:$i:1} + if [[ -n "${temp[$letter]}" ]]; then + echo $letter + break + fi + temp[$letter]=1 + done + + echo ' ' +} + +examples=("acbddbca" "abccd" "abcdabbb") + +for e in ${examples[@]}; do + ta=$(twice_appearance "$e") + echo "Input : str = $e" + echo "Output : $ta" +done + diff --git a/challenge-280/nelo-tovar/perl/ch-1.pl b/challenge-280/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..d47e00c938 --- /dev/null +++ b/challenge-280/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 280 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-280/ +# +# Task 1 - Twice Appearance +# + +use strict; +use warnings; +use v5.28; + +my @examples = ("acbddbca", "abccd", "abcdabbb"); + +sub twice_appearance { + my @letters = split(//,shift); + my %temp; + + foreach my $letter (@letters) { + return $letter if ($temp{$letter}); + + $temp{$letter} = 1; + } + + return ' ' +} + +for my $elements (@examples) { + my $ta = twice_appearance $elements; + + say 'Input : $str = ', $elements; + say 'Output : ', $ta; + say ' '; +} diff --git a/challenge-280/nelo-tovar/perl/ch-2.pl b/challenge-280/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..fbf9e2def1 --- /dev/null +++ b/challenge-280/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 280 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-280/ +# +# Task 2 - Count Asterisks +# + +use strict; +use warnings; +use v5.28; + +my @examples = ("p|*e*rl|w**e|*ekly|", "perl", "th|ewe|e**|k|l***ych|alleng|e"); + +sub count_asterisks { + my $string = shift; + + $string =~ s/\|.*?\|//sg; + + return $string =~ tr/*//; +} + +for my $elements (@examples) { + my $ca = count_asterisks $elements; + + say 'Input : @nums = ', $elements; + say 'Output : ', $ca; + say ' '; +} |
