, , . , , . , , , .
List<Int32> numbering = new List<Int32>();
Int32 lastNestingLevel = -1;
foreach (String line in lines)
{
String[] parts = line.Split(new Char[] { ' ' }, 2);
Int32 currentNestingLevel = parts[0].Count(c => c == '.');
if (currentNestingLevel > lastNestingLevel)
{
numbering.Add(1);
}
else if (currentNestingLevel == lastNestingLevel)
{
numbering[currentNestingLevel] += 1; }
else if (currentNestingLevel < lastNestingLevel)
{
numbering.RemoveAt(numbering.Count - 1);
numbering[currentNestingLevel] += 1;
}
lastNestingLevel = currentNestingLevel;
String newNumbering = String.Join(".", numbering
.Select(n => n.ToString())
.ToArray());
Console.WriteLine(newNumbering + " " + parts[1]);
}
List<String> lines = new List<String>()
{
"1 Welcome",
"2 Whats New",
"2.2 Ideas",
"2.3 Others",
"2.3.2 Boats",
"2.4 Vehicals",
"2.5 Fruits"
};
.
1 Welcome
2 Whats New
2.1 Ideas
2.2 Others
2.2.1 Boats
2.3 Vehicals
2.4 Fruits
UPDATE
- , .
Dictionary<Int32, Int32> numbering = new Dictionary<Int32, Int32>();
Int32 lastNestingLevel = -1;
foreach (String line in lines)
{
String[] parts = line.Split(new Char[] { ' ' }, 2);
Int32 currentNestingLevel = parts[0].Count(c => c == '.');
if (currentNestingLevel > lastNestingLevel)
{
numbering[currentNestingLevel] = 1;
}
else
{
numbering[currentNestingLevel] += 1;
}
lastNestingLevel = currentNestingLevel;
String newNumbering = String.Join(".", numbering
.Where(n => n.Key <= currentNestingLevel)
.OrderBy(n => n.Key)
.Select(n => n.Value.ToString())
.ToArray());
Console.WriteLine(newNumbering + " " + parts[1]);
}
. , .
2.3.2.1 Vehicals
2.5 Fruits <= nesting level drops by two
, , , , , , .