Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. /**
  2. * Given the next set of week of days as strings in the given order: <code>Mon, Tue, Wed, Thu, Fri, Sat, Sun</code> <br/>
  3. * and given a positive integer <code>K</code> in the range <code>[0..500]</code>. <br/>
  4. * Find the week of day for a given day plus K days.<br/>
  5. * For example:<br/>
  6. * <code>Wed + 5 = Mon</code>
  7. * <code>Sun + 0 = Sun</code>
  8. * <code>Tue + 28 = Tue</code>
  9. * <br/><br/>
  10. * Max time for resolution: 15 minutes.
  11. */
  12. class WeekOfDayNextToK {
  13.  
  14. private int daysOfWeekCount = 7;
  15. private String[] daysOfWeek = new String[] { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
  16.  
  17. public static void main(String[] args) {
  18.  
  19. WeekOfDayNextToK solution = new WeekOfDayNextToK();
  20.  
  21. System.out.println(solution.solution("Wed", 5)); // Mon
  22. System.out.println(solution.solution("Sun", 0)); // Sun
  23. System.out.println(solution.solution("Tue", 28)); // Tue
  24. }
  25.  
  26. public String solution(String S, int K) {
  27.  
  28. // find index of S
  29. int indexOfS = getIndexForDay(S, daysOfWeek);
  30.  
  31. // apply module over K to get the offset
  32. int indexForSPlusK = (indexOfS + K) % daysOfWeekCount;
  33.  
  34. return daysOfWeek[indexForSPlusK];
  35. }
  36.  
  37. private int getIndexForDay(String S, String[] daysOfWeek) {
  38. for (int i=0, c=daysOfWeek.length; i < c; ++i) {
  39. if (daysOfWeek[i].equals(S)) {
  40. return i;
  41. }
  42. }
  43. return 0;
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement