Run-length encoding: Difference between revisions

→‎Linq: Cleaned up code
(→‎Linq: Cleaned up code)
Line 686:
<lang csharp>using System.Collections.Generic;
using System.Linq;
using static System.Linq.EnumerableEx;
using static System.Console;
using MoreLinq; // best addon library for linq - NOT using built in runlengthencoding() method here
 
namespace RunLengthEncoding
Line 694:
{
public static string Encode(string input) => input
.BufferGroupAdjacent(2i=>i,(_, 1vals).Select(p => vals.Select(f: p[0], s: p.Countchr => 1 ? p[1] : p[0]chr)).SkipLast(1)
.ScanSelect((val: '\0', key: 0), (scn, fs)a => (valrpt: fsa.sCount(), keychr: fsa.f == fs.s ? scn.key : scn.key + 1First()))
.Prepend((val: input[0], key: 0))
.GroupBy(kvp => kvp.key, (_, vals) => vals.Select(t => t.val))
.Select(v=> $"{v.Count()}{v.First()}")
.StringConcatToDelimitedString("");
 
public static string Decode(string input) => input
.BufferScan(2(val: '\0', 1key: 0, next:0).Select, (pscn, chr) => (fval: p[0]chr, skey: pscn.Countnext, >next: 1!char.IsDigit(chr) ? p[scn.next +1] : p[0])scn.next).SkipLast(1)
.Skip(1)
.Scan((val: '\0', key: 0), (scn, pair) => (val: pair.s, key: char.IsDigit(pair.s) && char.IsLetter(pair.f) ? scn.key + 1 : scn.key))
.Prepend((val: input[0], key: 0))
.GroupBy(t => t.key, (_, grp) => grp.Select(c => c.val))
.Select(p => new string(p.Last(), int.Parse(string.Concat(p.SkipLast(1)))))
.StringConcatToDelimitedString("");
 
private static string StringConcat(this IEnumerable<string> seq) => string.Concat(seq);
 
 
public static void Main(string[] args)
Line 738 ⟶ 734:
using System.Linq;
using static System.Console;
using MoreLinq; // best/most popular addon library for linq - NOT using built in runlengthencoding() method here
 
namespace RunLengthEncoding
Line 743 ⟶ 740:
static class Program
{
public static IEnumerable<(int,char)>string Encode(string input) => input
.GroupByGroupAdjacent(kvp i=> kvp.keyi, (_, vals) => vals.Select(tchr => t.valchr))
{
var adj.Select(a => (rpt: a.Count(), chr: 0a.First()));
return input.Zip(input.Skip(1).Concat(new char[] { default }),
(f, s) => (val: f, key: s.Equals(default) || f.Equals(s) ? adj : adj++))
.GroupBy(t => t.key, (_, grp) => grp.Select(t => t.val))
.Select(a => (a.Count(),a.First()));
}
 
public static string Decode(IEnumerable<(int i , char c)> input) =>