Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace BadCode
- {
- class Thingy
- {
- public Thingy before;
- public Thingy after;
- public string value;
- public Thingy(string value)
- {
- this.value = value;
- }
- public Thingy Shrink()
- {
- if (after?.value == value)
- {
- return after;
- }
- return this;
- }
- }
- class ThingyContainer
- {
- public Thingy starto;
- public Thingy endo;
- public ThingyContainer(List<string> s)
- {
- starto = new Thingy(null);
- endo = new Thingy(null);
- starto.after = endo;
- endo.before = starto;
- foreach (var s1 in s)
- {
- Put(s1);
- }
- }
- public void Put(string s)
- {
- var endoBefore = endo.before;
- var x = new Thingy(s);
- endoBefore.after = x;
- x.before = endoBefore;
- endo.before = x;
- x.after = endo;
- }
- public void Shrink()
- {
- var x = starto;
- while (x?.after != null)
- {
- x.after = x.after.Shrink();
- x = x.after;
- }
- }
- public string Rebuild()
- {
- var p = new List<string>();
- var x = starto.after;
- while (x != null)
- {
- p.Add(x.value);
- x = x.after;
- }
- p = p.Where(u => u != null).ToList();
- string r = "";
- foreach (var s in p)
- {
- r += s;
- }
- return r;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- while (true)
- {
- Console.WriteLine("Your input:");
- var s = Console.ReadLine();
- Console.WriteLine(Dedupe(s));
- Console.WriteLine();
- }
- }
- private static string TryDedupe(string s) //does not work as intended on first try
- {
- var s1 = new List<string>();
- while (s?.Length > 0)
- {
- s1.Add(s.Substring(0, 1));
- s = s.Substring(1);
- }
- var t = new ThingyContainer(s1);
- t.Shrink();
- var i = t.Rebuild();
- return i;
- }
- public static string Dedupe(string s)
- {
- string s1 = s;
- string s2 = TryDedupe(s);
- while (s1.Length != s2.Length)
- {
- s1 = s2;
- s2 = TryDedupe(s2);
- }
- return s2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment