diff options
| -rw-r--r-- | challenge-231/paulo-custodio/perl/ch-1.pl | 1 | ||||
| -rw-r--r-- | challenge-233/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-233/paulo-custodio/perl/ch-1.pl | 54 | ||||
| -rw-r--r-- | challenge-233/paulo-custodio/perl/ch-2.pl | 44 | ||||
| -rw-r--r-- | challenge-233/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-233/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-234/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-234/paulo-custodio/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-234/paulo-custodio/perl/ch-2.pl | 49 | ||||
| -rw-r--r-- | challenge-234/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-234/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-235/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-235/paulo-custodio/perl/ch-1.pl | 51 | ||||
| -rw-r--r-- | challenge-235/paulo-custodio/perl/ch-2.pl | 44 | ||||
| -rw-r--r-- | challenge-235/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-235/paulo-custodio/t/test-2.yaml | 15 |
16 files changed, 384 insertions, 1 deletions
diff --git a/challenge-231/paulo-custodio/perl/ch-1.pl b/challenge-231/paulo-custodio/perl/ch-1.pl index 29ac80dbe6..a643a44c03 100644 --- a/challenge-231/paulo-custodio/perl/ch-1.pl +++ b/challenge-231/paulo-custodio/perl/ch-1.pl @@ -37,4 +37,3 @@ sub min_max { my @ret = grep {$_ != $min && $_ != $max} @n; return @ret ? @ret : -1; } - diff --git a/challenge-233/paulo-custodio/Makefile b/challenge-233/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-233/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-233/paulo-custodio/perl/ch-1.pl b/challenge-233/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..c23066d777 --- /dev/null +++ b/challenge-233/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +# Challenge 233 +# +# Task 1: Similar Words +# Submitted by: Mohammad S Anwar +# +# You are given an array of words made up of alphabets only. +# +# Write a script to find the number of pairs of similar words. Two words are +# similar if they consist of the same characters. +# Example 1 +# +# Input: @words = ("aba", "aabb", "abcd", "bac", "aabc") +# Output: 2 +# +# Pair 1: similar words ("aba", "aabb") +# Pair 2: similar words ("bac", "aabc") +# +# Example 2 +# +# Input: @words = ("aabb", "ab", "ba") +# Output: 3 +# +# Pair 1: similar words ("aabb", "ab") +# Pair 2: similar words ("aabb", "ba") +# Pair 3: similar words ("ab", "ba") +# +# Example 3 +# +# Input: @words = ("nba", "cba", "dba") +# Output: 0 + +use Modern::Perl; + +my @words = @ARGV; +my $count = 0; +for my $i (0 .. $#words-1) { + for my $j ($i+1 .. $#words) { + $count++ if is_similar($words[$i], $words[$j]); + } +} +say $count; + +sub is_similar { + my($a, $b) = @_; + + while ($a ne '' && $b ne '') { + my $ch = substr($a, 0, 1); + $a =~ s/$ch//gi; + $b =~ s/$ch//gi; + } + return $a eq '' && $b eq ''; +} diff --git a/challenge-233/paulo-custodio/perl/ch-2.pl b/challenge-233/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..dfd6ab0875 --- /dev/null +++ b/challenge-233/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# Challenge 233 +# +# Task 2: Frequency Sort +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers. +# +# Write a script to sort the given array in increasing order based on the +# frequency of the values. If multiple values have the same frequency then +# sort them in decreasing order. +# Example 1 +# +# Input: @ints = (1,1,2,2,2,3) +# Ouput: (3,1,1,2,2,2) +# +# '3' has a frequency of 1 +# '1' has a frequency of 2 +# '2' has a frequency of 3 +# +# Example 2 +# +# Input: @ints = (2,3,1,3,2) +# Ouput: (1,3,3,2,2) +# +# '2' and '3' both have a frequency of 2, so they are sorted in decreasing order. +# +# Example 3 +# +# Input: @ints = (-1,1,-6,4,5,-6,1,4,1) +# Ouput: (5,-1,4,4,-6,-6,1,1,1) + +use Modern::Perl; + +my @ints = @ARGV; +my %count; +for (@ints) { + $count{$_}++; +} +say join " ", + map {$_->[0]} + sort {$a->[1] == $b->[1] ? $b->[0] <=> $a->[0] : $a->[1] <=> $b->[1]} + map {[$_, $count{$_}]} @ints; diff --git a/challenge-233/paulo-custodio/t/test-1.yaml b/challenge-233/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..fa5648a760 --- /dev/null +++ b/challenge-233/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: aba aabb abcd bac aabc + input: + output: 2 +- setup: + cleanup: + args: aabb ab ba + input: + output: 3 +- setup: + cleanup: + args: aabb ab ba + input: + output: 3 diff --git a/challenge-233/paulo-custodio/t/test-2.yaml b/challenge-233/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..d16f59f7a6 --- /dev/null +++ b/challenge-233/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 1 2 2 2 3 + input: + output: 3 1 1 2 2 2 +- setup: + cleanup: + args: 2 3 1 3 2 + input: + output: 1 3 3 2 2 +- setup: + cleanup: + args: -1 1 -6 4 5 -6 1 4 1 + input: + output: 5 -1 4 4 -6 -6 1 1 1 diff --git a/challenge-234/paulo-custodio/Makefile b/challenge-234/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-234/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-234/paulo-custodio/perl/ch-1.pl b/challenge-234/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..5f8b706d15 --- /dev/null +++ b/challenge-234/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# Challenge 234 +# +# Task 1: Common Characters +# Submitted by: Mohammad S Anwar +# +# You are given an array of words made up of alphabetic characters only. +# +# Write a script to return all alphabetic characters that show up in all words +# including duplicates. +# Example 1 +# +# Input: @words = ("java", "javascript", "julia") +# Output: ("j", "a") +# +# Example 2 +# +# Input: @words = ("bella", "label", "roller") +# Output: ("e", "l", "l") +# +# Example 3 +# +# Input: @words = ("cool", "lock", "cook") +# Output: ("c", "o") + +use Modern::Perl; +use List::Util 'all'; + +my @words = @ARGV; +say join " ", find_common_chars(@words); + +sub find_common_chars { + my(@words) = @_; + my @result; + + while (all {$_ ne ''} @words) { + my $ch = substr($words[0],0,1); + my $count; + for (@words) { + $count += s/$ch//i; + } + push @result, $ch if $count == @words; + } + return @result; +} diff --git a/challenge-234/paulo-custodio/perl/ch-2.pl b/challenge-234/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b4cf19879d --- /dev/null +++ b/challenge-234/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +# Challenge 234 +# +# Task 2: Unequal Triplets +# Submitted by: Mohammad S Anwar +# +# You are given an array of positive integers. +# +# Write a script to find the number of triplets (i, j, k) that satisfies +# num[i] != num[j], num[j] != num[k] and num[k] != num[i]. +# Example 1 +# +# Input: @ints = (4, 4, 2, 4, 3) +# Ouput: 3 +# +# (0, 2, 4) because 4 != 2 != 3 +# (1, 2, 4) because 4 != 2 != 3 +# (2, 3, 4) because 2 != 4 != 3 +# +# Example 2 +# +# Input: @ints = (1, 1, 1, 1, 1) +# Ouput: 0 +# +# Example 3 +# +# Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) +# Output: 28 +# +# triplets of 1, 4, 7 = 3x2×2 = 12 combinations +# triplets of 1, 4, 10 = 3×2×1 = 6 combinations +# triplets of 4, 7, 10 = 2×2×1 = 4 combinations +# triplets of 1, 7, 10 = 3x2x1 = 6 combinations + +use Modern::Perl; + +my @nums = @ARGV; +my $count = 0; +for my $i (0 .. $#nums-2) { + for my $j ($i+1 .. $#nums-1) { + for my $k ($j+1 .. $#nums) { + $count++ if $nums[$i] != $nums[$j] && + $nums[$j] != $nums[$k] && + $nums[$k] != $nums[$i]; + } + } +} +say $count; diff --git a/challenge-234/paulo-custodio/t/test-1.yaml b/challenge-234/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..ba3da310b1 --- /dev/null +++ b/challenge-234/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: java javascript julia + input: + output: j a +- setup: + cleanup: + args: bella label roller + input: + output: e l l +- setup: + cleanup: + args: cool lock cook + input: + output: c o diff --git a/challenge-234/paulo-custodio/t/test-2.yaml b/challenge-234/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..ce2a88f237 --- /dev/null +++ b/challenge-234/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 4 4 2 4 3 + input: + output: 3 +- setup: + cleanup: + args: 1 1 1 1 1 + input: + output: 0 +- setup: + cleanup: + args: 4 7 1 10 7 4 1 1 + input: + output: 28 diff --git a/challenge-235/paulo-custodio/Makefile b/challenge-235/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-235/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-235/paulo-custodio/perl/ch-1.pl b/challenge-235/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..886aa2a188 --- /dev/null +++ b/challenge-235/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# Challenge 235 +# +# Task 1: Remove One +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers. +# +# Write a script to find out if removing ONLY one integer makes it strictly +# increasing order. +# Example 1 +# +# Input: @ints = (0, 2, 9, 4, 6) +# Output: true +# +# Removing ONLY 9 in the given array makes it strictly increasing order. +# +# Example 2 +# +# Input: @ints = (5, 1, 3, 2) +# Output: false +# +# Example 3 +# +# Input: @ints = (2, 2, 3) +# Output: true + +use Modern::Perl; + +my @ints = @ARGV; + +say can_make_strict_increasing_order(@ints) ? "true" : "false"; + +sub can_make_strict_increasing_order { + my(@ints) = @_; + return 1 if !@ints; + for my $i (0..$#ints) { + return 1 if is_strict_increasing(@ints[0..$i-1], @ints[$i+1..$#ints]); + } + return 0; +} + +sub is_strict_increasing { + my(@ints) = @_; + return 1 if !@ints; + for my $i (1..$#ints) { + return 0 if $ints[$i] <= $ints[$i-1]; + } + return 1; +} diff --git a/challenge-235/paulo-custodio/perl/ch-2.pl b/challenge-235/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..147b21b03b --- /dev/null +++ b/challenge-235/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# Challenge 235 +# +# Task 2: Duplicate Zeros +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers. +# +# Write a script to duplicate each occurrence of ZERO in the given array and +# shift the remaining to the right but make sure the size of array remain the same. +# Example 1 +# +# Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0) +# Ouput: (1, 0, 0, 2, 3, 0, 0, 4) +# +# Example 2 +# +# Input: @ints = (1, 2, 3) +# Ouput: (1, 2, 3) +# +# Example 3 +# +# Input: @ints = (0, 3, 0, 4, 5) +# Ouput: (0, 0, 3, 0, 0) + +use Modern::Perl; + +my @ints = @ARGV; +say join " ", dup_zeros(@ints); + +sub dup_zeros { + my(@ints) = @_; + my @result; + for (@ints) { + if ($_==0) { + push @result, 0, 0; + } + else { + push @result, $_; + } + } + return @result[0..$#ints]; +} diff --git a/challenge-235/paulo-custodio/t/test-1.yaml b/challenge-235/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..e33466a56d --- /dev/null +++ b/challenge-235/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 0 2 9 4 6 + input: + output: true +- setup: + cleanup: + args: 5 1 3 2 + input: + output: false +- setup: + cleanup: + args: 2 2 3 + input: + output: true diff --git a/challenge-235/paulo-custodio/t/test-2.yaml b/challenge-235/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..9e74df93de --- /dev/null +++ b/challenge-235/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 0 2 3 0 4 5 0 + input: + output: 1 0 0 2 3 0 0 4 +- setup: + cleanup: + args: 1 2 3 + input: + output: 1 2 3 +- setup: + cleanup: + args: 0 3 0 4 5 + input: + output: 0 0 3 0 0 |
