-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathProgram.cs
34 lines (28 loc) · 1007 Bytes
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
Assert("3A2B2A1C", Compress("AAABBAAC"));
}
public static string Compress(string input)
{
if (string.IsNullOrEmpty(input))
return "";
return input
.Skip(1) //We will use 1st character as a seed, so skip it
.Aggregate(
(repetitions: 1, prevChar: input[0], compressed: ""), //Seed
(accu, currentChar) => accu.prevChar == currentChar ? (accu.repetitions + 1, accu.prevChar, accu.compressed) : (1, currentChar, $"{accu.compressed}{accu.repetitions}{accu.prevChar}"), //Func
accu => $"{accu.compressed}{accu.repetitions}{accu.prevChar}") //Finish it
.ToString();
}
public static void Assert(string expected, string actual)
{
if (expected != actual)
{
throw new Exception($"Excpected: {expected}, but it is {actual}.");
}
}
}