C # line numbering

I am trying to develop a C # algorithm that can take a list of URL arrays and output them to a numbered list of paths.

As you can imagine, I need help. Does anyone have any logic suggestions for creating this list?

Result:

1 - http://www.example.com/aboutus 1.2 - http://www.example.com/aboutus/page1 1.3 - http://www.example.com/aboutus/page2 1.3.1 - http://www.example.com/aboutus/page2/page3 1.3.1.1 - http://www.example.com/aboutus/page2/page3/page4 1.3.2 - http://www.example.com/aboutus/page5/page6 1.3.2.1 - http://www.example.com/aboutus/page5/page7/page9 1.3.2.2 - http://www.example.com/aboutus/page5/page8/page10 1.4 - http://www.example.com/aboutus/page10 1.4.1 - http://www.example.com/aboutus/page10/page11 1.4.2 - http://www.example.com/aboutus/page10/page12 1.1.5 - http://www.example.com/aboutus/page13 1.1.6 - http://www.example.com/aboutus/page14 1.1.6.1 - http://www.example.com/aboutus/page14/page15 1.1.6.2 - http://www.example.com/aboutus/page14/page16 1.1.6.3 - http://www.example.com/aboutus/page14/page17 

... etc.

+6
source share
4 answers

You will probably have to turn off the parameters of the protocol string and the query, so +1 we recommend using the System.URI class to handle this.

As for printing in the form of a tree, a direct approach is to use Dictionary<string, string> to maintain the relationship of the child with the parent (value).

Another way is to use List<T>.Sort , for example. eg:

 public static void Print(List<string> list) { var path = new Stack<string>(); var count = new Stack<int>(); path.Push(""); count.Push(0); list.Sort(new Comparison<string>(UrlComparison)); foreach (var x in list) { while (!x.StartsWith(path.Peek())) { path.Pop(); count.Pop(); } count.Push(count.Pop() + 1); foreach(var n in count.Reverse()) Console.Write("{0}.", n); Console.WriteLine(" {0}", x); path.Push(x); count.Push(0); } } 

Unfortunately, p.campbell right, it actually requires a normal comparison, which makes this implementation quite successful, but more cumbersome ( ?:-abuse warning):

 public static int UrlComparison(string x, string y) { if (x == null && y == null) return 0; if (x == null) return -1; if (y == null) return 1; for(int n = 0; n < Math.Min(x.Length, y.Length); n++) { char cx = x[n], cy = y[n]; if(cx == cy) continue; return (cx == '/' || cx == '.' || cx == '?') ? -1 : (cy == '/' || cy == '.' || cy == '?') ? 1 : (cx > cy) ? 1 : -1; } return (x.Length == y.Length) ? 0 : (x.Length > y.Length) ? 1 : -1; } 

PS: Just to express a denial, I believe the Stacks logic is consize, but a little harder to understand. In a long-term project, I would stick to a parent dictionary.

+3
source

Take a look at the System.URI class. It should have some methods and properties that should be useful, such as a segment property, which flattenes the uri into its segmented parts (mostly slash separation). You can create a list of segment arrays, sort the list, and then simply iterate over the list that controls the numbers, depending on whether the index segments of the current list correspond to the previous segments of the list index.

+8
source

Perhaps this will be the Generic Tree Collection .

The Code Project has several collections of trees: one , two .

+2
source

I think you need to implement some kind of collection of trees to process the order. Because if you added a new link called http://www.example.com , it will become 1 instead of http://www.example.com/aboutus .

You can then print the tree traversal in order, and it will be extremely simple.

0
source

Source: https://habr.com/ru/post/898388/


All Articles