Skip to content

Add files via upload #3

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Binary file added Exercice pratique - La carte aux treėsors.pdf
Binary file not shown.
110 changes: 110 additions & 0 deletions LevelOne.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace TechnicalTests.Solution
{
public class LevelOne
{
private const int kNumber = 6;
private const int cNumber = 9;
private const string valueIfAnyChar = "0";

private List<char> _listOfCharToFindInFile;

public LevelOne(List<char> listOfCharToFindInFile)
{

_listOfCharToFindInFile = new List<char>();
_listOfCharToFindInFile = listOfCharToFindInFile;
}


public int GetMagicNumber(string path)
{
string valueConcatenation = valueIfAnyChar;
foreach (char ch in _listOfCharToFindInFile)
{
valueConcatenation += GetMagicNumber(path, ch).ToString();
}
return Int32.Parse(valueConcatenation);
}

public int GetSuperMagicNumber(string path)
{
string valueConcatenation = valueIfAnyChar;
foreach (char ch in _listOfCharToFindInFile)
{
valueConcatenation += GetSuperMagicNumber(path, ch).ToString();
}
return Int32.Parse(valueConcatenation);
}
private int GetSuperMagicNumber(string path, char charToCount)
{
int sumOfSpecificChar = CountCharByPositon(path, charToCount);
int totalChar = CountChar(path, charToCount);

if (totalChar == 0)
return 0;

if (charToCount == 'k')
return totalChar * (sumOfSpecificChar / totalChar) * kNumber;

return totalChar * (sumOfSpecificChar / totalChar) * cNumber;
}

private int GetMagicNumber(string path, char charToCount)
{

if (charToCount == 'k')
return CountChar(path, charToCount) * kNumber;
else
return CountChar(path, charToCount) * cNumber;
}

/// <summary>
/// Method who count number off specific charactere
/// </summary>
/// <param name="path"></param>
/// <param name="charToCount"></param>
/// <returns>number of occurence in a file</returns>
private int CountChar(string path, char charToCount)
{
string file = File.ReadAllText(path);
int nbOfChar = file.Count(c => char.ToUpperInvariant(c) == char.ToUpperInvariant(charToCount));
return nbOfChar;
}

/// <summary>
/// Method who get index of specific charactere and sum it
/// </summary>
/// <param name="path"></param>
/// <param name="charToCount"></param>
/// <returns></returns>
private int CountCharByPositon(string path, char charToCount)
{
int res = 0;
string file = File.ReadAllText(path);
List<string> fileLine = file.Split('\n').ToList();
List<int> foundIndexesOfSpecificChar = new List<int>();

foreach (string line in fileLine)
{
for (int it = 0; it < line.Count(); it ++)
{
if (char.ToUpperInvariant(line[it]) == char.ToUpperInvariant(charToCount))
foundIndexesOfSpecificChar.Add(it + 1);
}
}

foreach(int item in foundIndexesOfSpecificChar)
res += item;

return res;
}

}
}
71 changes: 71 additions & 0 deletions LevelOne.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
In level one, you have an input file (aptly named input.txt) containing words, arranged one by line.

There are two steps:

Step 1
=

We define the magic number as:

- take the total count of letters 'k' in all the words in the file, and multiply by 6
- take the total count of letters 'c' in all the words in the file, and multiply by 9
- the magic number is the concatenation of the first number then the second number.

Your task is to write a function that will return the magic number.

Example:
-

If your file contains the following input:

Mock
Crabs
Kangaroo
Chicken
Worcestershire

The number of 'k' is 3, multiplied by 6 gives us 18
The number of 'c' is 5, multiplied by 9 gives us 45
Here, your function should return 1845

Step 2
=

What we did isn't magical enough, we need MORE magic!

To create more magic, we've heard you need to take the average position of each considered letter
and multiply each individual sum by those number as well.

Ignore words that do not contain the letter in the average.
If a word has the letter multiple times, count each letter individually.
The first letter is considered at position 1.
The average must be rounded down.

So the super-magic number is defined as:

- take the total count of letters 'k' in all the words in the file, multiply by the average position of all the letters 'k', and finally multiply by 6
- take the total count of letters 'c' in all the words in the file, multiply by the average position of all the letters 'c', and finally multiply by 9
- the magic number is the concatenation of the first number then the second number.

Your task is to write a function that will return the super-magic number.

Example:
-

If your file contains the same input as step 1:

Mock
Crabs
Kangaroo
Chicken
Worcestershire

The number of 'k' is 3,
The average position of the letters 'k' is: (3 + 1 + 4) / 3 = 8 / 3 = 2
3 multiplied by 2 multiplied by 6 gives us 36

The number of 'c' is 5,
The average position of the letters 'c' is: (2 + 1 + 1 + 3 + 3) / 5 = 10 / 5 = 2
5 multiplied by 2 multiplied by 9 gives us 90

Here, your function should return 3645
126 changes: 126 additions & 0 deletions SolutionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using FluentAssertions;
using NUnit.Framework;
using System.Collections.Generic;


namespace TechnicalTests.UnitTests.LevelOne
{
[TestFixture]
internal class SolutionTests
{

#region Test of method GetMagicNumber with different file

// TO DO ADD TEST WITH ANOTHER CHAR

[Test]
public void GetMagicNumber_Should_Return_Correct_Value_With_Input()
{
var listForTest = new List<char>();
listForTest.Add('k');
listForTest.Add('c');
var solution = new Solution.LevelOne(listForTest);


var result = solution.GetMagicNumber("C:\\Users\\Mehdi\\Source\\Repos\\TechnicalTestsDotnet\\TechnicalTests\\TechnicalTests.UnitTests\\LevelOne\\input.txt");
result.Should().Be(78576);
}


[Test]
public void GetMagicNumber_Should_Return_Correct_Value_With_Input2()
{
var listForTest = new List<char>();
listForTest.Add('k');
listForTest.Add('c');
var solution = new Solution.LevelOne(listForTest);

var result = solution.GetMagicNumber("C:\\Users\\Mehdi\\Source\\Repos\\TechnicalTestsDotnet\\TechnicalTests\\TechnicalTests.UnitTests\\LevelOne\\input2.txt");
result.Should().Be(1845);

}

[Test]
public void GetMagicNumber_Should_Return_Correct_Value_With_Input3()
{
var listForTest = new List<char>();
listForTest.Add('k');
listForTest.Add('c');
var solution = new Solution.LevelOne(listForTest);

var result = solution.GetMagicNumber("C:\\Users\\Mehdi\\Source\\Repos\\TechnicalTestsDotnet\\TechnicalTests\\TechnicalTests.UnitTests\\LevelOne\\input3.txt");
result.Should().Be(3063);

}


[Test]
public void GetMagicNumber_Should_Return_Correct_Value_With_InputEmpty()
{
var listForTest = new List<char>();
listForTest.Add('k');
listForTest.Add('c');
var solution = new Solution.LevelOne(listForTest);

var result = solution.GetMagicNumber("C:\\Users\\Mehdi\\Source\\Repos\\TechnicalTestsDotnet\\TechnicalTests\\TechnicalTests.UnitTests\\LevelOne\\inputEmpty.txt");
result.Should().Be(0);
}
#endregion



[Test]
public void GetSuperMagicNumber_Should_Return_Correct_Value_With_Input()
{
var listForTest = new List<char>();
listForTest.Add('k');
listForTest.Add('c');
var solution = new Solution.LevelOne(listForTest);


var result = solution.GetSuperMagicNumber("C:\\Users\\Mehdi\\Source\\Repos\\TechnicalTestsDotnet\\TechnicalTests\\TechnicalTests.UnitTests\\LevelOne\\input.txt");
result.Should().Be(3902304);
}


[Test]
public void GetSuperMagicNumber_Should_Return_Correct_Value_With_Input2()
{
var listForTest = new List<char>();
listForTest.Add('k');
listForTest.Add('c');
var solution = new Solution.LevelOne(listForTest);

var result = solution.GetSuperMagicNumber("C:\\Users\\Mehdi\\Source\\Repos\\TechnicalTestsDotnet\\TechnicalTests\\TechnicalTests.UnitTests\\LevelOne\\input2.txt");
result.Should().Be(5490);

}

[Test]
public void GetSuperMagicNumber_Should_Return_Correct_Value_With_Input3()
{
var listForTest = new List<char>();
listForTest.Add('k');
listForTest.Add('c');
var solution = new Solution.LevelOne(listForTest);

var result = solution.GetSuperMagicNumber("C:\\Users\\Mehdi\\Source\\Repos\\TechnicalTestsDotnet\\TechnicalTests\\TechnicalTests.UnitTests\\LevelOne\\input3.txt");
result.Should().Be(60126);

}


[Test]
public void GetSuperMagicNumber_Should_Return_Correct_Value_With_InputEmpty()
{
var listForTest = new List<char>();
listForTest.Add('k');
listForTest.Add('c');
var solution = new Solution.LevelOne(listForTest);

var result = solution.GetSuperMagicNumber("C:\\Users\\Mehdi\\Source\\Repos\\TechnicalTestsDotnet\\TechnicalTests\\TechnicalTests.UnitTests\\LevelOne\\inputEmpty.txt");
result.Should().Be(0);
}

}
}
Loading