I am using C# to create a function that takes in a list of NodaTime.IsoDayOfWeek
days. I want to group the input into groups of consecutive days.
For example, the following lists should give the following output:
{ Mon, Tue } => { { Mon, Tue } }
{ Mon, Wed } => { { Mon }, { Wed } }
{ Mon, Tue, Fri, Sat } => { { Mon, Tue }, { Fri, Sat } }
{ Mon, Wed, Fri, Sun } => { { Sun, Mon }, { Wed }, { Fri } }
{ Mon, Tue, Wed, Thu, Fri, Sat, Sun } => { { Mon, Tue, Wed, Thu, Fri, Sat, Sun } }
Notice that Sunday and Monday are consecutive, so the list is a closed loop. In addition, the resulting lists should be ordered such that the first day directly follows a day that is not included in the input list (or Monday if the complete list is included).
Mauricio Scheffer published a great extension method to group consecutive integers here:
public static IEnumerable<IEnumerable<int>> GroupConsecutive(this IEnumerable<int> list) {
var group = new List<int>();
foreach (var i in list) {
if (group.Count == 0 || i - group[group.Count - 1] <= 1)
group.Add(i);
else {
yield return group;
group = new List<int> {i};
}
}
yield return group;
}
However I can't figure out how to modify this to group days since Sunday and Monday are also consecutive. How can I group consecutive days where Sunday and Monday are also considered consecutive?