Created
November 21, 2014 09:52
-
-
Save eriksk/7e5e549e40a7ceb35309 to your computer and use it in GitHub Desktop.
String scrambler
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
class StringScrambler | |
{ | |
private readonly char[] _scrambleLetters; | |
private Random rand = new Random(); | |
public StringScrambler(char[] scrambleLetters) | |
{ | |
if (scrambleLetters == null) throw new ArgumentNullException("scrambleLetters"); | |
_scrambleLetters = scrambleLetters; | |
} | |
public string Scramble(string source, float percent) | |
{ | |
var lettersToScramble = (int) (source.Length*percent); | |
var builder = new StringBuilder(source); | |
for (int i = lettersToScramble; i < source.Length; i++) | |
builder[i] = GetRandomChar(builder[i]); | |
return builder.ToString(); | |
} | |
private char GetRandomChar(char c) | |
{ | |
if (_scrambleLetters.Contains(c)) | |
{ | |
int index = _scrambleLetters.ToList().IndexOf(c) + rand.Next(6); | |
if (index > _scrambleLetters.Length - 1) | |
index = 0; | |
return _scrambleLetters[index]; | |
} | |
return _scrambleLetters[rand.Next(_scrambleLetters.Length)]; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var letters = new[] | |
{ | |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', | |
'V', 'W', 'X', 'Y', 'Z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', | |
'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '[', ']', '(', ')', '.', '-', | |
',' | |
}; | |
while (true) | |
{ | |
Console.Write(">"); | |
string input = Console.ReadLine(); | |
float current = 0; | |
float duration = 50 * input.Length; | |
float stepDuration = (input.Length/duration) * 1000f; | |
while (current < duration) | |
{ | |
if (input.Length == 0) break; | |
current += stepDuration; | |
Thread.Sleep((int)stepDuration); | |
Console.Clear(); | |
Console.WriteLine(current / duration); | |
Console.WriteLine(new StringScrambler(letters).Scramble(input, current / duration)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment