Created
July 19, 2023 01:04
-
-
Save jianminchen/98e6b3175a6d01d35c18b2f6f05011be to your computer and use it in GitHub Desktop.
Flatten dictionary - C# code - pramp question - check links inside source code in the following
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FlattenDictionary | |
{ | |
class Program | |
{ | |
/* | |
* #Flatten a Dictionary | |
Given a dictionary, write a function to flatten it. Consider the following | |
input/output scenario for better understanding: | |
Input: | |
{ 'Key1': '1', 'Key2': { 'a' : '2', 'b' : '3', 'c' : { 'd' : '3', 'e' : '1' } } } | |
Output: | |
{ 'Key1': '1', 'Key2.a': '2', 'Key2.b' : '3', 'Key2.c.d' : '3', 'Key2.c.e' : '1' } | |
*/ | |
static void Main(string[] args) | |
{ | |
var test = new Program(); | |
var dict = new Dictionary<string, object>(); | |
dict.Add("Key1", "1"); | |
//var dict3 = new Dictionary<string, string>(); | |
// Dictionary<string, object> is not same as Dictionary<string, string> | |
var dict3 = new Dictionary<string, object>(); | |
dict3.Add("d", "3"); | |
dict3.Add("e", "1"); | |
var dict2 = new Dictionary<string, object>(); | |
dict2.Add("a", "2"); | |
dict2.Add("b", "3"); | |
dict2.Add("c", dict3); | |
dict.Add("Key2", dict2); | |
var result = test.FlattenDictionary(dict); | |
var dict4 = new Dictionary<string, object>(); | |
dict4.Add("a","4"); | |
result = test.FlattenDictionary(dict4); | |
} | |
/// <summary> | |
/// Code review on July 18, 2023 | |
/// http://juliachencoding.blogspot.com/search?q=Flatten+dictionary | |
/// https://gist.github.com/jianminchen/74ca7d779505a3f079c667960f773fc7 | |
/// | |
/// </summary> | |
/// <param name="dict"></param> | |
/// <returns></returns> | |
public Dictionary<string, string> FlattenDictionary(Dictionary<string, object> dict) | |
{ | |
if (dict == null || dict.Count == 0) | |
{ | |
return new Dictionary<string, string>(); | |
} | |
var flatten = new Dictionary<string, string>(); | |
foreach (var pair in dict) | |
{ | |
var key = pair.Key; | |
var valueObject = pair.Value; | |
if (valueObject.GetType() == typeof(Dictionary<string, object>)) | |
{ | |
var subDict = FlattenDictionary((Dictionary<string, object>)valueObject); | |
foreach (var childPair in subDict) | |
{ | |
var childKey = childPair.Key; | |
var childValue = childPair.Value; | |
var keyComposed = key; | |
var newKey = key.Length == 0 ? "" : keyComposed; | |
var newSubKey = childKey; | |
if (key.Length > 0) | |
{ | |
newSubKey = keyComposed + "." + childKey; // bug | |
} | |
newKey = childKey.Length == 0 ? keyComposed : newSubKey; | |
flatten.Add(newKey, childValue); | |
} | |
} | |
else | |
{ | |
flatten.Add(key, (string)valueObject); | |
} | |
} | |
return flatten; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment