Skip to content

Commit

Permalink
Add atbash-cipher generator (#260)
Browse files Browse the repository at this point in the history
* Add atbash-cipher generator
  • Loading branch information
ErikSchierboom authored Mar 20, 2017
1 parent 03cfdb5 commit 76ae0b8
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 43 deletions.
11 changes: 8 additions & 3 deletions exercises/atbash-cipher/AtbashCipher.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System;

public static class Atbash
public static class AtbashCipher
{
public static string Encode(string value)
public static string Encode(string plainValue)
{
throw new NotImplementedException("You need to implement this function.");
}
}

public static string Decode(string encodedValue)
{
throw new NotImplementedException("You need to implement this function.");
}
}
76 changes: 76 additions & 0 deletions exercises/atbash-cipher/AtbashCipherTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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"));
}
}
17 changes: 0 additions & 17 deletions exercises/atbash-cipher/AtbashTest.cs

This file was deleted.

45 changes: 22 additions & 23 deletions exercises/atbash-cipher/Example.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;

public static class Atbash
public static class AtbashCipher
{
private const string PLAIN = "abcdefghijklmnopqrstuvwxyz";
private const string CIPHER = "zyxwvutsrqponmlkjihgfedcba";
private const int BlockSize = 5;
private const string Plain = "abcdefghijklmnopqrstuvwxyz0123456789";
private const string Cipher = "zyxwvutsrqponmlkjihgfedcba0123456789";

public static string Encode(string value)
{
var encoded = string.Concat(StripInvalidCharacters(value).ToLower().Select(ApplyCipher));
return SplitIntoFiveLetterWords(encoded);
}
public static string Encode(string plainValue)
=> string.Join(" ", EncodeInBlocks(GetEncodedCharacters(plainValue)));

private static string StripInvalidCharacters(string value)
{
return string.Concat(value.Where(char.IsLetterOrDigit));
}
public static string Decode(string encodedValue)
=> new string(encodedValue.Replace(" ", "").Select(Decode).ToArray());

private static char ApplyCipher(char value)
{
var idx = PLAIN.IndexOf(value);
return idx >= 0 ? CIPHER[idx] : value;
}
private static IEnumerable<char> GetEncodedCharacters(string words)
=> GetValidCharacters(words).Select(Encode);

private static string SplitIntoFiveLetterWords(string value)
{
var words = new List<string>();
private static IEnumerable<char> GetValidCharacters(string words)
=> words.ToLowerInvariant().Where(char.IsLetterOrDigit);

private static char Encode(char c) => Cipher[Plain.IndexOf(c)];

for (int i = 0; i < value.Length; i += 5)
words.Add(i + 5 <= value.Length ? value.Substring(i, 5) : value.Substring(i));
private static char Decode(char c) => Plain[Cipher.IndexOf(c)];

private static IEnumerable<string> EncodeInBlocks(IEnumerable<char> value)
{
var valueAsString = new string(value.ToArray());

return string.Join(" ", words);
for (var i = 0; i < valueAsString.Length; i += BlockSize)
yield return valueAsString.Substring(i, Math.Min(BlockSize, valueAsString.Length - i));
}
}
20 changes: 20 additions & 0 deletions generators/Exercises/AtbashCipherExercise.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Generators.Data;
using Generators.Methods;

namespace Generators.Exercises
{
public class AtbashCipherExercise : EqualityExercise
{
public AtbashCipherExercise() : base("atbash-cipher")
{
}

protected override TestMethodOptions CreateTestMethodOptions(CanonicalData canonicalData, CanonicalDataCase canonicalDataCase, int index)
{
var testMethodOptions = base.CreateTestMethodOptions(canonicalData, canonicalDataCase, index);
testMethodOptions.InputProperty = "phrase";

return testMethodOptions;
}
}
}

0 comments on commit 76ae0b8

Please sign in to comment.