From 98bac46911b37703f746797d6c183600d19d3513 Mon Sep 17 00:00:00 2001 From: simonkarofillidis <72390986+simonkarofillidis@users.noreply.github.com> Date: Tue, 11 Jan 2022 16:41:28 +0200 Subject: [PATCH] chore(CSharp): add NaisMith rule (#660) --- algorithms/CSharp/README.md | 1 + algorithms/CSharp/src/Maths/naismith-rule.cs | 73 +++++++++++++++++++ algorithms/CSharp/test/Maths/naismith-rule.cs | 22 ++++++ 3 files changed, 96 insertions(+) create mode 100644 algorithms/CSharp/src/Maths/naismith-rule.cs create mode 100644 algorithms/CSharp/test/Maths/naismith-rule.cs diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index 41d87b38..821ca8b1 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -29,6 +29,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/ ## Maths - [Abundant Number](src/Maths/abundant-number.cs) +- [Naismith's Rule](src/Maths/naismith-rule.cs) ## Queues - [Queue Implementation Using Two Stacks](src/Queues/queue-implementation-using-two-stacks.cs) diff --git a/algorithms/CSharp/src/Maths/naismith-rule.cs b/algorithms/CSharp/src/Maths/naismith-rule.cs new file mode 100644 index 00000000..6dc96ac4 --- /dev/null +++ b/algorithms/CSharp/src/Maths/naismith-rule.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; + +namespace Algorithms.Maths +{ + public class NaismithsRule + { + public static double ReturnTime(int pathLenght, int upHill) + { + double timeCalculation = (pathLenght / 65) + (upHill / 10); + Math.Round(timeCalculation); + + return timeCalculation; + } + + public static void PrintInfo(string locationStart, string locationEnd, int pathLenght, int upHill) + { + double calculateKilometers = (double)pathLenght / 1000; + string displayTwoDecimals = String.Format("{0:0.##}", calculateKilometers); + + double time = ReturnTime(pathLenght, upHill); + + int exactHour = (int)time / 60; + int exactMinutes = (int)time - (exactHour * 60); + + Console.WriteLine("Start: " + locationStart + " ---> End: " + locationEnd + "\nDistance: " + pathLenght + + " meters or " + displayTwoDecimals + " kilometers.\nUphill distance: " + upHill + " meters."); + if (exactHour > 1) + { + Console.WriteLine("Time required: " + exactHour + " hours and " + exactMinutes + " minutes.\n"); + } + else if (exactHour == 0) + { + Console.WriteLine("Time required: " + exactHour + " hour and " + exactMinutes + " minutes.\n"); + } + else + { + Console.WriteLine("Time required: " + time + " minutes.\n"); + } + } + + public static void NaisMithRule(string locationStart, string locationEnd, int pathLenght, int UpHill, int DownHill) + { + double timeCalculation = (pathLenght / 65) + (UpHill / 10); + Math.Round(timeCalculation); + + if (String.Equals(locationStart, locationEnd)) // Looping path + { + PrintInfo(locationStart, locationEnd, pathLenght, UpHill); + Console.WriteLine("-----------------------------------------------------\n"); + } + else // Not looping path + { + PrintInfo(locationStart, locationEnd, pathLenght, UpHill); + PrintInfo(locationEnd, locationStart, pathLenght, DownHill); + Console.WriteLine("-----------------------------------------------------\n"); + } + } + + /* NaisMith's Rule is the total walking time that is equal to the time of crossing the distance to the level from + point A to point B at a constant speed of 65 meters per minute (~ 4Km / h) + a delay of 1 minute for every + 10 meters of ascent. + In practice, the results of Naismith's rule are usually considered the minimum time necessary to complete a route. */ + + public static void Main() + { + NaisMithRule("New York", "Washington", 120421, 1203, 531); // Test Case 1 + NaisMithRule("London", "York", 111091, 7100, 9801); // Test Case 2 + NaisMithRule("Rome", "Napoli", 81112, 2112, 1411); // Test Case 3 + NaisMithRule("Athens", "Athens", 20000, 481, 222); // Test Case 4 -> Looping Path + } + } +} \ No newline at end of file diff --git a/algorithms/CSharp/test/Maths/naismith-rule.cs b/algorithms/CSharp/test/Maths/naismith-rule.cs new file mode 100644 index 00000000..0eb1232e --- /dev/null +++ b/algorithms/CSharp/test/Maths/naismith-rule.cs @@ -0,0 +1,22 @@ +using NUnit.Framework; + +namespace Algorithms.Tests.Maths + +{ + [TestFixture] + public class NaismithsRule + { + [TestCase(121052, 5001, 2362)] + [TestCase(51022, 1023, 886)] + [TestCase(91022, 5944, 1994)] + [TestCase(34992, 421, 580)] + [TestCase(14012, 2338, 448)] + [TestCase(301221, 8300, 5464)] + + public void CalculateTime_ShouldGetExpectedResult(int pathLenght, int upHill, double expected) + { + double result = Algorithms.Maths.NaismithsRule.ReturnTime(pathLenght, upHill); + Assert.AreEqual(expected, result); + } + } +} \ No newline at end of file