New answers tagged

2

public Node DeepCopy() { var other = (Node)MemberwiseClone(); other.Children = new List<Node>(collection: Children); other.Parent = Parent?.DeepCopy(); other.Value = new Node(value: Value); return other; } You should be careful when using this, because it actually clones the entire tree (via Parent and Children). Besides that, I ...


3

Reading Material .. any reading material about searching algorithms on trees These are the most common tree walkers: Breadth-First Search Depth-First Search Review There is a bug with IsRoot. Also, why not provide a property Root { get; }? if the parent is the root of the tree, then the parent is set to null public bool IsRoot { get { return ...


1

I like the following approach because it generalizes to other sequences such as lists and tuples. from functools import reduce from operator import getitem def get_nested_item(data, keys): return reduce(getitem, keys, data) Usage: get_nested_item(data={"a":{"b":{"c":1}}}, keys=["a","b","c"]) # => 1 get_nested_item(data=[[[1,2,3],[10,20,30]]], keys=...


0

Optimization starts from a sound logical process Let's think about the problem in particular. Given a positive integral number n, return a strictly increasing sequence (list/array/string depending on the language) of numbers, so that the sum of the squares is equal to n². If there are multiple solutions (and there will be), return the result with ...


5

The root of the problem lies in this line: dynamic expando = /* ParseConfigToJsonWithoutNulls -> ParseJsonToExpandoObject */ where you decided to parse *.json files into an ExpandoObject and not directly into a Dictionary or some other strong type. I bet you are using Json.Net for the job and there are countless possibilities to deserialize JSON in ...


2

As you have already noticed, this can be viewed as a case of mutual recursion. To demonstrate this I give the following mutually recursive definitions: (defn sort-parts [work] (lazy-seq (sp-loop work))) (defn sp-loop [[part & parts]] (if-let [[pivot & xs] (seq part)] (sp-loop ;or recur for tco (let [smaller? #(< % pivot)] (...


7

Your method is meant to handle ExpandoObjects explicitly, so it would be better named ConvertExpandoObjectToDictonary, and would probably be less confusing if it actually took an ExpandoObject as the parameter: Dictionary<string, object> ConvertExpandoObjectToDictonary(ExpandoObject expandoObject); This is what the public API should look like; you ...


2

A few minor stylistic bits that may increase its utility: since you take in an IDictionary, return an IDictionary. make it static as it accesses no instance data or methods. since it's now static, make it an extension method. validate the parameter passed in. Also: since it's a single return statement, make it an expression body. Further: yeah, the .Any(...


4

Inconsistent definition of ExpandoObject. It's IDictionary<string, object> here: if (p.Value is IDictionary<string, object> dict) { return ConvertDynamicToDictonary(dict); // <- possibly already visited? } and ExpandoObject here: if (list.Any(o => o is ExpandoObject)) { // .. } I would opt to use IDictionary<string, object>...


2

We are far from from beta-reduction here. The "reduction" can only be understood using explicit instruction flow semantics. No. You have chosen to interpret loop and recur in this way, but that is not how Clojure understands them. recur is simply a flag for a tail-recursive call, functional as you like. loop is a gloss on defining and applying a ...


4

Note that some messages from exceptions expand to more lines (e.g. ArgumentNullException), so consider to replace new line characters while formatting the message so each exception message is written neatly in one line: exception.Message.Replace(Environment.NewLine, " ") My version would look something like: public static string GetMessageTree(this ...


8

Readability first I've tried to condense everything to one line, but am unsure if this is the most efficient way of approaching this problem. Your primary goal when writing code is readability and maintainability and not squeezing everything into single lines. Can you read this? return exception != null ? string.Format("{0}: {1}\n{2}", msgCount, ...


4

Review If the purpose is to output each of the descendant inner exceptions, you are missing all except the first inner exception in case of an AggregateException. And this is a common exception. Don't use \n as new line unless you specifically need to comply to this style of new line. Prefer Environment.NewLine. I am not convinced of the exception != null ->...


2

There is a slight difference in the order of evaluation, in that the book's solution recurses before calling consp, whereas your solution recurses after calling consp. Either way, the recursion happens before concatenation, because the innermost expressions in (append (apply #'list-of elt) (uncompress (cdr lst))) and (cons elt (uncompress (cdr lst))))))) ...


Top 50 recent answers are included