diff --git a/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs b/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs index 278d90d..83d051e 100644 --- a/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs +++ b/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs @@ -13,18 +13,112 @@ 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}"); } - public static string GetCurrentTime(string[] exactPostTime, string[] showPostTime) - { + public static string AddTime(string postTime, string type, string showTime = ""){ + string[] time = postTime.Split(':'); + string temp = string.Empty; + int finalSecs = Int32.Parse(time[2]+59)%60; + int finalMins = Int32.Parse(time[1]); + int finalHours = Int32.Parse(time[0]); + + // Seconds ago condition + if(type.Equals("s")){ + + if(finalSecs < 59){ + finalMins += 1; + finalMins %= 60; + } + if(finalMins == 0){ + finalHours += 1; + finalHours %= 24; + } + //post time is always greater except below case + //when final hour is 00 (after adding 59 to the second's value) and post time hour is not 00 + if(finalHours == 0 && Int32.Parse(time[0]) != 0){ + time[0] = finalHours.ToString(); + time[1] = finalMins.ToString(); + time[2] = "0"; + } + for(int t=0; t < time.Length; t++){ + if(time[t].Length < 2){ + time[t] = "0"+time[t]; + } + } + + temp = time[0] + ":" + time[1] + ":" + time[2]; + //Console.WriteLine("Seconds " + temp); + } + // Minutes ago condition + else if(type.Equals("m")){ + int minutes = Int32.Parse(showTime.Split()[0]); + finalMins += minutes; + if(finalMins > 59){ + time[0] = ((Int32.Parse(time[0])+1)%24).ToString(); + } + finalMins %= 60; + time[1] = finalMins.ToString(); + + //checking which time is suitable according to other array values + temp = AddTime(time[0]+":"+time[1]+":"+time[2],"s"); + //Console.WriteLine("Minutes " + temp); + } + // Hours ago condition + else if(type.Equals("h")){ + //value of hours in showtime + int hours = Int32.Parse(showTime.Split()[0]); + + finalHours += hours; + finalHours %= 24; + + //updating hours in the time variable + time[0] = finalHours.ToString(); + + //checking which time is suitable according to other array values + temp = AddTime(time[0]+":"+time[1]+":"+time[2],"m", "59 minutes ago"); + //Console.WriteLine("Hours " + temp); + } + + return temp; + } + + public static string GetCurrentTime(string[] exactPostTime, string[] showPostTime) + { // Add your code here. - throw new NotImplementedException(); + //throw new NotImplementedException(); + string finalTime = string.Empty; + string firstOut = string.Empty; + for(int i = 0; i < exactPostTime.Length; i++){ + string postTime = exactPostTime[i]; + string showTime = showPostTime[i]; + + if(showTime.Equals("few seconds ago")){ + finalTime = AddTime(postTime, "s"); + } + else if(showTime.Contains("minutes ago")){ + finalTime = AddTime(postTime, "m", showTime); + } + else if(showTime.Contains("hours ago")){ + finalTime = AddTime(postTime, "h", showTime); + } + + // if this is first in the array + if(firstOut.Equals(string.Empty)){ + firstOut = finalTime; + } + // subsequent values in array of current test case + else if(!firstOut.Equals(finalTime)){ + finalTime = "impossible"; + } + } + + return finalTime; } } }