recursion

Traversing an object getting the key and all parents keys

China☆狼群 提交于 2020-01-01 19:30:07
问题 traverse tree(json), fulfill getKeys(data, str) function using JS. get the key and all parents keys. const data = { key1: 'str1', key2: { key3: 'str3', key4: 'str4', key5: { key6: 'str6', key7: 'str7', key8: 'str8', }, } } for example: getKeys(data, 'str1'); return: 'key1' getKeys(data, 'str3'); return: 'key2, key3' getKeys(data, 'str6'); return: 'key2, key5, key6' I think it can be done be recursion, but how? this is my solution, but failed let s = []; function getKeys(data, str, key='') {

Python code to find Eulerian Tour does not work in one case. Why? [duplicate]

被刻印的时光 ゝ 提交于 2020-01-01 19:10:58
问题 This question already has answers here : Finding a Eulerian Tour (10 answers) Closed 6 years ago . I am trying to solve a problem on Udacity described as follows: # Find Eulerian Tour # # Write a function that takes in a graph # represented as a list of tuples # and return a list of nodes that # you would follow on an Eulerian Tour # # For example, if the input graph was # [(1, 2), (2, 3), (3, 1)] # A possible Eulerian tour would be [1, 2, 3, 1] The code I wrote is below. It's not super

OutOfMemoryError: Java Heap Space. How can I fix this error that happens in a recursive method?

牧云@^-^@ 提交于 2020-01-01 19:02:47
问题 I have a Java application that parses pdf files in a directory and its subdirectories and creates a database using the information found in the files. Everything was fine when I was using the program on around 900 files or so (which create a SQLite database with multiple tables, some of wihch contain 150k rows). Now I'm trying to run my program on a larger set of data (around 2000 files) and at some point I get "OutOfMemoryError: Java Heap space". I changed the following line in my jdev.conf

WITH Common Table Expression vs. CREATE VIEW performance

你离开我真会死。 提交于 2020-01-01 15:52:09
问题 I have several queries that use a WITH clause, or Common Table Expression, with a UNION ALL statement to recur through a table with a tree like structure in SQL server as described here. Would I see a difference in performance if I were to CREATE that same VIEW instead of including it with the WITH clause and having it generated every time I run the query? Would it generally be considered good practice to actually CREATE the view since it is used in several queries? 回答1: What you're looking

Need help on making the recursive parser using pyparsing

拥有回忆 提交于 2020-01-01 14:26:56
问题 I am trying the python pyparsing for parsing. I got stuck up while making the recursive parser. Let me explain the problem I want to make the Cartesian product of the elements. The syntax is cross({elements },{element}) I put in more specific way cross({a},{c1}) or cross({a,b},{c1}) or cross({a,b,c,d},{c1}) or So the general form is first group will have n elements (a,b,c,d). The second group will have one element that so the final output will be Cartesian Product. The syntax is to be made

“recursive” self join in data.table

荒凉一梦 提交于 2020-01-01 14:10:10
问题 I have a component list made of 3 columns: product, component and quantity of component used: a <- structure(list(prodName = c("prod1", "prod1", "prod2", "prod3", "prod3", "int1", "int1", "int2", "int2"), component = c("a", "int1", "b", "b", "int2", "a", "b", "int1", "d"), qty = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L)), row.names = c(NA, -9L), class = c("data.table", "data.frame")) prodName component qty 1 prod1 a 1 2 prod1 int1 2 3 prod2 b 3 4 prod3 b 4 5 prod3 int2 5 6 int1 a 6 7 int1 b 7 8

“recursive” self join in data.table

扶醉桌前 提交于 2020-01-01 14:09:27
问题 I have a component list made of 3 columns: product, component and quantity of component used: a <- structure(list(prodName = c("prod1", "prod1", "prod2", "prod3", "prod3", "int1", "int1", "int2", "int2"), component = c("a", "int1", "b", "b", "int2", "a", "b", "int1", "d"), qty = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L)), row.names = c(NA, -9L), class = c("data.table", "data.frame")) prodName component qty 1 prod1 a 1 2 prod1 int1 2 3 prod2 b 3 4 prod3 b 4 5 prod3 int2 5 6 int1 a 6 7 int1 b 7 8

How do I get all children from a given parent node?

筅森魡賤 提交于 2020-01-01 12:07:10
问题 I have a list of parent/child IDs and would like to get all child IDs for a given parent ID. There are no null parents (the top level IDs don't appear as child IDs). Currently the parent/child IDs are recorded as a KeyValuePair in a list, however this could be easily changed to another data structure if that would be better: List<KeyValuePair<int, int>> groups = new List<KeyValuePair<int, int>>(); groups.Add(new KeyValuePair<int,int>(parentID, childID)); For example, here are sample parent

Prove that n! = O(n^n)

耗尽温柔 提交于 2020-01-01 12:01:22
问题 How can I prove that n! = O(n^n)? 回答1: I assume that you want to prove that the function n! is an element of the set O(n^n) . This can be proven quite easily: Definition: A function f(n) is element of the set O(g(n)) if there exists a c>0 such that there exists a m such that for all k>m we have that f(k)<=c*g(k) . So, we have to compare n! against n^n . Let's write them one under another: n! = n * (n-1) * (n-2) * (n-3) * ... * 3 * 2 * 1 n^n = n * n * n * n * ... * n * n * n As you can see,

Recursive functions and lists appending/extending

最后都变了- 提交于 2020-01-01 10:14:00
问题 This is a very simple code in place of a bigger problem, but I'm hoping I can tackle it in chunks. I'll start with my first problem. def testrecurse(z,target): x=[] if z<target: z*=2 x.append(z) x.extend(testrecurse(z,target)) return x This is a test function to help my brain out with recursion. It takes a number, then shows all the multiplications of two until it hits the target number. so if I enter: testrecurse(1,1000) I receive: [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] which is great!