C# flattening json structure

JObject jsonObject=JObject.Parse(theJsonString); IEnumerable<JToken> jTokens = jsonObject.Descendants().Where(p => !p.HasValues); Dictionary<string, string> results = jTokens.Aggregate(new Dictionary<string, string>(), (properties, jToken) => { properties.Add(jToken.Path, jToken.ToString()); return properties; }); I had the same requirement of flattening a nested json structure to a dictionary object. Found the solution here.

Breaking out of a recursion in java

No matter what you do, you are going to have to unwind the stack. This leaves two options: Magic return value (as described by one of the Toms) Throw an exception (as mentioned by thaggie) If the case where you want things to die is rare, this may be one of those situations where throwing … Read more

Can Python generate a random number that excludes a set of numbers, without using recursion?

Generate one random number and map it onto your desired ranges of numbers. If you wanted to generate an integer between 1-4 or 7-10, excluding 5 and 6, you might: Generate a random integer in the range 1-8 If the random number is greater than 4, add 2 to the result. The mapping becomes: Random … Read more

Why does the count of calls of a recursive method causing a StackOverflowError vary between program runs? [duplicate]

The observed variance is caused by background JIT compilation. This is how the process looks like: Method f() starts execution in interpreter. After a number of invocations (around 250) the method is scheduled for compilation. The compiler thread works in parallel to the application thread. Meanwhile the method continues execution in interpreter. As soon as … Read more

Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin

There’s nothing wrong with direct, “ad-hoc” solution. It can be clean enough too. Just notice that spiral is built from segments. And you can get next segment from current one rotating it by 90 degrees. And each two rotations, length of segment grows by 1. edit Illustration, those segments numbered … 11 10 7 7 … Read more

How to make a recursive lambda

Think about the difference between the auto version and the fully specified type version. The auto keyword infers its type from whatever it’s initialized with, but what you’re initializing it with needs to know what its type is (in this case, the lambda closure needs to know the types it’s capturing). Something of a chicken-and-egg … Read more