diff --git "a/Exercice pratique - La carte aux tre\304\227sors.pdf" "b/Exercice pratique - La carte aux tre\304\227sors.pdf" new file mode 100644 index 0000000..54286de Binary files /dev/null and "b/Exercice pratique - La carte aux tre\304\227sors.pdf" differ diff --git a/LevelOne.cs b/LevelOne.cs new file mode 100644 index 0000000..6be253e --- /dev/null +++ b/LevelOne.cs @@ -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 _listOfCharToFindInFile; + + public LevelOne(List listOfCharToFindInFile) + { + + _listOfCharToFindInFile = new List(); + _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; + } + + /// + /// Method who count number off specific charactere + /// + /// + /// + /// number of occurence in a file + 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; + } + + /// + /// Method who get index of specific charactere and sum it + /// + /// + /// + /// + private int CountCharByPositon(string path, char charToCount) + { + int res = 0; + string file = File.ReadAllText(path); + List fileLine = file.Split('\n').ToList(); + List foundIndexesOfSpecificChar = new List(); + + 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; + } + + } +} diff --git a/LevelOne.txt b/LevelOne.txt new file mode 100644 index 0000000..87b7384 --- /dev/null +++ b/LevelOne.txt @@ -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 \ No newline at end of file diff --git a/SolutionTests.cs b/SolutionTests.cs new file mode 100644 index 0000000..fbb099f --- /dev/null +++ b/SolutionTests.cs @@ -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(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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); + } + + } +} diff --git a/input.txt b/input.txt new file mode 100644 index 0000000..374928e --- /dev/null +++ b/input.txt @@ -0,0 +1,200 @@ +Virgin +Tactic +Essence +Grand +Duel +Record +Ephemeral +Dogtooth +Crew +Orchard +Fall +Believer +Born +Weak +Edge +Americana +Graphic +Exception +Robot +Distant +Extremist +Hill +Astronaut +Lock +Elastic +Chair +Popular +Bauble +Whip +Bittersweet +Moth +Crash +Award +Murky +Power +Brood +Delicacy +Begging +Delete +Apple +Destructive +Wept +Intruder +Lemon +Falling +Duke +Pity +Aquatic +Enzyme +Poor +Island +Flash +Racoon +Aggression +Answer +Chef +Beard +Blackwater +Glass +Examiner +Hatch +Domination +Major +Predatory +Mustache +Hearse +Expansion +Everyone +Present +Entropy +Relearn +Noisemaker +Bug +Dent +Discipline +Burglary +Gaunt +Poetry +Honeybee +Force +Carrion +Rabbit +Everyone +Elephants +Virgin +Disintegration +Divinity +Distancing +Gimmick +Headquarters +Discontent +Cardinal +Quantum +Guzzling +Homicidal +Everyday +City +Attitude +Bigwig +Hothead +Felony +Fog +Glamorous +Gutter +Chromosome +Gravitational +Gangland +Chapterhouse +Baffling +Beyond +Plantation +Tyrant +Human +Privilege +Westwork +Daring +Common +Hallucination +Rerun +Fool +Foul +Tactic +Live +Cellular +Locus +Breakwater +Beehive +Under +Guillotine +Apocalyptic +Empirical +Dynamite +Gunplay +Business +Arcane +Foreign +Destructive +Gunrunner +Saint +Feather +Actress +Formulation +Deer +Blowtorch +Brood +Appeal +Engine +Honeybee +Gravitational +Norm +Glimmer +Hit +Agitator +Engine +Gestural +Intoxicant +Fearsome +Defect +Disintegration +Loop +Biggest +Wreckage +Bottomless +Creeper +Fraud +Apparition +Assembly +Greatest +Blurb +Attachment +Passenger +Becoming +Escape +Wax +Despair +Deer +Emerge +Carcinogenic +Ideal +Headlock +Blackwater +Bellyful +Nuclear +Cage +Domino +Haunt +Polite +Killjoy +Cold +Extract +Disfigured +Primitive +Fog +Feature +Balcony +Web +Evidence +Happiness +Volume +Eating \ No newline at end of file diff --git a/input2.txt b/input2.txt new file mode 100644 index 0000000..314bd94 --- /dev/null +++ b/input2.txt @@ -0,0 +1,7 @@ +Mock +Crabs +Kangaroo +Chicken +Worcestershire + +1845 \ No newline at end of file diff --git a/input3.txt b/input3.txt new file mode 100644 index 0000000..3715746 --- /dev/null +++ b/input3.txt @@ -0,0 +1,27 @@ +Mock +Crabs +Kangaroo +Chicken +Worcestershire +KIKO +CACAO + + + +(4 + 1 + 5 + 1 + 3) + +14 / 5 = 2 + +5 * 2 * 6 = 60 + +(3+1+1+4+4+1+3) + +17 / 7 = 2 + +2*7*9 126 + +60126 + + +5*6 30 +7*9 63 \ No newline at end of file diff --git a/inputEmpty.txt b/inputEmpty.txt new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/inputEmpty.txt @@ -0,0 +1 @@ + \ No newline at end of file