From 0681f0484b58268e8a998d591c3d7a13dd27559d Mon Sep 17 00:00:00 2001 From: tavisca-schaitanya Date: Fri, 21 Jun 2019 19:41:01 +0530 Subject: [PATCH] After completing 2 cases --- .../Program.cs | 82 +++++++++++++++++-- 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs b/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs index 278d90d..1d181f5 100644 --- a/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs +++ b/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs @@ -1,5 +1,6 @@ using System; - +using System.Collections.Generic; +using System.Linq; namespace Tavisca.Bootcamp.LanguageBasics.Exercise1 { public static class Program @@ -13,18 +14,83 @@ static void Main(string[] args) Console.ReadKey(true); } - private static void Test(string[] postTimes, string[] showTimes, string expected) - { + private static void Test(string[] postTimes, string[] showTimes, string expected) + { var result = GetCurrentTime(postTimes, showTimes).Equals(expected) ? "PASS" : "FAIL"; var postTimesCsv = string.Join(", ", postTimes); var showTimesCsv = string.Join(", ", showTimes); - Console.WriteLine($"[{postTimesCsv}], [{showTimesCsv}] => {result}"); + Console.WriteLine($"[{postTimesCsv}], [{showTimesCsv}] => {result}"); + } + + //Function to add hours, minutes, seconds to given time + public static string AddTime(string postTime, int hours, int minutes, int seconds) + { + string[] splittedTime = postTime.Split(':'); + int currentSeconds = Convert.ToInt32(splittedTime[2]) + seconds; + int carryMinutes = currentSeconds/60; + currentSeconds %= 60; + int currentMinutes = Convert.ToInt32(splittedTime[1]) + minutes + carryMinutes; + int carryHours = currentMinutes/60; + currentMinutes %= 60; + int currentHours = Convert.ToInt32(splittedTime[0]) + hours + carryHours; + currentHours %= 24; + return new TimeSpan(currentHours, currentMinutes, currentSeconds).ToString(); } - public static string GetCurrentTime(string[] exactPostTime, string[] showPostTime) - { - // Add your code here. - throw new NotImplementedException(); + public static string GetCurrentTime(string[] exactPostTime, string[] showPostTime) + { + List startTime = new List(); + List endTime = new List(); + System.Console.WriteLine(startTime.Max()); + int i; + for(i = 0; i < exactPostTime.Length; i++) + { + if(showPostTime[i].Equals("few seconds ago")) + { + startTime.Add(AddTime(exactPostTime[i], 0, 0, 0)); + endTime.Add(AddTime(exactPostTime[i], 0, 0, 59)); + } + else if(showPostTime[i].Contains("minutes ago")) + { + int minutes = Convert.ToInt32(showPostTime[i].Split(' ')[0]); + startTime.Add(AddTime(exactPostTime[i], 0, minutes, 0)); + endTime.Add(AddTime(exactPostTime[i], 0, minutes, 59)); + } + else if(showPostTime[i].Contains("hours ago")) + { + int hours = Convert.ToInt32(showPostTime[i].Split(' ')[0]); + startTime.Add(AddTime(exactPostTime[i], hours, 0, 0)); + endTime.Add(AddTime(exactPostTime[i], hours, 59, 59)); + } + } + //Current Time should be between maximum startTime and minimum endTime + string currentStartTime = startTime.Max(); + string currentEndTime = endTime.Min(); + + //Case 1: StartTime is after 23:00:00 and EndTime is before 01:00:00 + if(string.Compare(currentStartTime, "23:00:00") > 0 && string.Compare(currentEndTime, "01:00:00") < 0) + { + //Check whether there is any maximum startTime between 00:00:00 and currentEndTime + currentStartTime = startTime.Where(s => string.Compare(s, "00:00:00") >= 0 && string.Compare(s, currentEndTime) == -1).Max(); + + //If there is no startTime after 00:00:00, then currentEndTime is lexicographically smaller + if(currentStartTime == null) + return currentEndTime; + //If there is startTime after 00:00:00, then currentStartTime is lexicographically smaller + else + return currentStartTime; + } + + //Case 2: StartTime is after 00:00:00 and EndTime is before 23:59:59 + else + { + //If startTime is greater than endTime then currentTime is impossible + if(string.Compare(currentStartTime, currentEndTime) > 0) + return "impossible"; + //StartTime is always lexicographically smaller than EndTime + else + return currentStartTime; + } } } }