Skip to content

Develop #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ insert_final_newline = true
# Use tabs for indentation (Makefiles require tabs)
indent_style = tab
indent_size = 2

###############################
# .NET C# Coding Conventions #
###############################

[*.cs]
#IDE1006
dotnet_naming_style.camel_case.capitalization = camel_case
dotnet_naming_symbols.private_symbols.applicable_accessibilities = private
dotnet_naming_rule.camel_case_for_private.severity = warning
dotnet_naming_rule.camel_case_for_private.symbols = private_symbols
dotnet_naming_rule.camel_case_for_private.style = camel_case
2 changes: 2 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ jobs:
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Lint (codestyle)
run: dotnet format --verify-no-changes --verbosity normal
- name: Test
run: dotnet test --no-build --verbosity normal
2 changes: 1 addition & 1 deletion .github/workflows/markdown-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node-version: [20.x]
node-version: [22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"sonarlint.connectedMode.project": {
"connectionId": "sir-gon",
"projectKey": "sir-gon_algorithm-exercises-csharp"
}
},
"editor.defaultFormatter": "ms-dotnettools.csdevkit",
"editor.formatOnSave": true
}
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ lint: test/static test/styling mdlint
test/static: dependencies

test/styling: dependencies
${PACKAGE_TOOL} format --verify-no-changes --verbosity ${VERBOSITY_LEVEL}

format:
${PACKAGE_TOOL} format --verbosity ${VERBOSITY_LEVEL}

build: env dependencies
${PACKAGE_TOOL} build --verbosity ${VERBOSITY_LEVEL}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
[TestClass]
public class Euler001Test
{
public class Euler001TestCase {
public class Euler001TestCase
{
public int a; public int b; public int n; public int answer;
}

// dotnet_style_readonly_field = true
private static readonly Euler001TestCase[] tests = [
new() { a = 3, b = 5, n = 10, answer = 23},
new() { a = 3, b = 5, n = 100, answer = 2318},
new() { a = 3, b = 5, n = 1000, answer = 233168},
new() { a = 3, b = 5, n = 10, answer = 23 },
new() { a = 3, b = 5, n = 100, answer = 2318 },
new() { a = 3, b = 5, n = 1000, answer = 233168 },

];

Expand All @@ -20,9 +21,10 @@ public void Euler001ProblemTest()
{
int result;

foreach (Euler001TestCase test in tests) {
foreach (Euler001TestCase test in tests)
{
result = Euler001Problem.Euler001(test.a, test.b, test.n);
Assert.AreEqual(test.answer, result);
Assert.AreEqual(test.answer, result);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
[TestClass]
public class Euler002Test
{
public class Euler002TestCase {
public class Euler002TestCase
{
public int n; public int answer;
}

// dotnet_style_readonly_field = true
private static readonly Euler002TestCase[] tests = [
new() { n = 10, answer = 10},
new() { n = 100, answer = 44}
new() { n = 10, answer = 10 },
new() { n = 100, answer = 44 }
];

[TestMethod]
public void Euler002ProblemTest()
{
int result;

foreach (Euler002TestCase test in tests) {
foreach (Euler002TestCase test in tests)
{
result = Euler002Problem.Euler002(test.n);
Assert.AreEqual(test.answer, result);
Assert.AreEqual(test.answer, result);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
[TestClass]
public class Euler003Test
{
public class Euler003TestCase {
public class Euler003TestCase
{
public int n; public int? answer;
}

// dotnet_style_readonly_field = true
private static readonly Euler003TestCase[] tests = [
new() { n = 1, answer = null},
new() { n = 10, answer = 5},
new() { n = 17, answer = 17}
new() { n = 1, answer = null },
new() { n = 10, answer = 5 },
new() { n = 17, answer = 17 }
];

[TestMethod]
public void Euler003ProblemTest()
{
int? result;

foreach (Euler003TestCase test in tests) {
foreach (Euler003TestCase test in tests)
{
result = Euler003Problem.Euler003(test.n);
Assert.AreEqual(test.answer, result);
}
Expand Down
5 changes: 3 additions & 2 deletions algorithm-exercises-csharp/src/Hello.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ public class HelloWorld
const string message = "Hello World!";

[ExcludeFromCodeCoverage]
protected HelloWorld() {}
protected HelloWorld() { }

public static string Hello()
{
return HelloWorld.message;
}

public static HelloWorld Create() {
public static HelloWorld Create()
{
return new HelloWorld();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
public class Euler001Problem
{
[ExcludeFromCodeCoverage]
protected Euler001Problem() {}
protected Euler001Problem() { }

public static int SuOfArithmeticProgression(int n, int d) {
public static int SuOfArithmeticProgression(int n, int d)
{
int new_n = n / d;

return new_n * (1 + new_n) * d / 2;
Expand All @@ -19,7 +20,7 @@ public static int GCD(int u, int v)
{
if (v != 0)
{
return GCD(v, u % v);
return GCD(v, u % v);
}
return u;
}
Expand Down
21 changes: 12 additions & 9 deletions algorithm-exercises-csharp/src/hackerrank/projecteuler/Euler002.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
public class Euler002Problem
{
[ExcludeFromCodeCoverage]
protected Euler002Problem() {}
protected Euler002Problem() { }

public static int FiboEvenSum(int n) {
public static int FiboEvenSum(int n)
{
int total = 0;
int fibo;
int fibo1 = 1;
int fibo2 = 1;

while (fibo1 + fibo2 < n) {
fibo = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibo;
while (fibo1 + fibo2 < n)
{
fibo = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibo;

if(fibo % 2 == 0) {
total += fibo;
}
if (fibo % 2 == 0)
{
total += fibo;
}
}

return total;
Expand Down
29 changes: 18 additions & 11 deletions algorithm-exercises-csharp/src/hackerrank/projecteuler/Euler003.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,34 @@ namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
public class Euler003Problem
{
[ExcludeFromCodeCoverage]
protected Euler003Problem() {}
protected Euler003Problem() { }

public static int? PrimeFactor(int n) {
if (n < 2) {
public static int? PrimeFactor(int n)
{
if (n < 2)
{
return null;
}

int divisor = n;
int? max_prime_factor = null;

int i = 2;
while (i <= Math.Sqrt(divisor)) {
if (0 == divisor % i) {
divisor = divisor / i;
max_prime_factor = divisor;
} else {
i += 1;
}
while (i <= Math.Sqrt(divisor))
{
if (0 == divisor % i)
{
divisor = divisor / i;
max_prime_factor = divisor;
}
else
{
i += 1;
}
}

if (max_prime_factor is null) {
if (max_prime_factor is null)
{
return n;
}

Expand Down