Skip to content

Instantly share code, notes, and snippets.

@JFFail
Created November 23, 2015 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JFFail/fd5dfabf87440e0892bd to your computer and use it in GitHub Desktop.
Save JFFail/fd5dfabf87440e0892bd to your computer and use it in GitHub Desktop.
Reddit Daily Programmer #240 Easy - Typoglycemia
using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
namespace Typoglycemia
{
class Program
{
static void Main(string[] args)
{
//Instantiate some stuff.
string first, last, middle, final;
bool checkCharacter;
bool isFirst = true;
int randomNumber, counter, index;
Regex specials = new Regex(@"[a-zA-Z0-9]");
ArrayList arrList = new ArrayList();
ArrayList indexList = new ArrayList();
DateTime current = new DateTime();
Random randNum = new Random(current.Millisecond);
//First import the text file.
StreamReader readFile = new StreamReader("words.txt");
string line = "";
final = "";
while(line != null)
{
//Get each line from the text file.
line = readFile.ReadLine();
//Get out of this if null.
if(line == null)
{
continue;
}
//Split the line into an array of words.
string[] words = line.Split(new char[] { ' ' });
//Operate on each word.
foreach(string word in words)
{
//Reset some instance variables.
first = "";
last = "";
arrList.Clear();
//See how long the word is, since less than 3 letters doesn't even matter.
if (word.Length <= 3)
{
if (isFirst)
{
final = word;
isFirst = false;
}
else
{
final = final + " " + word;
}
}
else
{
//Split the word into a character array.
char[] tempArray = word.ToCharArray();
//Populate the ArrayList
foreach (char letter in tempArray)
{
arrList.Add(letter.ToString());
}
//Figure out the set of stuff for the opening string.
foreach (string item in arrList)
{
checkCharacter = specials.IsMatch(item);
first += item;
if (checkCharacter)
{
break;
}
}
//Remove those items from the array list.
char[] firstRemoveArray = first.ToCharArray();
foreach (char item in firstRemoveArray)
{
arrList.Remove(item.ToString());
}
//Figure out stuff for the closing string.
for (int i = (arrList.Count - 1); i > -1; i--)
{
checkCharacter = specials.IsMatch(arrList[i].ToString());
last = arrList[i].ToString() + last;
if (checkCharacter)
{
break;
}
}
//Remove those items from the array list.
char[] lastRemoveArray = last.ToCharArray();
foreach (char item in lastRemoveArray)
{
arrList.RemoveAt(arrList.Count - 1);
}
//Figure out if we have enough characters left to do anything or not.
if (arrList.Count <= 1)
{
if (isFirst)
{
final = word;
isFirst = false;
}
else
{
final = final + " " + word;
}
}
else
{
//Make a new ArrayList to house the possible index values.
indexList.Clear();
for (int i = 0; i < arrList.Count; i++)
{
indexList.Add(i);
}
//Start to scramble shit.
middle = "";
counter = 0;
while (counter < arrList.Count)
{
//Generate a random number.
randomNumber = randNum.Next(0, indexList.Count);
index = Convert.ToInt32(indexList[randomNumber]);
middle += arrList[index];
indexList.Remove(index);
counter++;
}
//Add to the "final" string that is the result.
if (isFirst)
{
final = first + middle + last;
isFirst = false;
}
else
{
final = final + " " + first + middle + last;
}
}
}
}
}
//Write the final string.
Console.WriteLine(final);
//Close the file.
readFile.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment