Created
January 7, 2024 16:19
-
-
Save smothiki/a0b64da6d5f65d8060fe2f70586412d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nput - ("mon 10:00 am", mon 11:00 am) | |
Output - [11005, 11010, 11015...11100] | |
Output starts with 1 if the day is monday, 2 if tuesday and so on till 7 for sunday | |
Append 5 min interval times to that till the end time | |
So here it is 10:05 as first case, so its written as 11005 | |
2nd is 10:10 so its written as 11010 | |
... | |
... | |
Stop at 11100 | |
My solution: | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class StartEndTimeInterval { | |
static class Time { | |
int day = -1; | |
int hour = 0; | |
int min = 0; | |
boolean am = false; | |
public Time(int day, int hour, int min, boolean am) { | |
this.day = day; | |
this.hour = hour; | |
this.min = min; | |
this.am = am; | |
} | |
void add(int mins) { | |
hour += (mins + min)/60; | |
min = (mins + min)%60; | |
if(hour >=13) { | |
am = !am; | |
hour = hour%12; | |
if(am) | |
day += 1; | |
} | |
} | |
int getNumeric() { | |
return (((day*100)+hour)*100) + min; | |
} | |
boolean equals(Time t2) { | |
return day == t2.day && | |
hour == t2.hour && | |
min == t2.min && | |
Boolean.compare(am, t2.am) == 0; | |
} | |
} | |
static Map<String, Integer> mapDays = new HashMap<>(); | |
static List<Integer> getIntervals(String start, String end) { | |
mapDays(); | |
List<Integer> intervals = new ArrayList<>(); | |
Time startTime = getTime(start); | |
Time endTime = getTime(end); | |
while (!startTime.equals(endTime)) { | |
startTime.add(5); | |
System.out.println(startTime.getNumeric()); | |
intervals.add(startTime.getNumeric()); | |
} | |
return intervals; | |
} | |
static Time getTime(String time) { | |
String[] info = time.split(" "); | |
String[] hrMin = info[1].split(":"); | |
boolean am = info[2].equals("am"); | |
return new Time(mapDays.get(info[0]), Integer.parseInt(hrMin[0]), Integer.parseInt(hrMin[1]), am); | |
} | |
static void mapDays() { | |
mapDays.put("mon", 1); | |
mapDays.put("tue", 2); | |
mapDays.put("wed", 3); | |
mapDays.put("thu", 4); | |
mapDays.put("fri", 5); | |
mapDays.put("sat", 6); | |
mapDays.put("sun", 7); | |
} | |
public static void main(String[] args) { | |
for(Integer interval : getIntervals("mon 10:00 pm", "tue 11:00 pm")) { | |
System.out.println("->" + interval); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment