diff options
| -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 |
5 files changed, 157 insertions, 0 deletions
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 |
