diff options
| -rw-r--r-- | challenge-212/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-212/paulo-custodio/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-212/paulo-custodio/perl/ch-2.pl | 79 | ||||
| -rw-r--r-- | challenge-212/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-212/paulo-custodio/t/test-2.yaml | 20 |
5 files changed, 158 insertions, 0 deletions
diff --git a/challenge-212/paulo-custodio/Makefile b/challenge-212/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-212/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-212/paulo-custodio/perl/ch-1.pl b/challenge-212/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..cdf5e4a0e9 --- /dev/null +++ b/challenge-212/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +# Challenge 212 +# +# Task 1: Jumping Letters +# Submitted by: Mohammad S Anwar +# +# You are given a word having alphabetic characters only, and a list of positive +# integers of the same length +# +# Write a script to print the new word generated after jumping forward each +# letter in the given word by the integer in the list. The given list would have +# exactly the number as the total alphabets in the given word. +# +# Example 1 +# +# Input: $word = 'Perl' and @jump = (2,22,19,9) +# Output: Raku +# +# 'P' jumps 2 place forward and becomes 'R'. +# 'e' jumps 22 place forward and becomes 'a'. (jump is cyclic i.e. after 'z' you go back to 'a') +# 'r' jumps 19 place forward and becomes 'k'. +# 'l' jumps 9 place forward and becomes 'u'. +# +# Example 2 +# +# Input: $word = 'Raku' and @jump = (24,4,7,17) +# Output: 'Perl' + +use Modern::Perl; + +sub encode { + my($word, @jumps) = @_; + my @word = split //, $word; + for my $i (0..$#word) { + if ($word[$i] =~ /[a-z]/) { + $word[$i] = chr(((ord($word[$i])-ord('a') + $jumps[$i]) % 26) + ord('a')); + } + elsif ($word[$i] =~ /[A-Z]/) { + $word[$i] = chr(((ord($word[$i])-ord('A') + $jumps[$i]) % 26) + ord('A')); + } + } + return join "", @word; +} + +@ARGV or die "usage: ch-1.pl word jumps...\n"; +say encode(@ARGV); diff --git a/challenge-212/paulo-custodio/perl/ch-2.pl b/challenge-212/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b2af1c85af --- /dev/null +++ b/challenge-212/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,79 @@ +#!/usr/bin/perl + +# Challenge 212 +# +# Task 2: Rearrange Groups +# Submitted by: Mohammad S Anwar +# +# You are given a list of integers and group size greater than zero. +# +# Write a script to split the list into equal groups of the given size where +# integers are in sequential order. If it can’t be done then print -1. +# +# Example 1: +# +# Input: @list = (1,2,3,5,1,2,7,6,3) and $size = 3 +# Output: (1,2,3), (1,2,3), (5,6,7) +# +# Example 2: +# +# Input: @list = (1,2,3) and $size = 2 +# Output: -1 +# +# Example 3: +# +# Input: @list = (1,2,4,3,5,3) and $size = 2 +# Output: (1,2,3), (3,4,5) +# +# Example 4: +# +# Input: @list = (1,5,2,6,4,7) and $size = 3 +# Output: -1 + +use Modern::Perl; + +sub rearrange_groups { + my($count, @nums) = @_; + return -1 unless scalar(@nums) % $count == 0; + my $group_size = scalar(@nums) / $count; + @nums = sort {$a<=>$b} @nums; + my @output; + for my $i (0..$count-1) { + my @group; + my %seen; + my $j = 0; + while (scalar(@group) < $group_size) { + if (!$seen{$nums[$j]}++) { + push @group, $nums[$j]; + splice(@nums, $j, 1); + } + else { + $j++; + if ($j >= @nums) { + return -1; + } + } + } + push @output, \@group; + } + return @output; +} + +sub print_groups { + my(@output) = @_; + if (@output==1 && !ref($output[0])) { + say $output[0]; + } + else { + my $sep = ""; + for (@output) { + print $sep,"(",join(",", @$_),")"; + $sep = ", "; + } + print "\n"; + } +} + +my @nums = @ARGV; +my $count = pop(@nums); +print_groups(rearrange_groups($count, @nums)); diff --git a/challenge-212/paulo-custodio/t/test-1.yaml b/challenge-212/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..512ffaa14f --- /dev/null +++ b/challenge-212/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: Perl 2 22 19 9 + input: + output: Raku +- setup: + cleanup: + args: Raku 24 4 7 17 + input: + output: Perl diff --git a/challenge-212/paulo-custodio/t/test-2.yaml b/challenge-212/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..2e4b7e2d4c --- /dev/null +++ b/challenge-212/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 1 2 3 5 1 2 7 6 3 3 + input: + output: (1,2,3), (1,2,3), (5,6,7) +- setup: + cleanup: + args: 1 2 3 2 + input: + output: -1 +- setup: + cleanup: + args: 1 2 4 3 5 3 2 + input: + output: (1,2,3), (3,4,5) +- setup: + cleanup: + args: 1 5 2 6 4 7 3 + input: + output: (1,2), (4,5), (6,7) |
