Partition

CSharp · Console

/Program.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
					
public class Program
{
	public class Item
	{
		public int Id { get; set; }
		public int Val { get; set; }
	}
	
	public static void Main()
	{
		var list = new List<Item>(){
			new Item{ Id = 1, Val = 0 },
			new Item{ Id = 2, Val = 0 },
			new Item{ Id = 3, Val = 1 },
			new Item{ Id = 4, Val = 1 },
			new Item{ Id = 5, Val = 1 },
			new Item{ Id = 6, Val = 1 },
			new Item{ Id = 7, Val = 0 },
			new Item{ Id = 8, Val = 0 },
			new Item{ Id = 9, Val = 0 },
			new Item{ Id = 10, Val = 0 },
			new Item{ Id = 11, Val = 1 },
			new Item{ Id = 12, Val = 1 },
			new Item{ Id = 13, Val = 0 },
			new Item{ Id = 14, Val = 0 },
		};
		
		var prev = 0;
		List<int> bag = null;
		var result = new List<List<int>>();
		foreach(var item in list)
		{
			if(item.Val == 1)
			{
				if(prev == 0)
				{
					if(bag != null)
						result.Add(bag);
					bag = new List<int>();
				}						
				bag.Add(item.Id);
			}
			prev = item.Val;
		}
		if(bag != null)
			result.Add(bag);
		
		result.ForEach(x => {           
   			Console.WriteLine(String.Join(", ", x));
		});
	}
}
;