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

Update on Second Assignment #38

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
106 changes: 100 additions & 6 deletions Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}