Avatar of 4d47
0
2
Genius
0
0

4d47's solution

to Atbash Cipher in the Perl 6 Track

0
2
Genius
0
0
Instructions
Test suite
Solution

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards. The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba

It is a very weak cipher because it only has one possible key, and it is a simple monoalphabetic substitution cipher. However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.

Examples

  • Encoding test gives gvhg
  • Decoding gvhg gives test
  • Decoding gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt gives thequickbrownfoxjumpsoverthelazydog

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove6 . in the exercise directory.

Source

Wikipedia http://en.wikipedia.org/wiki/Atbash

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

atbash-cipher.t

#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use AtbashCipher;
plan 12;

my $c-data = from-json $=pod.pop.contents;
is .<input><phrase>.&::(.<property>), |.<expected description> for $c-data<cases>»<cases>».Array.flat;

=head2 Canonical Data
=begin code
{
  "exercise": "atbash-cipher",
  "version": "1.1.0",
  "comments": [
    "The tests are divided into two groups: ",
    "* Encoding from English to atbash cipher",
    "* Decoding from atbash cipher to all-lowercase-mashed-together English"
  ],
  "cases": [
    {
      "description": "encode",
      "comments": [ "Test encoding from English to atbash" ],
      "cases": [
        {
          "description": "encode yes",
          "property": "encode",
          "input": {
            "phrase": "yes"
          },
          "expected": "bvh"
        },
        {
          "description": "encode no",
          "property": "encode",
          "input": {
            "phrase": "no"
          },
          "expected": "ml"
        },
        {
          "description": "encode OMG",
          "property": "encode",
          "input": {
            "phrase": "OMG"
          },
          "expected": "lnt"
        },
        {
          "description": "encode spaces",
          "property": "encode",
          "input": {
            "phrase": "O M G"
          },
          "expected": "lnt"
        },
        {
          "description": "encode mindblowingly",
          "property": "encode",
          "input": {
            "phrase": "mindblowingly"
          },
          "expected": "nrmwy oldrm tob"
        },
        {
          "description": "encode numbers",
          "property": "encode",
          "input": {
            "phrase": "Testing,1 2 3, testing."
          },
          "expected": "gvhgr mt123 gvhgr mt"
        },
        {
          "description": "encode deep thought",
          "property": "encode",
          "input": {
            "phrase": "Truth is fiction."
          },
          "expected": "gifgs rhurx grlm"
        },
        {
          "description": "encode all the letters",
          "property": "encode",
          "input": {
            "phrase": "The quick brown fox jumps over the lazy dog."
          },
          "expected": "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
        }
      ]
    },
    {
      "description": "decode",
      "comments": [ "Test decoding from atbash to English" ],
      "cases": [
        {
          "description": "decode exercism",
          "property": "decode",
          "input": {
            "phrase": "vcvix rhn"
          },
          "expected": "exercism"
        },
        {
          "description": "decode a sentence",
          "property": "decode",
          "input": {
            "phrase": "zmlyh gzxov rhlug vmzhg vkkrm thglm v"
          },
          "expected": "anobstacleisoftenasteppingstone"
        },
        {
          "description": "decode numbers",
          "property": "decode",
          "input": {
            "phrase": "gvhgr mt123 gvhgr mt"
          },
          "expected": "testing123testing"
        },
        {
          "description": "decode all the letters",
          "property": "decode",
          "input": {
            "phrase": "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
          },
          "expected": "thequickbrownfoxjumpsoverthelazydog"
        }
      ]
    }
  ]
}
=end code
sub encode(Str $phrase --> Str) is export {
    $phrase.&clean.trans(['a'..'z'] => ['z'...'a']).comb(5).Str
}

sub decode(Str $phrase --> Str) is export {
    $phrase.trans(['z'...'a'] => ['a'..'z']).&clean
}

sub clean(Str $phrase --> Str) {
    $phrase.lc.subst(/<-[a..z 0..9]>/, '', :g)
}

What can you learn from this solution?

A huge amount can be learnt from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that I could read more about to develop my understanding?