recursion

JavaScript: Find all parents for element in tree recursive

久未见 提交于 2020-01-02 04:29:10
问题 I have a tree somthing like this var datas = { 'tree': [ { 'name': 'name1', 'tree': [ {'name': 'name2'}, {'name': 'name3'}, { 'name': 'name4', 'tree': [ {'name': 'name5'}, {'name': 'name6'} ] }, {'name': 'name7'} ] }, { 'name': 'name8', 'tree': [ {'name': 'name9'} ] } ] } I want to find all the parents of the specifig id for example in the tree demo, if I look for 'name5' I want to find "name1,name4,name5" I wrote this code but the results wrong and I got the ids of other elements and not the

I need an array_keys_recursive()

牧云@^-^@ 提交于 2020-01-02 04:10:27
问题 $temp = array(); function show_keys($ar) { foreach ($ar as $k => $v ) { $temp[] = $k; if (is_array($ar[$k])) { show_keys ($ar[$k]); } } return $temp; } I tried using that function but it still only returns the first key. 回答1: Using SPL, looping over the keys is quite easy (store them in another array if you wish): <?php $arr = array_fill(0,8,range(0,3)); var_dump($arr); foreach( new RecursiveIteratorIterator( new RecursiveArrayIterator($arr), RecursiveIteratorIterator::SELF_FIRST) as $key =>

How to improve the performance of the recursive method?

好久不见. 提交于 2020-01-02 03:16:48
问题 I'm learning data structures and algorithms, and here is a question that I'm stuck with. I have to improve the performance of the recursive call by storing the value into memory. But the problem is that the non-improved version seems faster than this. Can someone help me out? Syracuse numbers are a sequence of positive integers defined by the following rules: syra(1) ≡ 1 syra( n ) ≡ n + syra( n /2), if n mod 2 == 0 syra( n ) ≡ n + syra(( n *3)+1), otherwise import java.util.HashMap; import

C#: Code to fit LOTS of files onto a DVD as efficiently as possible

梦想的初衷 提交于 2020-01-02 03:11:51
问题 I need to write an application that will take a list of files (some large, some small) and fit them onto DVDs (or CDs, or whatever) as efficiently as possible. The whole point of this application is to use up as much of the 1st disc before moving onto the 2nd disc, filling the 2nd disc up as much as possible before moving onto the 3rd disc, etc. (Note: The application doesn't have to do the actual burning to the DVD, it just has to figure out the best possible fit). I initially thought I had

Python: Maximum recursion depth exceeded when printing custom exception

亡梦爱人 提交于 2020-01-02 01:46:29
问题 The following code throws RuntimeError: maximum recursion depth exceeded while getting the str of an object . I can resolve the infinite recursion in two different ways, but I don't understand why each fix works and thus don't know which to use, or if either are correct. class FileError( Exception ): def __init__( self, filename=None, *a, **k ): #Fix 1: remove super super( FileError, self ).__init__( self, *a, **k ) self.filename = filename def __repr__( self ): return "<{0} ({1})>".format(

How to `chmod -R +w` with Ant, files and folders?

牧云@^-^@ 提交于 2020-01-02 00:38:08
问题 I'd like to do the equivalent of a chmod -R +w foo/ in an Ant build script. So far I'm using this: <chmod perm="g+w"> <dirset dir="${basedir}/foo"> </dirset> <fileset dir="${basedir}/foo"> </fileset> </chmod> Is there a neater way to write that to include files and folders recursively ? 回答1: The following does work: <chmod file="${basedir}/foo/**" perm="g+w" type="both"/> Credits shared with the OP. See also Chmod Task 回答2: To chmod one can use exec: <exec executable="chmod" dir="${basedir}

Unexpected output in C

橙三吉。 提交于 2020-01-01 20:50:16
问题 Following is my code: #include<stdio.h> int i =5; int main(i) { if(i<10) printf(" %d\n",printf("%d",i+main(++i))); return 0; } Output (in both both Ideone.com and Codeblocks) 10 2 9 1 8 1 7 1 6 1 5 1 4 1 3 1 2 1 Can someone explain the reason behind this output? I expected 91, 81, ...., 51. Also, is it true that recursive main() results in unexpected outputs? PS: This is a program that I had found in an online forum. 回答1: Firstly, "implict int" rule has been outlawed a long time ago. int main

XSLT - build multiple (n) html tables of equal size (in this case, 3x3)

邮差的信 提交于 2020-01-01 19:57:50
问题 Reference Question 1 Reference Question 2 Ok, both of the preceding links go to discussions of how to build cells and rows within tables. I was hoping to find an expanded example that showed how to build multiple TABLES of N-cells each (in this case 9 cells each - 3x3). I have been trying for a while to use the logic from the two examples and about 500 other places on the web but haven't been able to crack the nut. Can someone, perhaps the contributor to the above links, shed some light on

Understanding a recursive IA32 assembly call

假装没事ソ 提交于 2020-01-01 19:47:56
问题 I'm trying to do some practice to get more comfortable with IA32 assembly, and am struggling a bit on translating this recursive snippet of assembly code to understandable C code. They gave us a hint that all functions in the code give only one argument, but my understanding of the IA32 stack is still a bit poor. .globl bar .type bar, @function bar: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax addl $10, %eax popl %ebp ret .globl foo .type foo, @function foo: pushl %ebp movl %esp, %ebp subl

Maximum number of elements in the path of a matrix

左心房为你撑大大i 提交于 2020-01-01 19:46:09
问题 I tried to solve a problem of map (matrix 4x4) using python. I want to find Maximum number of elements in the path of a map provided the next node must be lesser than the previous node with all possible combinations of elements in the matrix. 4 8 7 3 2 5 9 3 6 3 2 5 4 4 1 6 The movement is like from an element can move to east-west-north-south For example from m[0][1] can move to m[0][2] and m[1][1] 4-> 8 or 2 Here is the sample code but i have no idea to how to recursively check every