Skip to content

Commit

Permalink
Use ReadOnlySpan<char>.Split() (#557)
Browse files Browse the repository at this point in the history
- Treat some strings as spans and use their `Split()` method.
- Use `string.Join(char)`.
- Rename variable to theta symbol.
  • Loading branch information
martincostello committed Sep 24, 2024
1 parent e88f892 commit e5c7063
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/ProjectEuler/Puzzles/Puzzle039.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ internal static ICollection<string> Solve(int perimeter)
for (double b = 1; b < perimeter - a - 1; b++)
{
double tangent = a / b;
double theta = Math.Atan(tangent);
double θ = Math.Atan(tangent);

double c = a / Math.Sin(theta);
double c = a / Math.Sin(θ);

if (Math.Abs(a + b + c - perimeterDouble) < double.Epsilon)
{
Expand All @@ -45,7 +45,7 @@ internal static ICollection<string> Solve(int perimeter)
c.ToString(Format, formatProvider),
];

string solution = $"{{{string.Join(",", sides.Order())}}}";
string solution = '{' + string.Join(',', sides.Order()) + '}';

solutions.Add(solution);
}
Expand Down
13 changes: 6 additions & 7 deletions src/ProjectEuler/Puzzles/Puzzle042.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ internal IList<string> ReadWords()
using var stream = ReadResource();
using var reader = new StreamReader(stream);

string rawWords = reader.ReadToEnd();
var text = reader.ReadToEnd().AsSpan();
var words = new List<string>();

string[] split = rawWords.Split(',');

var words = new List<string>(split.Length);

foreach (string word in split)
foreach (var word in text.Split(','))
{
words.Add(word.Trim('\"'));
words.Add(text[word].Trim('\"').ToString());
}

words.TrimExcess();

return words;
}

Expand Down

0 comments on commit e5c7063

Please sign in to comment.