Skip to content

Commit

Permalink
solve p22 in c#
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 4, 2024
1 parent 8cb4c3c commit edf7954
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Olivia's Project Euler Solutions
| | |gcc| |br| | | |Cp-Cov| |br| |
| | C++14+ in: |msvc| [1]_ | | |CodeQL| |
+------------+--------------------------+--------+-------------------+
| C# | .NET 2+ | 13 | |C#i| |br| |
| C# | .NET 2+ | 14 | |C#i| |br| |
| | | | |Cs-Cov| |br| |
| | | | |CodeQL| |
+------------+--------------------------+--------+-------------------+
Expand Down
1 change: 1 addition & 0 deletions csharp/Euler.Test/test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static IEnumerable<object[]> Data()
yield return new object[] { typeof(p0014), false, 837799 };
yield return new object[] { typeof(p0015), false, 137846528820 };
yield return new object[] { typeof(p0017), false, 21124 };
yield return new object[] { typeof(p0022), false, 871198282 };
yield return new object[] { typeof(p0034), false, 40730 };
yield return new object[] { typeof(p0076), true, 190569291 };
yield return new object[] { typeof(p0836), false, "aprilfoolsjoke" };
Expand Down
46 changes: 46 additions & 0 deletions csharp/Euler/p0022.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Project Euler Problem 22
I had to approach this by modifying the factors function from p0003, but it
seemed to work fairly well.
Problem:
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file
containing over five-thousand first names, begin by sorting it into
alphabetical order. Then working out the alphabetical value for each name,
multiply this value by its alphabetical position in the list to obtain a name
score.
For example, when the list is sorted into alphabetical order, COLIN, which is
worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would
obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?
*/
using System;

namespace Euler
{
public class p0022 : IEuler
{
public object Answer()
{
int answer = 0;
string[] names = Utilities.getDataFileText("p0022_names.txt").Replace("\"", "").Split(',');

Check failure on line 30 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (2, ubuntu-latest)

'Utilities' does not contain a definition for 'getDataFileText'

Check failure on line 30 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (3, ubuntu-latest)

'Utilities' does not contain a definition for 'getDataFileText'

Check failure on line 30 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (5, ubuntu-latest)

'Utilities' does not contain a definition for 'getDataFileText'

Check failure on line 30 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (6, ubuntu-latest)

'Utilities' does not contain a definition for 'getDataFileText'

Check failure on line 30 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (7, ubuntu-latest)

'Utilities' does not contain a definition for 'getDataFileText'

Check failure on line 30 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (8, ubuntu-latest)

'Utilities' does not contain a definition for 'getDataFileText'

Check failure on line 30 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (9, ubuntu-latest)

'Utilities' does not contain a definition for 'getDataFileText'

Check failure on line 30 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (6, macos-latest)

'Utilities' does not contain a definition for 'getDataFileText'

Check failure on line 30 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (6, macos-13)

'Utilities' does not contain a definition for 'getDataFileText'

Check failure on line 30 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (6, windows-latest)

'Utilities' does not contain a definition for 'getDataFileText'
names.Sort();

Check failure on line 31 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (2, ubuntu-latest)

No overload for method 'Sort' takes 0 arguments

Check failure on line 31 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (3, ubuntu-latest)

No overload for method 'Sort' takes 0 arguments

Check failure on line 31 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (5, ubuntu-latest)

No overload for method 'Sort' takes 0 arguments

Check failure on line 31 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (6, ubuntu-latest)

No overload for method 'Sort' takes 0 arguments

Check failure on line 31 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (7, ubuntu-latest)

No overload for method 'Sort' takes 0 arguments

Check failure on line 31 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (8, ubuntu-latest)

No overload for method 'Sort' takes 0 arguments

Check failure on line 31 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (9, ubuntu-latest)

No overload for method 'Sort' takes 0 arguments

Check failure on line 31 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (6, macos-latest)

No overload for method 'Sort' takes 0 arguments

Check failure on line 31 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (6, macos-13)

No overload for method 'Sort' takes 0 arguments

Check failure on line 31 in csharp/Euler/p0022.cs

View workflow job for this annotation

GitHub Actions / csharp (6, windows-latest)

No overload for method 'Sort' takes 0 arguments
for (int i = 0; i < names.Length; i += 1)
{
int sum = 0;
foreach (char c in names[i])
{
sum += c & 0x3F;
}
answer += sum * (i + 1);
}
return answer;
}
}
}


1 change: 1 addition & 0 deletions csharp/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ Problems Solved
- ☒ `9 <./Euler/p0009.cs>`__
- ☒ `11 <./Euler/p0011.cs>`__
- ☒ `17 <./Euler/p0017.cs>`__
- ☒ `22 <./Euler/p0022.cs>`__
- ☒ `836 <./Euler/p0836.cs>`__

24 changes: 24 additions & 0 deletions docs/csharp/p0022.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
C# Implementation of Problem 22
===============================

View source code :source:`csharp/Euler/p0022.cs`

Includes
--------

- `utils <./utils.html>`_

Problem Solution
----------------

.. csharp:namespace:: Euler
.. csharp:class:: p0022
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: object Answer()
.. literalinclude:: ../../csharp/Euler/p0022.cs
:language: csharp
:linenos:
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Problems Solved
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`21` | | | | |:py-d:`0021`| |
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`22` |:c-i:`0022` | | |:js-d:`0022`|:py-d:`0022`|:rs-d:`0022`|
|:prob:`22` |:c-i:`0022` | |:cs-d:`0022`|:js-d:`0022`|:py-d:`0022`|:rs-d:`0022`|
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`23` | | | | |:py-d:`0023`| |
+-----------+------------+------------+------------+------------+------------+------------+
Expand Down
1 change: 1 addition & 0 deletions javascript/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@ Problems Solved
- ☒ `9 <./src/p0009.js>`__
- ☒ `11 <./src/p0011.js>`__
- ☒ `17 <./src/p0017.js>`__
- ☒ `22 <./src/p0022.js>`__
- ☒ `836 <./src/p0836.js>`__

0 comments on commit edf7954

Please sign in to comment.