Skip to main content
deleted 3 characters in body
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

Apart from other reviews I show you an alternative how you can make this really short with LINQ and Regex:

OK, the first solution wasn't perfect. This one will however can do it correctly:

var alphabet = Enumerable.Range(97, 26).Select (i => (char)i + "+");
var pattern = "(" + string.Join("|", alphabet) + ")";

var compressed2 =
    Regex.Matches(str, pattern)
    .Cast<Match>().Select (m => new 
    { 
        Char = m.Groups[1].Value[0], 
        Count = m.Groups[1].Value.Length 
    })
    .Aggregate (string.Empty, (result, nextGroup)  => 
        result.ToString() 
        + nextGroup.Char 
        + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));

For:

var str = "aaabbccdddeezbbb";

the result is:

a3b2c2d3e2zb3

  • First get letter groups with regex and their legthslengths
  • Then aggregate them to the final string

Apart from other reviews I show you an alternative how you can make this really short with LINQ and Regex:

OK, the first solution wasn't perfect. This one will however can do it correctly:

var alphabet = Enumerable.Range(97, 26).Select (i => (char)i + "+");
var pattern = "(" + string.Join("|", alphabet) + ")";

var compressed2 =
    Regex.Matches(str, pattern)
    .Cast<Match>().Select (m => new 
    { 
        Char = m.Groups[1].Value[0], 
        Count = m.Groups[1].Value.Length 
    })
    .Aggregate (string.Empty, (result, nextGroup)  => 
        result.ToString() 
        + nextGroup.Char 
        + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));

For:

var str = "aaabbccdddeezbbb";

the result is:

a3b2c2d3e2zb3

  • First get letter groups with regex and their legths
  • Then aggregate them to the final string

Apart from other reviews I show you an alternative how you can make this really short with LINQ and Regex:

OK, the first solution wasn't perfect. This one will however do it correctly:

var alphabet = Enumerable.Range(97, 26).Select (i => (char)i + "+");
var pattern = "(" + string.Join("|", alphabet) + ")";

var compressed2 =
    Regex.Matches(str, pattern)
    .Cast<Match>().Select (m => new 
    { 
        Char = m.Groups[1].Value[0], 
        Count = m.Groups[1].Value.Length 
    })
    .Aggregate (string.Empty, (result, nextGroup)  => 
        result.ToString() 
        + nextGroup.Char 
        + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));

For:

var str = "aaabbccdddeezbbb";

the result is:

a3b2c2d3e2zb3

  • First get letter groups with regex and their lengths
  • Then aggregate them to the final string
deleted 619 characters in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

Apart from other reviews I show you an alternative how you can make this really short with LINQ and GroupBy:

var str = "aaaaabbbccdeeeee";

var compressed  = 
    str.AsEnumerable()
    .GroupBy(c => c, (c, items) => new 
    { 
        Char = c, 
        Count = items.Count()
    })
    .Aggregate(string.Empty, (current, nextGroup) => 
       current.ToString() 
       + nextGroup.Char
       + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));

Result Apart from other reviews I show you an alternative how you can make this really short with LINQ and Regex:

a5b3c2de5

  • First GroupBy each letter and get their count
  • Then Aggregate them and add the count only if it's greater then one

EDIT:

OK, the first solution wasn't perfect. This one will however can do it correctly:

var alphabet = Enumerable.Range(97, 26).Select (i => (char)i + "+");
var pattern = "(" + string.Join("|", alphabet) + ")";

var compressed2 =
    Regex.Matches(str, pattern)
    .Cast<Match>().Select (m => new 
    { 
        Char = m.Groups[1].Value[0], 
        Count = m.Groups[1].Value.Length 
    })
    .Aggregate (string.Empty, (result, nextGroup)  => 
        result.ToString() 
        + nextGroup.Char 
        + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));

For:

var str = "aaabbccdddeezbbb";

the result is:

a3b2c2d3e2zb3

  • First get letter groups with regex and their legths
  • Then aggregate them to the final string

Apart from other reviews I show you an alternative how you can make this really short with LINQ and GroupBy:

var str = "aaaaabbbccdeeeee";

var compressed  = 
    str.AsEnumerable()
    .GroupBy(c => c, (c, items) => new 
    { 
        Char = c, 
        Count = items.Count()
    })
    .Aggregate(string.Empty, (current, nextGroup) => 
       current.ToString() 
       + nextGroup.Char
       + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));

Result:

a5b3c2de5

  • First GroupBy each letter and get their count
  • Then Aggregate them and add the count only if it's greater then one

EDIT:

OK, the first solution wasn't perfect. This one will however can do it correctly:

var alphabet = Enumerable.Range(97, 26).Select (i => (char)i + "+");
var pattern = "(" + string.Join("|", alphabet) + ")";

var compressed2 =
    Regex.Matches(str, pattern)
    .Cast<Match>().Select (m => new 
    { 
        Char = m.Groups[1].Value[0], 
        Count = m.Groups[1].Value.Length 
    })
    .Aggregate (string.Empty, (result, nextGroup)  => 
        result.ToString() 
        + nextGroup.Char 
        + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));
  • First get letter groups with regex and their legths
  • Then aggregate them to the final string

Apart from other reviews I show you an alternative how you can make this really short with LINQ and Regex:

OK, the first solution wasn't perfect. This one will however can do it correctly:

var alphabet = Enumerable.Range(97, 26).Select (i => (char)i + "+");
var pattern = "(" + string.Join("|", alphabet) + ")";

var compressed2 =
    Regex.Matches(str, pattern)
    .Cast<Match>().Select (m => new 
    { 
        Char = m.Groups[1].Value[0], 
        Count = m.Groups[1].Value.Length 
    })
    .Aggregate (string.Empty, (result, nextGroup)  => 
        result.ToString() 
        + nextGroup.Char 
        + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));

For:

var str = "aaabbccdddeezbbb";

the result is:

a3b2c2d3e2zb3

  • First get letter groups with regex and their legths
  • Then aggregate them to the final string
added 760 characters in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

Apart from other reviews I show you an alternative how you can make this really short with LINQ and GroupByApart from other reviews I show you an alternative how you can make this really short with LINQ and GroupBy:

var str = "aaaaabbbccdeeeee";

var compressed  = 
    str.AsEnumerable()
    .GroupBy(c => c, (c, items) => new 
    { 
        Char = c, 
        Count = items.Count()
    })
    .Aggregate(string.Empty, (current, nextGroup) => 
       current.ToString() 
       + nextGroup.Char
       + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));

Result:

a5b3c2de5

  • First GroupBy each letter and get their count
  • Then Aggregate them and add the count only if it's greater then one

EDIT:

OK, the first solution wasn't perfect. This one will however can do it correctly:

var alphabet = Enumerable.Range(97, 26).Select (i => (char)i + "+");
var pattern = "(" + string.Join("|", alphabet) + ")";

var compressed2 =
    Regex.Matches(str, pattern)
    .Cast<Match>().Select (m => new 
    { 
        Char = m.Groups[1].Value[0], 
        Count = m.Groups[1].Value.Length 
    })
    .Aggregate (string.Empty, (result, nextGroup)  => 
        result.ToString() 
        + nextGroup.Char 
        + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));
  • First get letter groups with regex and their legths
  • Then aggregate them to the final string

Apart from other reviews I show you an alternative how you can make this really short with LINQ and GroupBy:

var str = "aaaaabbbccdeeeee";

var compressed  = 
    str.AsEnumerable()
    .GroupBy(c => c, (c, items) => new 
    { 
        Char = c, 
        Count = items.Count()
    })
    .Aggregate(string.Empty, (current, nextGroup) => 
       current.ToString() 
       + nextGroup.Char
       + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));

Result:

a5b3c2de5

  • First GroupBy each letter and get their count
  • Then Aggregate them and add the count only if it's greater then one

Apart from other reviews I show you an alternative how you can make this really short with LINQ and GroupBy:

var str = "aaaaabbbccdeeeee";

var compressed  = 
    str.AsEnumerable()
    .GroupBy(c => c, (c, items) => new 
    { 
        Char = c, 
        Count = items.Count()
    })
    .Aggregate(string.Empty, (current, nextGroup) => 
       current.ToString() 
       + nextGroup.Char
       + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));

Result:

a5b3c2de5

  • First GroupBy each letter and get their count
  • Then Aggregate them and add the count only if it's greater then one

EDIT:

OK, the first solution wasn't perfect. This one will however can do it correctly:

var alphabet = Enumerable.Range(97, 26).Select (i => (char)i + "+");
var pattern = "(" + string.Join("|", alphabet) + ")";

var compressed2 =
    Regex.Matches(str, pattern)
    .Cast<Match>().Select (m => new 
    { 
        Char = m.Groups[1].Value[0], 
        Count = m.Groups[1].Value.Length 
    })
    .Aggregate (string.Empty, (result, nextGroup)  => 
        result.ToString() 
        + nextGroup.Char 
        + (nextGroup.Count > 1 ? nextGroup.Count.ToString() : string.Empty));
  • First get letter groups with regex and their legths
  • Then aggregate them to the final string
added 1 character in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191
Loading
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191
Loading