Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. List<TimePeriod> GetTimesAvail(String person)
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8.  
  9. namespace JobInterviewTests
  10. {
  11.  
  12. [TestClass]
  13. public class Question
  14. {
  15. [TestMethod]
  16. public void TestMethod1()
  17. {
  18. List<string> people = new List<string> { "personA", "personB", "personC" };
  19. List<TimePeriod> result = GetTimeSlotForAll(people);
  20. Assert.AreEqual(1,result[0].Start.Hour);
  21. Assert.AreEqual(0,result[0].Start.Minute);
  22. Assert.AreEqual(1,result[0].End.Hour);
  23. Assert.AreEqual(30,result[0].End.Minute);
  24.  
  25. Assert.AreEqual(3, result[1].Start.Hour);
  26. Assert.AreEqual(0, result[1].Start.Minute);
  27. Assert.AreEqual(3, result[1].End.Hour);
  28. Assert.AreEqual(30, result[1].End.Minute);
  29. }
  30.  
  31. private List<TimePeriod> GetTimeSlotForAll(List<string> people)
  32. {
  33. if (people == null || people.Count == 0)
  34. {
  35. return new List<TimePeriod>();
  36. }
  37. //future improvment -- suggested
  38. //List<List<TimePeriod>> tempTimeList = new List<List<TimePeriod>>();
  39. //foreach (var person in people)
  40. //{
  41. // var personList = Utiliies.GetTimesAvail(person);
  42. // tempTimeList.Add(personList);
  43. //}
  44. //List<Dictionary<string, List<TimePeriod>>> dictionaryList = new List<Dictionary<string, List<TimePeriod>>>();
  45. //foreach (var list in tempTimeList)
  46. //{
  47. // //key is the day/month/year
  48. // Dictionary<string, List<TimePeriod>> personDictionary = new Dictionary<string, List<TimePeriod>>();
  49. // foreach (var time in list)
  50. // {
  51. // if (personDictionary.ContainsKey(time.ToDateTimeDay()))
  52. // {
  53. // personDictionary[time.ToDateTimeDay()].Add(time);
  54. // }
  55. // else
  56. // {
  57. // personDictionary[time.ToDateTimeDay()] = new List<TimePeriod>();
  58. // personDictionary[time.ToDateTimeDay()].Add(time);
  59. // }
  60. // }
  61. // dictionaryList.Add(personDictionary);
  62. //}
  63.  
  64. //place the first person meetings a the first result
  65. List<TimePeriod> firstPersonList = Utiliies.GetTimesAvail(people.FirstOrDefault());
  66. List<TimePeriod> result = new List<TimePeriod>();
  67. //intersect the meetings with the others
  68. for (int i = 1; i < people.Count; i++)
  69. {
  70. List<TimePeriod> secondPersonList = Utiliies.GetTimesAvail(people[i]);
  71. foreach (var secondSlot in secondPersonList)
  72. {
  73. foreach (var firstSlot in firstPersonList)
  74. {
  75. if (secondSlot.SameDay(firstSlot))
  76. {
  77. CheckHourIntersections(firstSlot, secondSlot, result);
  78. }
  79. }
  80. }
  81. //copy the result into the first person
  82. firstPersonList.Clear();
  83. foreach (var timeSlot in result)
  84. {
  85. firstPersonList.Add(new TimePeriod(timeSlot.Start, timeSlot.End));
  86. }
  87. //clear result
  88. result.Clear();
  89. }
  90. return firstPersonList;
  91. }
  92.  
  93. private static void CheckHourIntersections(TimePeriod firstSlot, TimePeriod secondSlot, List<TimePeriod> result)
  94. {
  95. // check all the different interval intersections
  96. //one intersects with the start of anothesr
  97. //[-----] //firstSlot
  98. // [------] //secondSlot
  99. // 00:59 -> 1:30 --- 01:00 -> 01:45
  100. //also cover
  101. //[-----] //firstSlot
  102. //[------] //secondSlot
  103. //also cover
  104. //[-------] //firstSlot
  105. // [------] //secondSlot
  106. if (((firstSlot.Start.Hour < secondSlot.Start.Hour) || (firstSlot.Start.Hour == secondSlot.Start.Hour && firstSlot.Start.Minute > secondSlot.Start.Minute))
  107. && ((firstSlot.End.Hour < secondSlot.End.Hour) ||(firstSlot.End.Hour == secondSlot.End.Hour && firstSlot.End.Minute < secondSlot.End.Minute))
  108. && ((secondSlot.Start.Hour < firstSlot.End.Hour) || (firstSlot.End.Hour == secondSlot.End.Hour && firstSlot.End.Minute < secondSlot.End.Minute)) )
  109. {
  110. result.Add(new TimePeriod(secondSlot.Start, firstSlot.End));
  111. return;
  112. }
  113. // [----] //firstSlot //02:00 -> 02:30
  114. //[------] //secondSlot 01:00->01:45
  115. if (((firstSlot.Start.Hour > secondSlot.Start.Hour) ||(firstSlot.Start.Hour == secondSlot.Start.Hour && firstSlot.Start.Minute >= secondSlot.Start.Minute))
  116. && ((firstSlot.End.Hour < secondSlot.End.Hour)|| (firstSlot.End.Hour == secondSlot.End.Hour && firstSlot.End.Minute < secondSlot.End.Minute)))
  117. {
  118. result.Add(new TimePeriod(firstSlot.Start, firstSlot.End));
  119. return;
  120. }
  121. // [----] //firstSlot
  122. //[------] //secondSlot
  123. if (((firstSlot.Start.Hour > secondSlot.Start.Hour && firstSlot.Start.Minute < secondSlot.Start.Minute) || (firstSlot.Start.Hour == secondSlot.Start.Hour && firstSlot.Start.Minute > secondSlot.Start.Minute))
  124. && ((firstSlot.End.Hour > secondSlot.End.Hour && firstSlot.End.Minute < secondSlot.End.Minute) || (firstSlot.End.Hour == secondSlot.End.Hour && firstSlot.End.Minute > secondSlot.End.Minute)))
  125. {
  126. result.Add(new TimePeriod(firstSlot.Start, secondSlot.End));
  127. }
  128. }
  129. }
  130. [DebuggerDisplay("{Start.Hour}:{Start.Minute}->{End.Hour}:{End.Minute}")]
  131. public class TimePeriod
  132. {
  133. public DateTime Start { get; set; }
  134. public DateTime End { get; set; }
  135.  
  136. public TimePeriod(DateTime start, DateTime end)
  137. {
  138. Start = start;
  139. End = end;
  140. }
  141.  
  142. public bool SameDay(TimePeriod other)
  143. {
  144. return this.Start.Year == other.Start.Year && this.Start.Month == other.Start.Month &&
  145. this.Start.Day == other.Start.Day;
  146. }
  147. public string ToDateTimeDay()
  148. {
  149. return Start.ToShortDateString();
  150. }
  151. }
  152.  
  153. public static class Utiliies
  154. {
  155. public static List<TimePeriod> GetTimesAvail(String person)
  156. {
  157. var res = new List<TimePeriod>();
  158. if (person == "personA")
  159. {
  160. res.Add(new TimePeriod(new DateTime(2017, 3, 23, 0, 59, 00), new DateTime(2017, 3, 23, 1, 30, 00)));
  161. res.Add(new TimePeriod(new DateTime(2017, 3, 23, 2, 00, 00), new DateTime(2017, 3, 23, 2, 30, 00)));
  162. res.Add(new TimePeriod(new DateTime(2017, 3, 23, 3, 00, 00), new DateTime(2017, 3, 23, 3, 30, 00)));
  163. }
  164. if (person == "personB")
  165. {
  166. res.Add(new TimePeriod(new DateTime(2017, 3, 23, 1, 00, 00), new DateTime(2017, 3, 23, 1, 45, 00)));
  167. res.Add(new TimePeriod(new DateTime(2017, 3, 23, 3, 00, 00), new DateTime(2017, 3, 23, 4, 00, 00)));
  168. }
  169. if (person == "personC")
  170. {
  171. res.Add(new TimePeriod(new DateTime(2017, 3, 23, 00, 00, 00), new DateTime(2017, 3, 23, 14, 00, 00)));
  172. }
  173. return res;
  174. }
  175. }
  176.  
  177.  
  178. }
  179.  
  180. List<List<TimePeriod>> temptimeList = new List<List<TimePeriod>>();
  181. foreach(var person in people)
  182. {
  183. var personList = getTimesAvail(person);
  184. tempTimelist.Add(personList);
  185. }
  186.  
  187. List<Dictionary<DateTime, TimePeriod>> dictionaryList = new List<Dictionary<DateTime, TimePeriod>>();
  188. foreach(var list in temptimeList)
  189. {
  190. Dictionary <DateTime, TimePeriod> personDictionary = new <DateTime, TimePeriod>();
  191. foreach(var time in list)
  192. {
  193. personDictionary.Add(time.ToDateTimeDay(), time);
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement