Created
December 3, 2014 15:20
-
-
Save JamisonWhite/02f18eb9e0f89c747a23 to your computer and use it in GitHub Desktop.
Get all sequences of labels for a domain. This finds "paypal.com" in "paypal.com.hacker.net".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Get all sequences of labels for a domain. | |
/// This finds "paypal.com" in "paypal.com.hacker.net". | |
/// </summary> | |
/// <remarks> | |
/// Inner loop is reversed so that returned list looks like a depth first search. | |
/// This is just a personal preference. | |
/// </remarks> | |
/// <param name="domain"></param> | |
/// <returns></returns> | |
public static IEnumerable<string> GetDomainSequences(string domain) | |
{ | |
var parts = domain.Split('.').ToArray(); | |
var labels = new List<String>(Enumerable.Range(0, parts.Length).Sum(x => x + 1)); | |
for (var i = 0; i <= parts.Length; i++) | |
{ | |
for (var j = parts.Length - i; j >= 1; j--) | |
{ | |
labels.Add(String.Join(".", parts.Skip(i).Take(j))); | |
} | |
} | |
return labels; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment