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

Completed Assignment language-basics-02 #15

Open
wants to merge 1 commit 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
82 changes: 74 additions & 8 deletions Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;

using System.Collections.Generic;
using System.Linq;
namespace Tavisca.Bootcamp.LanguageBasics.Exercise1
{
public static class Program
Expand All @@ -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<string> startTime = new List<string>();
List<string> endTime = new List<string>();
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;
}
}
}
}