Created
December 14, 2016 14:31
-
-
Save Xmerr/ecd7942af1642a1771c7f717f93aa7a5 to your computer and use it in GitHub Desktop.
For Reddit Easy Challenge 275
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Splurthian_Chemistry_101 | |
{ | |
class Program | |
{ | |
static Tuple<string, string>[] isValidInputs = | |
{ | |
new Tuple<string, string>("Spenglerium", "Ee"), | |
new Tuple<string, string>("Zeddemorium", "Zr"), | |
new Tuple<string, string>("Venkmine", "Kn"), | |
new Tuple<string, string>("Stantzon", "Zt"), | |
new Tuple<string, string>("Melintzum", "Nn"), | |
new Tuple<string, string>("Tullium", "Ty") | |
}; | |
static string[] makeSymbolsInputs = new string[] | |
{ | |
"Gozerium", | |
"Slimyrine" | |
}; | |
static string[] countSymbolsInput = new string[] | |
{ | |
"Zuulon", | |
"Xenon" | |
}; | |
static void Main(string[] args) | |
{ | |
foreach(var input in isValidInputs) | |
Console.WriteLine(input.Item1 + ", " + input.Item2 + " -> " + IsValid(input.Item1, input.Item2)); | |
Console.ReadKey(); | |
Console.Clear(); | |
foreach (var input in makeSymbolsInputs) | |
Console.WriteLine(input + " -> " + MakeSymbol(input)); | |
Console.ReadKey(); | |
Console.Clear(); | |
foreach (var input in countSymbolsInput) | |
Console.WriteLine(input + " -> " + CountValidSymbols(input)); | |
Console.ReadKey(); | |
Console.Clear(); | |
foreach (var input in countSymbolsInput) | |
Console.WriteLine(input + " -> " + CountValidSymbolsNoRules(input)); | |
Console.ReadKey(); | |
} | |
/// <summary> | |
/// Determines if the passed in | |
/// symbol is valid for the passed in element | |
/// </summary> | |
/// <param name="element"> | |
/// Element to be validated against | |
/// </param> | |
/// <param name="symbol"> | |
/// Symbol to validate | |
/// </param> | |
static bool IsValid(string element, string symbol) | |
{ | |
element = element.ToUpper(); | |
symbol = symbol.ToUpper(); | |
if (symbol.Length != 2) | |
return false; | |
foreach(var c in symbol) | |
{ | |
int loc = element.IndexOf(c); | |
if (loc == -1) | |
return false; | |
element = element.Substring(loc + 1); | |
} | |
return true; | |
} | |
/// <summary> | |
/// Creates the most alphabetic symbol | |
/// possible for the passed in element | |
/// </summary> | |
/// <param name="element"> | |
/// Element to create a symbol for | |
/// </param> | |
static string MakeSymbol(string element) | |
{ | |
element = element.ToUpper(); | |
string symbol = ""; | |
string substring = element.Substring(0, element.Length - 1); | |
symbol += substring.OrderBy(x => x).Take(1).Single(); | |
substring = element.Substring(element.IndexOf(symbol) + 1); | |
symbol += substring.OrderBy(x => x).Take(1).Single(); | |
symbol = symbol[0] + symbol[1].ToString().ToLower(); | |
return symbol; | |
} | |
/// <summary> | |
/// Returns the amount of valid element symbols | |
/// </summary> | |
/// <param name="element"> | |
/// Element to count symbols | |
/// </param> | |
static int CountValidSymbols(string element) | |
{ | |
element = element.ToUpper(); | |
List<string> symbols = new List<string>(); | |
for (int i = 0; i < element.Length - 1; i++) | |
for (int j = i + 1; j < element.Length; j++) | |
symbols.Add(element[i].ToString() + element[j].ToString()); | |
return symbols.Distinct().ToArray().Length; | |
} | |
/// <summary> | |
/// Returns the amount of valid element symbols | |
/// This method does not take into account max or min symbol length | |
/// </summary> | |
/// <param name="element"> | |
/// Element to count symbols | |
/// </param> | |
static int CountValidSymbolsNoRules(string element) | |
{ | |
List<string> symbols = new List<string>(); | |
foreach(var s in CountValidSymbolsNoRulesWorker("", element)) | |
symbols.Add(s.ToUpper()); | |
return symbols.Distinct().ToArray().Length; | |
} | |
/// <summary> | |
/// Recursive function used to determine every possible output | |
/// </summary> | |
/// <param name="pre"> | |
/// The prefix of this symbol (Should start with "") | |
/// </param> | |
/// <param name="parse"> | |
/// Every other letter of this symbol (should start with the entire element name) | |
/// </param> | |
static IEnumerable<string> CountValidSymbolsNoRulesWorker(string pre, string parse) | |
{ | |
for(int i = 0; i < parse.Length; i++) | |
{ | |
yield return pre + parse[i]; | |
foreach(var s in CountValidSymbolsNoRulesWorker(pre + parse[i], parse.Substring(i + 1))) | |
yield return s; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Reddit Easy Challenge 275