Skip to main content
Brackets
Source Link

Here's a way to do it in one line of Linq code:

int score = System.IO.File.ReadAllLines(ConfigurationManager.AppSettings["LocationOfNamesFile"])
    .SelectMany(s => s.Split(','))
    .OrderBy(b => b)
    .Select((s, i) => new { 
        Index = i + 1, 
        Score = s.Sum(ch => (int)(char.ToUpper(ch) - 'A' + 1)) })
    .Sum(s => s.Index * s.Score);

This will throw an exception if the file doesn't exist, but your original code does this as well.

Here's a way to do it in one line of Linq code:

int score = System.IO.File.ReadAllLines(ConfigurationManager.AppSettings["LocationOfNamesFile"])
    .SelectMany(s => s.Split(','))
    .OrderBy(b => b)
    .Select((s, i) => new { 
        Index = i + 1, 
        Score = s.Sum(ch => (int)(char.ToUpper(ch) - 'A' + 1))
    .Sum(s => s.Index * s.Score);

This will throw an exception if the file doesn't exist, but your original code does this as well.

Here's a way to do it in one line of Linq code:

int score = System.IO.File.ReadAllLines(ConfigurationManager.AppSettings["LocationOfNamesFile"])
    .SelectMany(s => s.Split(','))
    .OrderBy(b => b)
    .Select((s, i) => new { 
        Index = i + 1, 
        Score = s.Sum(ch => (int)(char.ToUpper(ch) - 'A' + 1)) })
    .Sum(s => s.Index * s.Score);

This will throw an exception if the file doesn't exist, but your original code does this as well.

Forgot to put in alphabetical order before determining the score. Also forgot to split the lines by comma.
Source Link

Here's a way to do it in one line of Linq code:

int score = System.IO.File.ReadAllLines(ConfigurationManager.AppSettings["LocationOfNamesFile"])
    .SelectMany(s => s.Split(','))
    .OrderBy(b => b)
    .Select((s, i) => new { 
        Index = i + 1, 
        Score = s.SelectSum(ch => (int)(char.ToUpper(ch) - 'A' + 1)).Sum() })
    .Sum(s => s.Index * s.Score);

This will throw an exception if the file doesn't exist, but your original code does this as well.

Here's a way to do it in one line of Linq code:

int score = System.IO.File.ReadAllLines(ConfigurationManager.AppSettings["LocationOfNamesFile"])
    .Select((s, i) => new { 
        Index = i + 1, 
        Score = s.Select(ch => (int)(char.ToUpper(ch) - 'A' + 1)).Sum() })
    .Sum(s => s.Index * s.Score);

This will throw an exception if the file doesn't exist, but your original code does this as well.

Here's a way to do it in one line of Linq code:

int score = System.IO.File.ReadAllLines(ConfigurationManager.AppSettings["LocationOfNamesFile"])
    .SelectMany(s => s.Split(','))
    .OrderBy(b => b)
    .Select((s, i) => new { 
        Index = i + 1, 
        Score = s.Sum(ch => (int)(char.ToUpper(ch) - 'A' + 1))
    .Sum(s => s.Index * s.Score);

This will throw an exception if the file doesn't exist, but your original code does this as well.

Source Link

Here's a way to do it in one line of Linq code:

int score = System.IO.File.ReadAllLines(ConfigurationManager.AppSettings["LocationOfNamesFile"])
    .Select((s, i) => new { 
        Index = i + 1, 
        Score = s.Select(ch => (int)(char.ToUpper(ch) - 'A' + 1)).Sum() })
    .Sum(s => s.Index * s.Score);

This will throw an exception if the file doesn't exist, but your original code does this as well.