Guest User

Dedupe

a guest
May 26th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace BadCode
  6. {
  7.     class Thingy
  8.     {
  9.         public Thingy before;
  10.         public Thingy after;
  11.         public string value;
  12.  
  13.         public Thingy(string value)
  14.         {
  15.             this.value = value;
  16.         }
  17.  
  18.         public Thingy Shrink()
  19.         {
  20.             if (after?.value == value)
  21.             {
  22.                 return after;
  23.             }
  24.  
  25.             return this;
  26.         }
  27.     }
  28.  
  29.     class ThingyContainer
  30.     {
  31.         public Thingy starto;
  32.         public Thingy endo;
  33.  
  34.         public ThingyContainer(List<string> s)
  35.         {
  36.             starto = new Thingy(null);
  37.             endo = new Thingy(null);
  38.             starto.after = endo;
  39.             endo.before = starto;
  40.            
  41.             foreach (var s1 in s)
  42.             {
  43.                 Put(s1);
  44.             }
  45.         }
  46.  
  47.         public void Put(string s)
  48.         {
  49.             var endoBefore = endo.before;
  50.             var x = new Thingy(s);
  51.             endoBefore.after = x;
  52.             x.before = endoBefore;
  53.             endo.before = x;
  54.             x.after = endo;
  55.         }
  56.  
  57.         public void Shrink()
  58.         {
  59.             var x = starto;
  60.             while (x?.after != null)
  61.             {
  62.                 x.after = x.after.Shrink();
  63.                 x = x.after;
  64.             }
  65.         }
  66.  
  67.         public string Rebuild()
  68.         {
  69.             var p = new List<string>();
  70.             var x = starto.after;
  71.             while (x != null)
  72.             {
  73.                 p.Add(x.value);
  74.                 x = x.after;
  75.             }
  76.  
  77.             p = p.Where(u => u != null).ToList();
  78.             string r = "";
  79.             foreach (var s in p)
  80.             {
  81.                 r += s;
  82.             }
  83.  
  84.             return r;
  85.         }
  86.     }
  87.    
  88.     class Program
  89.     {
  90.         static void Main(string[] args)
  91.         {
  92.             while (true)
  93.             {
  94.                 Console.WriteLine("Your input:");
  95.                 var s = Console.ReadLine();
  96.                 Console.WriteLine(Dedupe(s));
  97.                 Console.WriteLine();
  98.             }
  99.         }
  100.  
  101.         private static string TryDedupe(string s) //does not work as intended on first try
  102.         {
  103.             var s1 = new List<string>();
  104.             while (s?.Length > 0)
  105.             {
  106.                 s1.Add(s.Substring(0, 1));
  107.                 s = s.Substring(1);
  108.             }
  109.            
  110.             var t = new ThingyContainer(s1);
  111.             t.Shrink();
  112.             var i = t.Rebuild();
  113.             return i;
  114.         }
  115.  
  116.         public static string Dedupe(string s)
  117.         {
  118.             string s1 = s;
  119.             string s2 = TryDedupe(s);
  120.  
  121.             while (s1.Length != s2.Length)
  122.             {
  123.                 s1 = s2;
  124.                 s2 = TryDedupe(s2);
  125.             }
  126.  
  127.             return s2;
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment