diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2021-01-30 10:38:41 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2021-01-30 12:43:43 +0100 |
| commit | e9c74676c298b1c5ae8245a2b143528dd1a10026 (patch) | |
| tree | 97d799c9964f4b206b29018355b6066e6a8c664b | |
| parent | 057ae84b2540d506fcdafaa19692538f7c2e18d5 (diff) | |
| download | perlweeklychallenge-club-e9c74676c298b1c5ae8245a2b143528dd1a10026.tar.gz perlweeklychallenge-club-e9c74676c298b1c5ae8245a2b143528dd1a10026.tar.bz2 perlweeklychallenge-club-e9c74676c298b1c5ae8245a2b143528dd1a10026.zip | |
Task1 Chal 097 Perl LK
| -rw-r--r-- | challenge-097/lubos-kolouch/perl/ch-1.pl | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-097/lubos-kolouch/perl/ch-1.pl b/challenge-097/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..db63827bc7 --- /dev/null +++ b/challenge-097/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: Perl Weekly Challenge 097 +# Task 1 +# Caesar Cipher +# +# AUTHOR: Lubos Kolouch +# CREATED: 01/30/2021 10:11:26 AM +#=============================================================================== + +use strict; +use warnings; +use feature qw/say/; + +sub caesar_cipher { + my $what = shift; + + my $inp = $what->[0]; + my $shift = $what->[1]; + + my $result = ''; + + for (split '', $inp) { + + if (((ord($_) < ord('A')) or (ord($_) > ord('Z')))) { + $result .= $_; + next; + } + + my $translated = ord($_) - $shift; + + $translated = ord('Z') - ord('A') + $translated + 1 if $translated < ord('A'); + + $result .= chr($translated); + } + + return $result; +} + +use Test::More; + +is(caesar_cipher(['THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG',3]),'QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD'); +is(caesar_cipher(['ABCDEFGHIJKLMNOPQRSTUVWXYZ', 3]), 'XYZABCDEFGHIJKLMNOPQRSTUVW'); + +done_testing; |
