From 0088cf511bb71c0ae35f0a941878ce5c7c4db28c Mon Sep 17 00:00:00 2001 From: tavisca-skarmakar Date: Thu, 27 Jun 2019 11:20:34 +0530 Subject: [PATCH] Added implementation of GetCurrentTime --- .../Program.cs | 64 +++++++++++++++++-- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs b/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs index 278d90d..6c4fc73 100644 --- a/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs +++ b/Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace Tavisca.Bootcamp.LanguageBasics.Exercise1 { @@ -13,18 +14,67 @@ 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) - { - // Add your code here. - throw new NotImplementedException(); + public static string GetCurrentTime(string[] exactPostTime, string[] showPostTime) + { + + var currentTime = new List(); + + for(int i=0; i< exactPostTime.Length; i++){ + + for (int j = 0; j < exactPostTime.Length; j++) + { + if(exactPostTime[i] == exactPostTime[j]){ + if(showPostTime[i] != showPostTime[j]) return "impossible"; + } + } + + TimeSpan timeSpan = TimeSpan.Parse(exactPostTime[i]); + /* Console.WriteLine(timeSpan); + + TimeSpan time = new TimeSpan(timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds); + Console.WriteLine(timeSpan.Hours); + Console.WriteLine(TimeSpan.FromHours(timeSpan.Hours)); + Console.WriteLine(timeSpan.Minutes); + Console.WriteLine(TimeSpan.FromMinutes(timeSpan.Minutes)); + Console.WriteLine(timeSpan.Seconds); + Console.WriteLine(TimeSpan.FromSeconds(timeSpan.Seconds)); */ + + if(showPostTime[i].Contains("seconds")){ + TimeSpan time = new TimeSpan(timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds); + currentTime.Add(time); + } + else if(showPostTime[i].Contains("minutes")){ + + string minute = showPostTime[i].Split(' ')[0]; + timeSpan = timeSpan.Add(TimeSpan.FromMinutes(double.Parse(minute))); + System.Console.WriteLine(timeSpan); + TimeSpan time = new TimeSpan(timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds); + + System.Console.WriteLine(time); + + currentTime.Add(time); + } + else if(showPostTime[i].Contains("hours")){ + string hours = showPostTime[i].Split(' ')[0]; + timeSpan = timeSpan.Add(TimeSpan.FromHours(double.Parse(hours))); + System.Console.WriteLine(timeSpan); + TimeSpan time = new TimeSpan(timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds); + + System.Console.WriteLine(time); + + currentTime.Add(time); + } + } + currentTime.Sort(); + return currentTime[currentTime.Count-1].ToString(); } } }