recursion

How does the [].push.apply work?

霸气de小男生 提交于 2020-01-01 06:11:09
问题 can someone please explain me how does this line of code work. [].push.apply(perms, permutation(arr.slice(0), start + 1, last)); This function generates an array of all permutations of an input array; var permutation = function(arr, start, last){ var length = arr.length; if(!start){ start = 0; } if(!last){ last = length - 1; } if( last === start){ return [arr]; } var temp; var perms = []; for(var i = start; i < length; i++){ swapIndex(arr, i, start); console.log(arr); [].push.apply(perms,

Generating gray codes.

强颜欢笑 提交于 2020-01-01 05:46:10
问题 I tried generating gray codes in Python . This code works correctly. The issue is that I am initialising the base case ( n=1,[0,1] ) in the main function and passing it to gray_code function to compute the rest. I want to generate all the gray codes inside the function itself including the base case. How do I do that? def gray_code(g,n): k=len(g) if n<=0: return else: for i in range (k-1,-1,-1): char='1'+g[i] g.append(char) for i in range (k-1,-1,-1): g[i]='0'+g[i] gray_code(g,n-1) def main()

Recursive routine to obtain PropertyInfo

☆樱花仙子☆ 提交于 2020-01-01 05:33:07
问题 I'm attempting to create a recursive routine that will retrieve PropertyInfos for all members under a specified object (in .NET 3.5). Everything for immediate members is working, but it needs to parse nested classes as well (and their nested classes, etc). I do not understand how to handle the section that parses nested classes. How would you write this part of the code? public class ObjectWalkerEntity { public object Value { get; set; } public PropertyInfo PropertyInfo { get; set; } } public

Parsing an XML structure with an unknown amount of recursions using SAX

六眼飞鱼酱① 提交于 2020-01-01 05:27:09
问题 I have to parse a XML structure in JAVA using the SAX parser. The problem is that the structure is recursive with an unspecified count of recursions. This still is not such a big deal, the big deal is that I can't take advantage of the XML namespace functionality and the tags are the same on every recursion level. Here is an example of the structure. <?xml version="1.0" encoding="UTF-8"?> <RootTag> <!-- LOADS OF OTHER TAGS --> <Tags attribute="value"> <Tag attribute="value"> <SomeOtherTag><

What is the difference between “git submodule foreach git pull origin master” and “git pull origin master --recurse-submodules”

只谈情不闲聊 提交于 2020-01-01 05:21:09
问题 I have a dotfiles repository where all my vim plugins are stored as submodules so they are easy to update when they have changes. I thought these two commands did the same thing, but I noticed this must not be the case. I knew I had updates to pull down in several submodules so I ran git pull origin master --recurse-submodules from the root of the parent repository. It appeared to iterate over each submodule, but only fetch updates from their origin repositories. When I ran git submodule

Tail recursion with Groovy

╄→尐↘猪︶ㄣ 提交于 2020-01-01 05:20:08
问题 I coded 3 factorial algorithms: First, I expect to fail by Stack Overflow. No problem. Second, I try tail recusive call , convert previous algorithm from recursive to iterative. It doesn't work but I don't understand why . Third, I use trampoline() method and works fine as I expect. def factorial factorial = { BigInteger n -> if (n == 1) return 1 n * factorial(n - 1) } factorial(1000) // Stack Overflow factorial = { Integer n, BigInteger acc = 1 -> if (n == 1) return acc factorial(n - 1, n *

Karatsuba algorithm too much recursion

只愿长相守 提交于 2020-01-01 05:14:09
问题 I am trying to implement the Karatsuba multiplication algorithm in c++ but right now I am just trying to get it to work in python. Here is my code: def mult(x, y, b, m): if max(x, y) < b: return x * y bm = pow(b, m) x0 = x / bm x1 = x % bm y0 = y / bm y1 = y % bm z2 = mult(x1, y1, b, m) z0 = mult(x0, y0, b, m) z1 = mult(x1 + x0, y1 + y0, b, m) - z2 - z0 return mult(z2, bm ** 2, b, m) + mult(z1, bm, b, m) + z0 What I don't get is: how should z2 , z1 , and z0 be created? Is using the mult

PHP recursive function to delete all child nodes causes stackoverflow

非 Y 不嫁゛ 提交于 2020-01-01 04:56:06
问题 My MySQL looks like this: (the name of the table is category) 'id', 'content', 'parent' where: id = the id of the category content = some-text-we-dont-care-about parent = the id of the parent category this is what I'm trying right now: function remrecurs($id) { $qlist=mysql_query("SELECT * FROM category WHERE parent='$id'"); if (mysql_num_rows($qlist)>0) { while($curitem=mysql_fetch_array($qlist)) { remrecurs($curitem['parent']); } } mysql_query("DELETE FROM category WHERE id='$id'"); } Which

Is there any hard-wired limit on recursion depth in C

谁说胖子不能爱 提交于 2020-01-01 04:17:47
问题 The program under discussion attempts to compute sum-of-first-n-natural-numbers using recursion . I know this can be done using a simple formula n*(n+1)/2 but the idea here is to use recursion . The program is as follows: #include <stdio.h> unsigned long int add(unsigned long int n) { return (n == 0) ? 0 : n + add(n-1); } int main() { printf("result : %lu \n", add(1000000)); return 0; } The program worked well for n = 100,000 but when the value of n was increased to 1,000,000 it resulted in a

Recursion query?

流过昼夜 提交于 2020-01-01 03:47:05
问题 I'm new to mongodb. Let's say I have a "file system" hierarchy in my database: db.directories.save({ _id: "root", directories: ["src", "lib"], files: ["config.cfg"] }) db.directories.save({ _id: "src", directories: [], files: ["file1.js", "file2.js"] }) db.directories.save({ _id: "lib", directories: [], files: [] }) db.files.save({ _id: "config.cfg", size: 2310 }) db.files.save({ _id: "file1.js", size: 5039 }) db.files.save({ _id: "file2.js", size: 1299 }) How would I get the total size of a