Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Written program. #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,65 @@ 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 GetCurrentTime(string[] exactPostTime, string[] showPostTime)
{
// Add your code here.
throw new NotImplementedException();
string ans;
DateTime[] dateValues = new DateTime[exactPostTime.Length];
int hour=0, minute=0, second=0;
bool flag = true;
DateTime dateTime;
for (int i = 0; i < exactPostTime.Length; i++)
{
if (DateTime.TryParse(exactPostTime[i], out dateValues[i]))
{
string[] words = showPostTime[i].Split(' ');
if (flag)
{
//will be executed only once.
hour = dateValues[i].Hour;
minute = dateValues[i].Minute;
second = dateValues[i].Second;
flag = false;
}
else
{
//we will check wheather repeated time is given.
if (dateValues[i-1].ToString("HH:mm:ss").Equals(dateValues[i].ToString("HH:mm:ss")) && (!(showPostTime[i-1].Split(' ')[0].Equals(words[0])) || !(showPostTime[i-1].Split(' ')[1].Equals(words[1]))))
{
return "impossible";
}
}
if (words[1].Equals("hours"))
{
hour = dateValues[i].AddHours(int.Parse(words[0])).Hour;
}
else if (words[1].Equals("minutes"))
{
dateTime = dateValues[i].AddMinutes(int.Parse(words[0]));
hour = dateTime.Hour;
minute = dateTime.Minute;
second = dateValues[i].Second;
}
}
else
{
Console.WriteLine("Unable to convert '{0}' to a date.", exactPostTime[i]);
}
}
string h1 = ""+hour;
if (hour == 0) h1 = "00";
ans = h1 + ":" + minute;
ans += ":" + second;
return ans;
}
}
}