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.
test
gives gvhg
gvhg
gives test
gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt
gives thequickbrownfoxjumpsoverthelazydog
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 AtbashCipher.cs
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.
Wikipedia http://en.wikipedia.org/wiki/Atbash
// This file was auto-generated based on version 1.2.0 of the canonical data.
using Xunit;
public class AtbashCipherTest
{
[Fact]
public void Encode_yes()
{
Assert.Equal("bvh", AtbashCipher.Encode("yes"));
}
[Fact(Skip = "Remove to run test")]
public void Encode_no()
{
Assert.Equal("ml", AtbashCipher.Encode("no"));
}
[Fact(Skip = "Remove to run test")]
public void Encode_omg()
{
Assert.Equal("lnt", AtbashCipher.Encode("OMG"));
}
[Fact(Skip = "Remove to run test")]
public void Encode_spaces()
{
Assert.Equal("lnt", AtbashCipher.Encode("O M G"));
}
[Fact(Skip = "Remove to run test")]
public void Encode_mindblowingly()
{
Assert.Equal("nrmwy oldrm tob", AtbashCipher.Encode("mindblowingly"));
}
[Fact(Skip = "Remove to run test")]
public void Encode_numbers()
{
Assert.Equal("gvhgr mt123 gvhgr mt", AtbashCipher.Encode("Testing,1 2 3, testing."));
}
[Fact(Skip = "Remove to run test")]
public void Encode_deep_thought()
{
Assert.Equal("gifgs rhurx grlm", AtbashCipher.Encode("Truth is fiction."));
}
[Fact(Skip = "Remove to run test")]
public void Encode_all_the_letters()
{
Assert.Equal("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt", AtbashCipher.Encode("The quick brown fox jumps over the lazy dog."));
}
[Fact(Skip = "Remove to run test")]
public void Decode_exercism()
{
Assert.Equal("exercism", AtbashCipher.Decode("vcvix rhn"));
}
[Fact(Skip = "Remove to run test")]
public void Decode_a_sentence()
{
Assert.Equal("anobstacleisoftenasteppingstone", AtbashCipher.Decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v"));
}
[Fact(Skip = "Remove to run test")]
public void Decode_numbers()
{
Assert.Equal("testing123testing", AtbashCipher.Decode("gvhgr mt123 gvhgr mt"));
}
[Fact(Skip = "Remove to run test")]
public void Decode_all_the_letters()
{
Assert.Equal("thequickbrownfoxjumpsoverthelazydog", AtbashCipher.Decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"));
}
[Fact(Skip = "Remove to run test")]
public void Decode_with_too_many_spaces()
{
Assert.Equal("exercism", AtbashCipher.Decode("vc vix r hn"));
}
[Fact(Skip = "Remove to run test")]
public void Decode_with_no_spaces()
{
Assert.Equal("anobstacleisoftenasteppingstone", AtbashCipher.Decode("zmlyhgzxovrhlugvmzhgvkkrmthglmv"));
}
}
using System;
using System.Collections.Generic;
using System.Linq;
public static class AtbashCipher
{
private const int BlockSize = 5;
private const string Plain = "abcdefghijklmnopqrstuvwxyz0123456789";
private const string Cipher = "zyxwvutsrqponmlkjihgfedcba0123456789";
public static string Encode(string plainValue)
=> string.Join(" ", EncodeInBlocks(GetEncodedCharacters(plainValue)));
public static string Decode(string encodedValue)
=> new string(encodedValue.Replace(" ", "").Select(Decode).ToArray());
private static IEnumerable<char> GetEncodedCharacters(string words)
=> GetValidCharacters(words).Select(Encode);
private static IEnumerable<char> GetValidCharacters(string words)
=> words.ToLowerInvariant().Where(char.IsLetterOrDigit);
private static char Encode(char c) => Cipher[Plain.IndexOf(c)];
private static char Decode(char c) => Plain[Cipher.IndexOf(c)];
private static IEnumerable<string> EncodeInBlocks(IEnumerable<char> value)
{
var valueAsString = new string(value.ToArray());
for (var i = 0; i < valueAsString.Length; i += BlockSize)
yield return valueAsString.Substring(i, Math.Min(BlockSize, valueAsString.Length - i));
}
}
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.
Level up your programming skills with 3,104 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments