C
C#11mo ago
Rémi

❔ Find last element in dictionnary, using a list as path

Hey, I have a dictionnary:
c#
Dictionary<string, object> tree => new()
{
{ "show", new Dictionary<string, object>() { { "10", null } } },
{ "clear", null },
{ "true", null },
{ "false", null}
};
c#
Dictionary<string, object> tree => new()
{
{ "show", new Dictionary<string, object>() { { "10", null } } },
{ "clear", null },
{ "true", null },
{ "false", null}
};
like this, and i wanna make a function that return me the next/end word. Like if I input 'sh' i wanna have 'ow', and if I input 'show' I wanna have 10, but if i input 'show 10' then only return a null value.
3 Replies
Rémi
Rémi11mo ago
for now ive done this, wich was only supposed to give the next word but tdoesn,'t work: private static string? LastOfTree(Dictionary<string, object> currentNode, List<string> path) { if (path.Count() == 0) { return null; } if (currentNode.ContainsKey(path[0])) { if (currentNode[path[0]] is Dictionary<string, object> dict) { return TraverseTree(dict, path.Skip(1).ToList()); } else { return path[0]; } } else { return null; } }
Moods
Moods11mo ago
Okay so If you inputed show as your path, it would check if you tree has the key path[0], so the char ‘s’ Which it does not There’s like, a handful of things wrong about this I don’t even think the approach is right Isn’t there a data structure for this too Yeah it’s a trie, I haven’t learned about it myself but I’d recommend looking at it if you haven’t already
Accord
Accord11mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.