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

Solution - tavisca-pkadam #100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ bld/

# Visual Studio 2015/2017 cache/options directory
.vs/
.history/
.vscode/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

Expand Down
21 changes: 21 additions & 0 deletions Tavisca.Bootcamp.LanguageBasics.Exercise2/CustomTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Tavisca.Bootcamp.LanguageBasics.Exercise2
{
class CustomTime
{
public TimeSpan timeSpan;
public string time;
public string readAs;

public CustomTime(string time, string read_as)
{
this.time = time;
this.readAs = read_as;
TimeSpan.TryParse(time, out this.timeSpan);

}
}
}
112 changes: 102 additions & 10 deletions Tavisca.Bootcamp.LanguageBasics.Exercise2/Program.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,122 @@
using System;
using System.Text.RegularExpressions;
using System.Linq;

namespace Tavisca.Bootcamp.LanguageBasics.Exercise1
namespace Tavisca.Bootcamp.LanguageBasics.Exercise2
{
public static class Program
{
static void Main(string[] args)
{
Test(new[] {"12:12:12"}, new [] { "few seconds ago" }, "12:12:12");
Test(new[] { "12:12:12" }, new[] { "few seconds ago" }, "12:12:12");
Test(new[] { "23:23:23", "23:23:23" }, new[] { "59 minutes ago", "59 minutes ago" }, "00:22:23");
Test(new[] { "00:10:10", "00:10:10" }, new[] { "59 minutes ago", "1 hours ago" }, "impossible");
Test(new[] { "11:59:13", "11:13:23", "12:25:15" }, new[] { "few seconds ago", "46 minutes ago", "23 hours ago" }, "11:59:23");
Console.ReadKey(true);
`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)
{
var result = new TimeSpan();
var resultPrevious = new TimeSpan();

var cTime = new CustomTime[exactPostTime.Length];

for (int i = 0; i < exactPostTime.Length; i++)
{
cTime[i] = new CustomTime(exactPostTime[i], showPostTime[i]);

if (i > 0)
{
if (DifferentReadingsForSameTime(exactPostTime, showPostTime))
{
return "impossible";
}
}

result = CalculateTime(cTime, i);
result = MoreThanDay(result);

GetLowestTime(ref result, ref resultPrevious);

}
return result.ToString();
}

private static TimeSpan CalculateTime(CustomTime[] t, int i)
{
TimeSpan result;
if (t[i].readAs.Contains("seconds"))
{
result = CalculateForSeconds(t, i);
}
else if (t[i].readAs.Contains("minutes"))
{
result = CalculateForMinutes(t, i);
}
else
{
result = CalculateForHours(t, i);
}

return result;
}

private static bool DifferentReadingsForSameTime(string[] exactPostTime, string[] showPostTime)
{
return !int.Equals(exactPostTime.Distinct().Count(), showPostTime.Distinct().Count());
}

private static void GetLowestTime(ref TimeSpan result, ref TimeSpan resultPrevious)
{
if (result < resultPrevious)
{
result = resultPrevious;
}
else
{
resultPrevious = result;
}
}

private static TimeSpan MoreThanDay(TimeSpan result)
{
if (result > TimeSpan.Parse("1.00:00:00"))
{
result = (result - TimeSpan.FromDays(1)).Duration();
}

return result;
}

private static TimeSpan CalculateForHours(CustomTime[] cTime, int index)
{
TimeSpan result;
var hr = new TimeSpan(Int32.Parse(Regex.Split(cTime[index].readAs, @"\s")[0]), 0, 0);
result = cTime[index].timeSpan.Add(hr);
return result;
}

public static string GetCurrentTime(string[] exactPostTime, string[] showPostTime)
{
// Add your code here.
throw new NotImplementedException();
private static TimeSpan CalculateForMinutes(CustomTime[] cTime, int index)
{
TimeSpan result;
var min = new TimeSpan(0, Int32.Parse(Regex.Split(cTime[index].readAs, @"\s")[0]), 0);
result = cTime[index].timeSpan.Add(min);
return result;
}

private static TimeSpan CalculateForSeconds(CustomTime[] cTime, int index)
{
return cTime[index].timeSpan;
}
}
}