diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-26 23:41:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-26 23:41:21 +0100 |
| commit | 18d629295946a724a754250f6ada2d47b00f154f (patch) | |
| tree | a6cb8af5a943ccc3844201dd08d2e276d64f6b2e | |
| parent | 8ee8ff419c2fd069a1e041eea79e8e114692875b (diff) | |
| parent | 9d338078338608dfca3b4b0afef8a62b4f7eb31f (diff) | |
| download | perlweeklychallenge-club-18d629295946a724a754250f6ada2d47b00f154f.tar.gz perlweeklychallenge-club-18d629295946a724a754250f6ada2d47b00f154f.tar.bz2 perlweeklychallenge-club-18d629295946a724a754250f6ada2d47b00f154f.zip | |
Merge pull request #10714 from pauloscustodio/master
Add Perl solutions
| -rw-r--r-- | challenge-264/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-264/paulo-custodio/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-264/paulo-custodio/perl/ch-2.pl | 51 | ||||
| -rw-r--r-- | challenge-264/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-264/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-265/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-265/paulo-custodio/perl/ch-1.pl | 51 | ||||
| -rw-r--r-- | challenge-265/paulo-custodio/perl/ch-2.pl | 74 | ||||
| -rw-r--r-- | challenge-265/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-265/paulo-custodio/t/test-2.yaml | 15 |
10 files changed, 278 insertions, 0 deletions
diff --git a/challenge-264/paulo-custodio/Makefile b/challenge-264/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-264/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-264/paulo-custodio/perl/ch-1.pl b/challenge-264/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..7bf856a74d --- /dev/null +++ b/challenge-264/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# Challenge 264 +# +# Task 1: Greatest English Letter +# Submitted by: Mohammad Sajid Anwar +# You are given a string, $str, made up of only alphabetic characters [a..zA..Z]. +# +# Write a script to return the greatest english letter in the given string. +# +# A letter is greatest if it occurs as lower and upper case. Also letter ‘b’ is +# greater than ‘a’ if ‘b’ appears after ‘a’ in the English alphabet. +# +# Example 1 +# Input: $str = 'PeRlwEeKLy' +# Output: L +# +# There are two letters E and L that appears as lower and upper. +# The letter L appears after E, so the L is the greatest english letter. +# Example 2 +# Input: $str = 'ChaLlenge' +# Output: L +# Example 3 +# Input: $str = 'The' +# Output: '' + +use Modern::Perl; + +say greatest_letter(shift // ''); + +sub greatest_letter { + my($word) = @_; + for my $upper (reverse 'A' .. 'Z') { + my $lower = lc($upper); + return $upper if $word =~ /$upper/ && $word =~ /$lower/; + } + return "''"; +} diff --git a/challenge-264/paulo-custodio/perl/ch-2.pl b/challenge-264/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..0a4a4ec9f8 --- /dev/null +++ b/challenge-264/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# Challenge 264 +# +# Task 2: Target Array +# Submitted by: Mohammad Sajid Anwar +# You are given two arrays of integers, @source and @indices. The @indices can +# only contains integers 0 <= i < size of @source. +# +# Write a script to create target array by insert at index $indices[i] the +# value $source[i]. +# +# Example 1 +# Input: @source = (0, 1, 2, 3, 4) +# @indices = (0, 1, 2, 2, 1) +# Output: (0, 4, 1, 3, 2) +# +# @source @indices @target +# 0 0 (0) +# 1 1 (0, 1) +# 2 2 (0, 1, 2) +# 3 2 (0, 1, 3, 2) +# 4 1 (0, 4, 1, 3, 2) +# Example 2 +# Input: @source = (1, 2, 3, 4, 0) +# @indices = (0, 1, 2, 3, 0) +# Output: (0, 1, 2, 3, 4) +# +# @source @indices @target +# 1 0 (1) +# 2 1 (1, 2) +# 3 2 (1, 2, 3) +# 4 3 (1, 2, 3, 4) +# 0 0 (0, 1, 2, 3, 4) +# Example 3 +# Input: @source = (1) +# @indices = (0) +# Output: (1) + +use Modern::Perl; + +my($source, $indices) = split /,/, "@ARGV"; +my @source = split ' ', $source; +my @indices = split ' ', $indices; + +my @result; +for (0 .. $#source) { + splice(@result, $indices[$_], 0, $source[$_]); +} + +say "@result"; diff --git a/challenge-264/paulo-custodio/t/test-1.yaml b/challenge-264/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f670be05a8 --- /dev/null +++ b/challenge-264/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: PeRlwEeKLy + input: + output: L +- setup: + cleanup: + args: ChaLlenge + input: + output: L +- setup: + cleanup: + args: The + input: + output: "''" diff --git a/challenge-264/paulo-custodio/t/test-2.yaml b/challenge-264/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..c3ccc99e99 --- /dev/null +++ b/challenge-264/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 0 1 2 3 4 , 0 1 2 2 1 + input: + output: 0 4 1 3 2 +- setup: + cleanup: + args: 1 2 3 4 0 , 0 1 2 3 0 + input: + output: 0 1 2 3 4 +- setup: + cleanup: + args: 1 , 0 + input: + output: 1 diff --git a/challenge-265/paulo-custodio/Makefile b/challenge-265/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-265/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-265/paulo-custodio/perl/ch-1.pl b/challenge-265/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..f7e0a707bd --- /dev/null +++ b/challenge-265/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# Challenge 265 +# +# Task 1: 33% Appearance +# Submitted by: Mohammad Sajid Anwar +# You are given an array of integers, @ints. +# +# Write a script to find an integer in the given array that appeared 33% or more. +# If more than one found, return the smallest. If none found then return undef. +# +# Example 1 +# Input: @ints = (1,2,3,3,3,3,4,2) +# Output: 3 +# +# 1 appeared 1 times. +# 2 appeared 2 times. +# 3 appeared 4 times. +# +# 3 appeared 50% (>33%) in the given array. +# Example 2 +# Input: @ints = (1,1) +# Output: 1 +# +# 1 appeared 2 times. +# +# 1 appeared 100% (>33%) in the given array. +# Example 3 +# Input: @ints = (1,2,3) +# Output: 1 +# +# 1 appeared 1 times. +# 2 appeared 1 times. +# 3 appeared 1 times. +# +# Since all three appeared 33.3% (>33%) in the given array. +# We pick the smallest of all. + +use Modern::Perl; + +my @ints = @ARGV; +say freq_33(@ints); + +sub freq_33 { + my(@ints) = @_; + my %count; $count{$_}++ for @ints; + for my $n (sort {$a <=> $b} keys %count) { + return $n if $count{$n}/scalar(@ints) >= 1/3; + } + return "''"; +} diff --git a/challenge-265/paulo-custodio/perl/ch-2.pl b/challenge-265/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..1e44c8679b --- /dev/null +++ b/challenge-265/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl + +# Challenge 265 +# +# Task 2: Completing Word +# Submitted by: Mohammad Sajid Anwar +# You are given a string, $str containing alphnumeric characters and array of +# strings (alphabetic characters only), @str. +# +# Write a script to find the shortest completing word. If none found return +# empty string. +# +# A completing word is a word that contains all the letters in the given string, +# ignoring space and number. If a letter appeared more than once in the given +# string then it must appear the same number or more in the word. +# +# Example 1 +# Input: $str = 'aBc 11c' +# @str = ('accbbb', 'abc', 'abbc') +# Output: 'accbbb' +# +# The given string contains following, ignoring case and number: +# a 1 times +# b 1 times +# c 2 times +# +# The only string in the given array that satisfies the condition is 'accbbb'. +# Example 2 +# Input: $str = 'Da2 abc' +# @str = ('abcm', 'baacd', 'abaadc') +# Output: 'baacd' +# +# The given string contains following, ignoring case and number: +# a 2 times +# b 1 times +# c 1 times +# d 1 times +# +# The are 2 strings in the given array that satisfies the condition: +# 'baacd' and 'abaadc'. +# +# Shortest of the two is 'baacd' +# Example 3 +# Input: $str = 'JB 007' +# @str = ('jj', 'bb', 'bjb') +# Output: 'bjb' +# +# The given string contains following, ignoring case and number: +# j 1 times +# b 1 times +# +# The only string in the given array that satisfies the condition is 'bjb'. + +use Modern::Perl; + +my($str, @words) = @ARGV; + +my @result = + sort {length($a) <=> length($b)} + grep {is_completing($str, $_)} @words; +say @result ? $result[0] : "''"; + + +sub is_completing { + my($str, $word) = @_; + $str =~ s/[^a-z]//gi; + my %count; $count{lc($_)}++ for split //, $str; + $word = join '', sort split //, $word; + while (my($ch, $count) = each %count) { + my $code = '$word =~ s/'.$ch.'{'.$count.',}//i'; + eval($code) or return 0; + } + return $word eq ''; +} diff --git a/challenge-265/paulo-custodio/t/test-1.yaml b/challenge-265/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..47d9b783b7 --- /dev/null +++ b/challenge-265/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 3 3 3 3 4 2 + input: + output: 3 +- setup: + cleanup: + args: 1 1 + input: + output: 1 +- setup: + cleanup: + args: 1 2 3 + input: + output: 1 diff --git a/challenge-265/paulo-custodio/t/test-2.yaml b/challenge-265/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..d987374fae --- /dev/null +++ b/challenge-265/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: '"aBc 11c" accbbb abc abbc' + input: + output: accbbb +- setup: + cleanup: + args: '"Da2 abc" abcm baacd abaadc' + input: + output: baacd +- setup: + cleanup: + args: '"JB 007" jj bb bjb' + input: + output: bjb |
