From f6670f0ff9e0cf8b313bdf153a298535476c0c2e Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 13 Aug 2024 22:39:13 +0100 Subject: Add Perl solution to challenge 280 --- challenge-280/paulo-custodio/Makefile | 2 ++ challenge-280/paulo-custodio/perl/ch-1.pl | 34 ++++++++++++++++++++++++++++++ challenge-280/paulo-custodio/perl/ch-2.pl | 32 ++++++++++++++++++++++++++++ challenge-280/paulo-custodio/t/test-1.yaml | 15 +++++++++++++ challenge-280/paulo-custodio/t/test-2.yaml | 15 +++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 challenge-280/paulo-custodio/Makefile create mode 100644 challenge-280/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-280/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-280/paulo-custodio/t/test-1.yaml create mode 100644 challenge-280/paulo-custodio/t/test-2.yaml diff --git a/challenge-280/paulo-custodio/Makefile b/challenge-280/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-280/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-280/paulo-custodio/perl/ch-1.pl b/challenge-280/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..bae783b821 --- /dev/null +++ b/challenge-280/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +# Challenge 280 +# +# Task 1: Twice Appearance +# Submitted by: Mohammad Sajid Anwar +# You are given a string, $str, containing lowercase English letters only. +# +# Write a script to print the first letter that appears twice. +# +# Example 1 +# Input: $str = "acbddbca" +# Output: "d" +# Example 2 +# Input: $str = "abccd" +# Output: "c" +# Example 3 +# Input: $str = "abcdabbb" +# Output: "a" + +use Modern::Perl; + +my $str = shift || ""; +say double_letter($str); + +sub double_letter { + my($str) = @_; + + my %found; + for (split //, $str) { + return $_ if $found{$_}++; + } + return ""; +} diff --git a/challenge-280/paulo-custodio/perl/ch-2.pl b/challenge-280/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..a09904d437 --- /dev/null +++ b/challenge-280/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +# Challenge 280 +# +# Task 2: Count Asterisks +# Submitted by: Mohammad Sajid Anwar +# You are given a string, $str, where every two consecutive vertical bars are +# grouped into a pair. +# +# Write a script to return the number of asterisks, *, excluding any between +# each pair of vertical bars. +# +# Example 1 +# Input: $str = "p|*e*rl|w**e|*ekly|" +# Ouput: 2 +# +# The characters we are looking here are "p" and "w**e". +# Example 2 +# Input: $str = "perl" +# Ouput: 0 +# Example 3 +# Input: $str = "th|ewe|e**|k|l***ych|alleng|e" +# Ouput: 5 +# +# The characters we are looking here are "th", "e**", "l***ych" and "e". + +use Modern::Perl; + +my $str = shift // ""; +$str =~ s/\|[^|]*\|//g; +my $count = $str =~ tr/*/*/; +say $count; diff --git a/challenge-280/paulo-custodio/t/test-1.yaml b/challenge-280/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f4d353757e --- /dev/null +++ b/challenge-280/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: acbddbca + input: + output: d +- setup: + cleanup: + args: abccd + input: + output: c +- setup: + cleanup: + args: abcdabbb + input: + output: a diff --git a/challenge-280/paulo-custodio/t/test-2.yaml b/challenge-280/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..58c30615f7 --- /dev/null +++ b/challenge-280/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: "'p|*e*rl|w**e|*ekly|'" + input: + output: 2 +- setup: + cleanup: + args: "'perl'" + input: + output: 0 +- setup: + cleanup: + args: "'th|ewe|e**|k|l***ych|alleng|e'" + input: + output: 5 -- cgit