-
-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
126 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |