Dictionary<char, int> scoreLookup = new Dictionary<char, int>() {
{ 'a', 1 }, { 'b', 2 }, { 'c', 3 }, { 'd', 4 }, { 'e', 5 }, { 'f', 6 }, { 'g', 7 },
{ 'h', 8 }, { 'i', 9 }, { 'j', 10 }, { 'k', 11 }, { 'l', 12 }, { 'm', 13 }, { 'n', 14 },
{ 'o', 15 }, { 'p', 16 }, { 'q', 17 }, { 'r', 18 }, { 's', 19 }, { 't', 20 },
{ 'u', 21 }, { 'v', 22 }, { 'w', 23 }, { 'x', 24 }, { 'y', 25 }, { 'z', 26 } };
// then to convert the string
name.ToLower().Select((character, index) => scoreLookup[character] * (index + 1)).Sum();
Dictionary<char, int> scoreLookup = new Dictionary<char, int>() {
{ 'a', 1 }, { 'b', 2 }, { 'c', 3 }, { 'd', 4 }, { 'e', 5 }, { 'f', 6 }, { 'g', 7 },
{ 'h', 8 }, { 'i', 9 }, { 'j', 10 }, { 'k', 11 }, { 'l', 12 }, { 'm', 13 }, { 'n', 14 },
{ 'o', 15 }, { 'p', 16 }, { 'q', 17 }, { 'r', 18 }, { 's', 19 }, { 't', 20 },
{ 'u', 21 }, { 'v', 22 }, { 'w', 23 }, { 'x', 24 }, { 'y', 25 }, { 'z', 26 } };
// then to convert the string
name.Select((character, index) => scoreLookup[character] * (index + 1)).Sum();
Dictionary<char, int> scoreLookup = new Dictionary<char, int>() {
{ 'a', 1 }, { 'b', 2 }, { 'c', 3 }, { 'd', 4 }, { 'e', 5 }, { 'f', 6 }, { 'g', 7 },
{ 'h', 8 }, { 'i', 9 }, { 'j', 10 }, { 'k', 11 }, { 'l', 12 }, { 'm', 13 }, { 'n', 14 },
{ 'o', 15 }, { 'p', 16 }, { 'q', 17 }, { 'r', 18 }, { 's', 19 }, { 't', 20 },
{ 'u', 21 }, { 'v', 22 }, { 'w', 23 }, { 'x', 24 }, { 'y', 25 }, { 'z', 26 } };
// then to convert the string
name.ToLower().Select((character, index) => scoreLookup[character] * (index + 1)).Sum();
// read the lines from the file
File.ReadLines(fileName)
// if there are multiple names per line, extract all of them
.SelectMany(line => line.Split(','))
// sort by the name
.OrderBy(name => name)
// now for each name, we also get the index and use that to calculate the score
.Select((name, index) => {
// convert to lower
name = name.ToLower();
// convert each character to an int, and subtract 96
// (since the character 'a' has the value 97);
// finally get the sum
int score = name.AsEnumerable().Select(character => character - 96).Sum();
// add the indexmultiplicate toby the scoreindex, add 1 because the index starts at 0
score +=*= index + 1;
// return the score
return score;
})
// finally, calculate the sum of all scores
.Sum();
For the future, if you need a more complicated lookup that does not work using character arithmetics, use a dictionary:
Dictionary<char, int> scoreLookup = new Dictionary<char, int>() {
{ 'a', 1 }, { 'b', 2 }, { 'c', 3 }, { 'd', 4 }, { 'e', 5 }, { 'f', 6 }, { 'g', 7 },
{ 'h', 8 }, { 'i', 9 }, { 'j', 10 }, { 'k', 11 }, { 'l', 12 }, { 'm', 13 }, { 'n', 14 },
{ 'o', 15 }, { 'p', 16 }, { 'q', 17 }, { 'r', 18 }, { 's', 19 }, { 't', 20 },
{ 'u', 21 }, { 'v', 22 }, { 'w', 23 }, { 'x', 24 }, { 'y', 25 }, { 'z', 26 } };
// then to convert the string
name.Select((character, index) => scoreLookup[character] * (index + 1)).Sum();
// read the lines from the file
File.ReadLines(fileName)
// if there are multiple names per line, extract all of them
.SelectMany(line => line.Split(','))
// sort by the name
.OrderBy(name => name)
// now for each name, we also get the index and use that to calculate the score
.Select((name, index) => {
// convert to lower
name = name.ToLower();
// convert each character to an int, and subtract 96
// (since the character 'a' has the value 97);
// finally get the sum
int score = name.AsEnumerable().Select(character => character - 96).Sum();
// add the index to the score, add 1 because the index starts at 0
score += index + 1;
// return the score
return score;
})
// finally, calculate the sum of all scores
.Sum();
// read the lines from the file
File.ReadLines(fileName)
// if there are multiple names per line, extract all of them
.SelectMany(line => line.Split(','))
// sort by the name
.OrderBy(name => name)
// now for each name, we also get the index and use that to calculate the score
.Select((name, index) => {
// convert to lower
name = name.ToLower();
// convert each character to an int, and subtract 96
// (since the character 'a' has the value 97);
// finally get the sum
int score = name.AsEnumerable().Select(character => character - 96).Sum();
// multiplicate by the index, add 1 because the index starts at 0
score *= index + 1;
// return the score
return score;
})
// finally, calculate the sum of all scores
.Sum();
For the future, if you need a more complicated lookup that does not work using character arithmetics, use a dictionary:
Dictionary<char, int> scoreLookup = new Dictionary<char, int>() {
{ 'a', 1 }, { 'b', 2 }, { 'c', 3 }, { 'd', 4 }, { 'e', 5 }, { 'f', 6 }, { 'g', 7 },
{ 'h', 8 }, { 'i', 9 }, { 'j', 10 }, { 'k', 11 }, { 'l', 12 }, { 'm', 13 }, { 'n', 14 },
{ 'o', 15 }, { 'p', 16 }, { 'q', 17 }, { 'r', 18 }, { 's', 19 }, { 't', 20 },
{ 'u', 21 }, { 'v', 22 }, { 'w', 23 }, { 'x', 24 }, { 'y', 25 }, { 'z', 26 } };
// then to convert the string
name.Select((character, index) => scoreLookup[character] * (index + 1)).Sum();
- Use
File.ReadLines
to read from your file. This makes everything a lot simpler and avoids having to work with stream readers and buffers. You will just get back every line, so you don’t have too much about things. Regex.Split(line, ",")
– Don’t useRegex.Split
here but justString.Split
:line.Split(',')
List<string> sumOffAllNames
– This shouldn’t be a list of strings for two reasons: A sum is a single value, and more importantly, it is a number.- Don’t use
Regex.Replace
but just useString.Replace
for simple static replacements. Regex.Replace(...).Replace(...)
actually uses two different replace methods: First it usesRegex.Replace
, which returns a string, and then you useString.Replace
.- For the character to number conversions, use a dictionary that just looks up the values for each character. Or use arithmetics to calculate the value based on the character value.
- You shouldn’t need to use
int.Parse
for this situation ever. If instead of replacing characters by number strings you just collect the numbers directly, then you can just sum them later without having to parse them (you actually parse them multiple times making this worse).
- Use
File.ReadLines
to read from your file. This makes everything a lot simpler and avoids having to work with stream readers and buffers. You will just get back every line, so you don’t have too much about things. Regex.Split(line, ",")
– Don’t useRegex.Split
here but justString.Split
:line.Split(',')
List<string> sumOffAllNames
– This shouldn’t be a list of strings for two reasons: A sum is a single value, and more importantly, it is a number.- Don’t use
Regex.Replace
but just useString.Replace
for simple static replacements. - For the character to number conversions, use a dictionary that just looks up the values for each character. Or use arithmetics to calculate the value based on the character value.
- You shouldn’t need to use
int.Parse
for this situation ever. If instead of replacing characters by number strings you just collect the numbers directly, then you can just sum them later without having to parse them (you actually parse them multiple times making this worse).
- Use
File.ReadLines
to read from your file. This makes everything a lot simpler and avoids having to work with stream readers and buffers. You will just get back every line, so you don’t have too much about things. Regex.Split(line, ",")
– Don’t useRegex.Split
here but justString.Split
:line.Split(',')
List<string> sumOffAllNames
– This shouldn’t be a list of strings for two reasons: A sum is a single value, and more importantly, it is a number.- Don’t use
Regex.Replace
but just useString.Replace
for simple static replacements. Regex.Replace(...).Replace(...)
actually uses two different replace methods: First it usesRegex.Replace
, which returns a string, and then you useString.Replace
.- For the character to number conversions, use a dictionary that just looks up the values for each character. Or use arithmetics to calculate the value based on the character value.
- You shouldn’t need to use
int.Parse
for this situation ever. If instead of replacing characters by number strings you just collect the numbers directly, then you can just sum them later without having to parse them (you actually parse them multiple times making this worse).
lang-cs