Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- namespace DeDupe // Submitted to r/badcode by Killing Torcher
- {
- class Program
- {
- static void Main(string[] args)
- {
- string deduped;
- Dedupe(Console.ReadLine(), out deduped);
- Console.WriteLine(deduped);
- Console.Read();
- }
- public static void Dedupe(string str, out string target)
- {
- UniqueLetterMatrix ULM = new UniqueLetterMatrix(str);
- StringBuilder sb = new StringBuilder();
- foreach (Letter L in ULM.getVal(true))
- {
- sb.Append(L.letterValue.ToString());
- }
- target = sb.ToString();
- }
- public class UniqueLetterMatrix
- {
- public List<Letter> Letters = new List<Letter>();
- public UniqueLetterMatrix(string value)
- {
- foreach (char c in value)
- {
- string LetterString = c.ToString();
- LetterValidator LV = new LetterValidator();
- LV.LoadString(LetterString);
- if (string.IsNullOrEmpty(LV.LoadedString))
- {
- throw (new MatrixInitializationError("Could not load letter " + LetterString));
- }
- }
- foreach (char c in value)
- {
- string realStr = c.ToString();
- bool seen = false;
- foreach (Letter L in Letters)
- {
- if (L.cmp(new Letter(realStr))) seen = true;
- }
- if (!seen) Letters.Add(new FirstSeenLetter(realStr));
- if (seen) Letters.Add(new MultiSeenLetter(realStr));
- }
- }
- public List<Letter> getVal(bool unique=false)
- {
- List<Letter> temp = new List<Letter>();
- Letter last = null;
- foreach (Letter L in Letters) {
- if (unique && (L.GetType() == typeof(FirstSeenLetter) || (last != null && !last.cmp(L)))) temp.Add(L);
- else if(!unique) temp.Add(L);
- last = L;
- }
- return temp;
- }
- }
- public class MultiSeenLetter : Letter
- {
- public MultiSeenLetter(string letter) : base(letter) { }
- public override string GetValueProper()
- {
- return "";
- }
- }
- public class FirstSeenLetter : Letter
- {
- public FirstSeenLetter(string letter) : base(letter) { }
- public override string GetValueProper()
- {
- return base.GetValueProper();
- }
- }
- public class Letter
- {
- public string letterValue {
- get
- {
- return _letter;
- }
- set {
- _letter = value;
- }
- }
- private string _letter;
- public Letter(string letter)
- {
- LetterValidator LV = new LetterValidator();
- LV.LoadString(letter);
- LV.Validate();
- if (string.IsNullOrEmpty(LV.LoadedString)) throw (new LetterValidationError("Failed to validate letter in Letter(string Letter) constructor!"));
- letterValue = LV.LoadedString;
- }
- public virtual string GetValueProper()
- {
- return letterValue.ToString();
- }
- public bool cmp(Letter other)
- {
- return other.letterValue == this.letterValue;
- }
- }
- public class LetterValidator
- {
- public string LoadedString
- {
- get { return _loaded; }
- set { _loaded = value; }
- }
- private string _loaded;
- public void LoadString(string str)
- {
- LoadedString = str;
- }
- public void Validate()
- {
- if (LoadedString.ToLower() != "a" &&
- LoadedString.ToLower() != "b" &&
- LoadedString.ToLower() != "c" &&
- LoadedString.ToLower() != "d" &&
- LoadedString.ToLower() != "e" &&
- LoadedString.ToLower() != "f" &&
- LoadedString.ToLower() != "g" &&
- LoadedString.ToLower() != "h" &&
- LoadedString.ToLower() != "i" &&
- LoadedString.ToLower() != "j" &&
- LoadedString.ToLower() != "k" &&
- LoadedString.ToLower() != "l" &&
- LoadedString.ToLower() != "m" &&
- LoadedString.ToLower() != "n" &&
- LoadedString.ToLower() != "o" &&
- LoadedString.ToLower() != "p" &&
- LoadedString.ToLower() != "q" &&
- LoadedString.ToLower() != "r" &&
- LoadedString.ToLower() != "s" &&
- LoadedString.ToLower() != "t" &&
- LoadedString.ToLower() != "u" &&
- LoadedString.ToLower() != "v" &&
- LoadedString.ToLower() != "w" &&
- LoadedString.ToLower() != "x" &&
- LoadedString.ToLower() != "y" &&
- LoadedString.ToLower() != "z")
- {
- LoadedString = "";
- }
- }
- }
- public class LetterValidationError : Exception
- {
- public LetterValidationError(string tx) : base("Validation error: " + tx) { }
- }
- public class MatrixInitializationError : Exception
- {
- public MatrixInitializationError(string tx) : base("Matrix Initialization error: " + tx) { }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment