aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-212/solathian/perl/ch-1.pl44
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-212/solathian/perl/ch-1.pl b/challenge-212/solathian/perl/ch-1.pl
new file mode 100644
index 0000000000..7a29614b37
--- /dev/null
+++ b/challenge-212/solathian/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!usr/bin/perl
+use v5.36;
+
+# Challenge 212 - 1 - Jumping Letters
+# 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.
+my $range = ord('z') - ord('a') +1;
+jump('Perl', (2, 22, 19, 9)); # Output: Raku
+jump('Raku', (24, 4, 7, 17)); # Output: Perl
+
+
+sub jump($string, @jump)
+{
+ my @stringArray = split('', $string);
+
+ die "String and jumparray does not have the same length" if(@stringArray != @jump);
+
+ foreach my $char (@stringArray)
+ {
+ my $jump = shift @jump;
+ my $newChar;
+
+ if( ord('a') <= ord($char) <= ord('z') )
+ {
+ my $offset = (ord($char) - ord('a') + $jump) % $range;
+ $newChar = chr(ord('a') + $offset);
+ }
+ elsif( ord('A') <= ord($char) <= ord('Z') )
+ {
+ my $offset = (ord($char) - ord('A') + $jump) % $range;
+ $newChar = chr(ord('A') + $offset);
+ }
+ else
+ {
+ die "Character is not in the range of a-zA-Z!"
+ }
+
+ print $newChar;
+ }
+
+ say "";
+} \ No newline at end of file