flatten

How to Serialize Binary Tree

不羁的心 提交于 2019-11-29 22:59:05
I went to an interview today where I was asked to serialize a binary tree. I implemented an array-based approach where the children of node i (numbering in level-order traversal) were at the 2*i index for the left child and 2*i + 1 for the right child. The interviewer seemed more or less pleased, but I'm wondering what serialize means exactly? Does it specifically pertain to flattening the tree for writing to disk, or would serializing a tree also include just turning the tree into a linked list, say. Also, how would we go about flattening the tree into a (doubly) linked list, and then

Is it possible to flatten MongoDB result query?

蹲街弑〆低调 提交于 2019-11-29 21:19:19
I have a deeply nested collection in my MongoDB collection. When I run the following query: db.countries.findOne({},{'data.country.neighbor.name':1,'_id':0}) I end up with this nested result here: {"data" : { "country" : [ { "neighbor" : [ { "name" : "Austria" }, { "name" : "Switzerland" } ] }, { "neighbor" : { "name" : "Malaysia" } }, { "neighbor" : [ { "name" : "Costa Rica" }, { "name" : "Colombia" } ] } ] }} Now, this is what I want: ['Austria', 'Switzerland', 'Malaysia', 'Costa Rica', 'Colombia'] or this: {'name':['Austria', 'Switzerland', 'Malaysia', 'Costa Rica', 'Colombia']} or anything

Scala: Remove none elements from map and flatten

不问归期 提交于 2019-11-29 17:01:05
问题 I have a map: Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3")) I want to remove all None elements and flatten the map. What is the easiest way to accomplish that? I only found this way: Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3")).filter(_._2.nonEmpty).map(item => (item._1 -> item._2.getOrElse(Nil))) The result is: Map(key1 -> value1, key3 -> value3) Do you know a better way? 回答1: My take using pattern matching is: Map("key1" -> Some("value1")

Racket/Scheme Flatten Explanations

这一生的挚爱 提交于 2019-11-29 14:38:17
Can someone help me to break down exactly the order of execution for the following versions of flatten? I'm using Racket. version 1, is from racket itself, while version two is a more common? implementation. (define (flatten1 list) (let loop ([l list] [acc null]) (printf "l = ~a acc = ~a\n" l acc) (cond [(null? l) acc] [(pair? l) (loop (car l) (loop (cdr l) acc))] [else (cons l acc)]))) (define (flatten2 l) (printf "l = ~a\n" l) (cond [(null? l) null] [(atom? l) (list l)] [else (append (flatten2 (car l)) (flatten2 (cdr l)))])) Now, running the first example with '(1 2 3) produces: l = (1 2 3)

Depth-first flattened collection of an object hierarchy using LINQ

蓝咒 提交于 2019-11-29 14:31:46
I have an object hierarchy (MasterNode -> ChildNodes) where master and child nodes are of the same type, and there are only two levels (top level and children) like this ('A' is parent of D,E and F, 'B' is parent of G, etc) A--+ | D | E | F | B--+ | G | C--+ H I Suppose I have a MasterNodes as an IEnumerable of the parent objects (A,B,C) and given a parent object X I can get an IEnumerable of its children by X.children I know that I can enumerate all of the leaf (child nodes) with the SelectMany method or by using from parent in Masternodes from child in parent.children select child This will

How to transpose an array more Swiftly?

只愿长相守 提交于 2019-11-29 12:03:22
I asked a similar question a while ago. It was asking how can I turn an array like this: [[1,2,3],[4,5,6],[7,8,9]] to this: [1,2,3,4,5,6,7,8,9] But now I want to turn that same array to this: [1,4,7,2,5,8,3,6,9] Assume all the subarrays have the same length. If you haven't noticed already, the first three items in the result is the first item of the three subarrays. The fourth, fifth and sixth items in the result is the second item of each subarray. If you still don't understand, maybe this will help: Original array: [ [1,2,3], [4,5,6], [7,8,9] ] Result: [ 1,4,7, 2,5,8, 3,6,9 ] At the moment,

How to flatten a list of nested tuples in Python?

梦想与她 提交于 2019-11-29 11:21:55
I have a list of tuples that looks like this: [('a', 'b'), ('c', 'd'), (('e', 'f'), ('h', 'i'))] I want to turn it into this: [('a', 'b'), ('c', 'd'), ('e', 'f'), ('h', 'i')] What is the most Pythonic way to do this? one-line, using list comprehension: l = [('a', 'b'), ('c', 'd'), (('e', 'f'), ('h', 'i'))] result = [z for y in (x if isinstance(x[0],tuple) else [x] for x in l) for z in y] print(result) yields: [('a', 'b'), ('c', 'd'), ('e', 'f'), ('h', 'i')] this is artificially creating a list if the element is not a tuple of tuples, then flattening all does the job. To avoid creating a single

Perl: What is the easiest way to flatten a multidimensional array?

浪子不回头ぞ 提交于 2019-11-29 03:29:07
What's the easiest way to flatten a multidimensional array ? Using List::Flatten seems like the easiest: use List::Flatten; my @foo = (1, 2, [3, 4, 5], 6, [7, 8], 9); my @bar = flat @foo; # @bar contains 9 elements, same as (1 .. 9) Actually, that module exports a single simple function flat , so you might as well copy the source code : sub flat(@) { return map { ref eq 'ARRAY' ? @$_ : $_ } @_; } You could also make it recursive to support more than one level of flattening: sub flat { # no prototype for this one to avoid warnings return map { ref eq 'ARRAY' ? flat(@$_) : $_ } @_; } One level

How to flatten a List of different types in Scala?

為{幸葍}努か 提交于 2019-11-29 02:54:23
问题 I have 4 elements: List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I obtained a nested list that I can't apply flatten to because it says: no implicit argument matching parameter type. How can I solve this? should I try another way or is there any way to make the flatten work? I'm kinda new to scala so it may be a dumb question :D Thanks in advance! clau 回答1: For One

How to flatten a pandas dataframe with some columns as json?

自作多情 提交于 2019-11-28 18:46:56
I have a dataframe df that loads data from a database. Most of the columns are json strings while some are even list of jsons. For example: id name columnA columnB 1 John {"dist": "600", "time": "0:12.10"} [{"pos": "1st", "value": "500"},{"pos": "2nd", "value": "300"},{"pos": "3rd", "value": "200"}, {"pos": "total", "value": "1000"}] 2 Mike {"dist": "600"} [{"pos": "1st", "value": "500"},{"pos": "2nd", "value": "300"},{"pos": "total", "value": "800"}] ... As you can see, not all the rows have the same number of elements in the json strings for a column. What I need to do is keep the normal