我有一個物件,它有一個字串串列,其中每個字串代表一個區域(NUTS 代碼)。前任。
["SE","SE12","SE124"]
我要做的是獲得最一般和最具體的一個(我不知道我是否有道理)我會寫一些輸入示例以及預期的輸出是什么,以便更清楚我意思是。
input1 : ["SE", "SE123", "SE124", "SE123456", "SE123456789"],
input2 : ["SE", "SE2", "SE123", "SE123456", "SE123456789"],
input3 : ["SE", "SE123", "SE123456", "SE123456789"],
input4 : ["SE","FI", "SE2"]
預期的輸出應該是:輸出1 =>“SE12”,輸出2 =>“SE”,ouptut3 =>“SE123456789”,輸出=>“”。
我使用了不同的方法,但它似乎比我想象的要棘手。
我的方法目前看起來像這樣:
public static string GetSpecificNuts(IList<string> nuts)
{
var outNuts = "";
var annNuts = nuts.Distinct().ToList();
if (annNuts.Any())
{
if (annNuts.Count() == 1)
{
outNuts = annNuts.SingleOrDefault();
}
else
{
var grouped = annNuts.GroupBy(n => n.Length).OrderByDescending(n=>n.Key).ToList();
var highest = grouped.Select(g => g.Key).FirstOrDefault();
var highestGroup = grouped?.SingleOrDefault(g => g.Key == highest)?.ToList();
var length = highestGroup?.Count;
if (length == 1)
{
var highestNuts = highestGroup?.SingleOrDefault();
var contained = grouped?.Where(n => n.Key != highest).SelectMany(g => g.ToList()).Where(s => highestNuts.StartsWith(s)).OrderByDescending(s=>s.Length);
var firstContained = contained.FirstOrDefault();
if (!string.IsNullOrWhiteSpace(firstContained))
{
outNuts = firstContained;
}
}
while (length > 1)
{
var deducted = new List<string>();
highestGroup?.ForEach(i => { deducted.Add(i.Length > 2 ? i.Remove(i.Length - 1, 1) : i); });
var distinct = deducted?.Distinct().ToList();
length = distinct?.Count;
highestGroup = distinct;
if (length == 1)
{
outNuts = distinct?.SingleOrDefault();
}
}
}
}
return outNuts;
}
有什么想法嗎?
編輯以獲得更多解釋:將前 2 個字母之后的數字視為樹視圖。第一個數字代表一組州,第二個代表一個州,第三個代表一個區,第四個代表自治市..等等。我需要獲得最具體的區域,我在 input3 中實作了這一點。但如果串列有前。2個或更多不同的地區,然后我需要獲得代表該州的數字。再有 2 個不同的狀態,然后我需要獲得代表狀態組的數字。2個或更多不同的州組然后我需要得到代表國家的前2個字母。2 或國家代碼 ex ("SE","FI") 那么輸出應該是一個空字串。
uj5u.com熱心網友回復:
為了澄清我認為您正在尋找的內容:您希望找到代表最具體的部門的 NUTS 代碼,該部門要么對串列中的所有人通用,要么所有其他 NUTS 代碼是. 本質上,在 k-ary 樹中找到所有葉節點的路徑都經過的最深節點:
SE
\
5
\
7 <-- Most specific in the common branch (SE57)
/ \
4 2
SE
\
5
\
7
\
2 <-- Most specific in the common branch (SE572)
一種解決方案可能如下(給方法起一個好名字有點棘手):
public static string GetDeepestNodeInCommonBranch(IList<string> nutsCodes)
{
// Build a tree with the depth being the specificity (index in the code) of
// the country subdivisions and the nodes at each level being the different
// divisions at that level/specificity (different numbers at the same index).
var levels = nutsCodes.Distinct(StringComparer.OrdinalIgnoreCase)
.SelectMany(nc => nc.Select((c, i) => new {Character = c, Index = i}))
.GroupBy(obj => obj.Index)
// Walk the tree from the top
.OrderBy(g => g.Key);
var commonDiv = string.Empty;
foreach (var level in levels)
{
var divisions = level.Select(obj => obj.Character).Distinct().ToList();
// If the tree branches out here, the division at the last level is the
// most specific division common for all
if (divisions.Count > 1)
{
// Only return full country codes
return commonDiv.Length >= 2 ? commonDiv : string.Empty;
}
commonDiv = divisions.Single();
}
return commonDiv;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/532858.html
標籤:C#。网细绳林克
