Avatar of artemkorsakov

artemkorsakov's solution

to Rotational Cipher in the C# Track

Published at Apr 10 2019 · 0 comments
Instructions
Test suite
Solution

Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.

The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between 0 and 26. Using a key of 0 or 26 will always yield the same output due to modular arithmetic. The letter is shifted for as many values as the value of the key.

The general notation for rotational ciphers is ROT + <key>. The most commonly used rotational cipher is ROT13.

A ROT13 on the Latin alphabet would be as follows:

Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm

It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.

Ciphertext is written out in the same formatting as the input including spaces and punctuation.

Examples

  • ROT5 omg gives trl
  • ROT0 c gives c
  • ROT26 Cool gives Cool
  • ROT13 The quick brown fox jumps over the lazy dog. gives Gur dhvpx oebja sbk whzcf bire gur ynml qbt.
  • ROT13 Gur dhvpx oebja sbk whzcf bire gur ynml qbt. gives The quick brown fox jumps over the lazy dog.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit RotationalCipher.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

Wikipedia https://en.wikipedia.org/wiki/Caesar_cipher

RotationalCipherTest.cs

// This file was auto-generated based on version 1.2.0 of the canonical data.

using Xunit;

public class RotationalCipherTest
{
    [Fact]
    public void Rotate_a_by_0_same_output_as_input()
    {
        Assert.Equal("a", RotationalCipher.Rotate("a", 0));
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotate_a_by_1()
    {
        Assert.Equal("b", RotationalCipher.Rotate("a", 1));
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotate_a_by_26_same_output_as_input()
    {
        Assert.Equal("a", RotationalCipher.Rotate("a", 26));
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotate_m_by_13()
    {
        Assert.Equal("z", RotationalCipher.Rotate("m", 13));
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotate_n_by_13_with_wrap_around_alphabet()
    {
        Assert.Equal("a", RotationalCipher.Rotate("n", 13));
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotate_capital_letters()
    {
        Assert.Equal("TRL", RotationalCipher.Rotate("OMG", 5));
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotate_spaces()
    {
        Assert.Equal("T R L", RotationalCipher.Rotate("O M G", 5));
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotate_numbers()
    {
        Assert.Equal("Xiwxmrk 1 2 3 xiwxmrk", RotationalCipher.Rotate("Testing 1 2 3 testing", 4));
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotate_punctuation()
    {
        Assert.Equal("Gzo'n zvo, Bmviyhv!", RotationalCipher.Rotate("Let's eat, Grandma!", 21));
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotate_all_letters()
    {
        Assert.Equal("Gur dhvpx oebja sbk whzcf bire gur ynml qbt.", RotationalCipher.Rotate("The quick brown fox jumps over the lazy dog.", 13));
    }
}
using System.Linq;

public static class RotationalCipher
{
    public static string Rotate(string text, int shiftKey) =>
        string.Concat(text.Select(c => c.Rotate(shiftKey)));

    private static char Rotate(this char letter, int shiftkey)
    {
        if (!char.IsLetter(letter))
        {
            return letter;
        }

        char firstLetter = char.IsUpper(letter) ? 'A' : 'a';
        int pos = firstLetter + (letter - firstLetter + shiftkey) % 26;
        return (char)pos;
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned 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 you could read more about to improve your understanding?