Run-length encoding: Difference between revisions

Content added Content deleted
(→‎Linq: More idiomatic Linq and faster)
(→‎Linq: Updated short linq solution to match)
Line 733: Line 733:
using System.Linq;
using System.Linq;
using static System.Console;
using static System.Console;
using MoreLinq; // best/most popular addon library for linq - NOT using built in runlengthencoding() method here

namespace RunLengthEncoding
namespace RunLengthEncoding
{
{
static class Program
static class Program
{
{
public static string Encode(string input) => input
public static string Encode(string input) => input.Length ==0 ? "" : input.Skip(1)
.GroupAdjacent(i=>i,(key, vals) => (rpt:vals.Count(), chr: key));
.Aggregate((t:input[0].ToString(),o:Empty<string>()),
(a,c)=>a.t[0]==c ? (a.t+c,a.o) : (c.ToString(),a.o.Append(a.t)),
a=>a.o.Append(a.t).Select(p => (key: p.Length, chr: p[0])));


public static string Decode(IEnumerable<(int i , char c)> input) =>
public static string Decode(IEnumerable<(int i , char c)> input) =>