diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-04-16 23:05:48 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-16 23:05:48 +0100 |
| commit | 89b6dc12c13cfdb078b6bb27d685a47431303a9f (patch) | |
| tree | 4204be61ae87f73101297503c93c9ef6b7abc1c3 | |
| parent | 5d6d07db1d14c7ac461ba506b0c55410bc1255c3 (diff) | |
| parent | fd3c5bffb1784d8c28531c2660f8cd5c4d6fcf4d (diff) | |
| download | perlweeklychallenge-club-89b6dc12c13cfdb078b6bb27d685a47431303a9f.tar.gz perlweeklychallenge-club-89b6dc12c13cfdb078b6bb27d685a47431303a9f.tar.bz2 perlweeklychallenge-club-89b6dc12c13cfdb078b6bb27d685a47431303a9f.zip | |
Merge pull request #7911 from Solathian/branch-for-challenge-212
Adding file for challenge
| -rw-r--r-- | challenge-212/solathian/perl/ch-1.pl | 44 |
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 |
