You have probably seen these signs on the doors of various shops:
OPENING HOURS
mon-fri 0900-1800
sat-sun 1100-1530
The task here is to generate a sign like that, grouping consecutive days with the same opening hours, from a list of opening hours for the whole week. Note that the week "wraps around" for what is considered consecutive.
Input:
- 7 elements, representing the opening hours for each day in a week, starting with Monday.
- Each element is a string, on the form XXXX-XXXX
Example input:
0900-1800 0900-1800 0930-1730 0930-1730 0900-1500 1100-1500 1100-1500
- It's ok to send the input as an array (for instance as input to a function if you don't read from stdin)
Output:
- A list of opening hours, where consecutive days with the same opening hours are shown as a range. Note that sunday (the last day) and monday (the first day) also are consecutive days.
- A day where the day doesn't have similar opening hours to the days before or after is printed by itself
- Days are specified as three lowercase letters: mon tue wed thu fri sat sun
- Remember that first element in the input corresponds to mon, next to tue etc.
- Opening hours are shown as in the input
Two examples
mon-fri 0900-1800, sat-sun 1100-1500 mon-wed 1030-1530, thu 100-1800, fri-sun 1200-1630
The output should be sorted, so ranges appear in the order the days do in the week. Monday is preferred to be first, but it may happen it is not first in a group because the week wraps. So in this case tue is the first range.
tue-fri 0900-1800, sat-mon 1100-1500
Don't group unless consecutive, here wednesday and friday have same opening hours but are separated by a Thursday with different opening hours so they are listed by themselves.
mon-tue 1000-1200, wed 0900-1500, thu 1000-1800, fri 0900-1500, sat-sun 1000-1500
- The output can be either comma-separated as the examples here, or separated by a newline as in the example on top.
Test cases
First line is input, second line is expected output
0900-1800 0900-1800 0900-1800 0900-1800 0900-1800 1100-1500 1100-1500
mon-fri 0900-1800, sat-sun 1100-1500
0900-1800 0900-1800 0900-1800 0930-1700 0900-1800 1100-1500 1100-1500
mon-wed 0900-1800, thu 0930-1700, fri 0900-1800, sat-sun 1100-1500
1100-1500 0900-1800 0900-1800 0900-1800 0900-1800 1100-1500 1100-1500
tue-fri 0900-1800, sat-mon 1100-1500
1100-1500 1100-1500 0900-1800 0900-1800 0900-1800 0900-1800 1100-1500
wed-sat 0900-1800, sun-tue 1100-1500
1200-1500 1100-1500 0900-1800 0900-1800 0900-1800 0900-1800 1100-1500
mon 1200-1500, tue 1100-1500, wed-sat 0900-1800, sun 1100-1500
Rules
This is code-golf, so the shortest answer in bytes wins.