recursion

Crockford's hanoi function (from “The Good Parts”) [duplicate]

断了今生、忘了曾经 提交于 2019-12-30 09:32:12
问题 This question already has answers here : How does recursive algorithm work for Towers of Hanoi? (2 answers) Closed 5 years ago . At the moment I'm reading Douglas Crockford's book, and the towers of hanoi function is a bit over my head. Even with logging stuff to the console I wasn't able to really understand what's going on. Here's the function with my additions: var hanoi = function (disc, src, aux, dst) { console.log(disc); console.log(src, dst); if (disc > 0) { hanoi(disc - 1, src, dst,

How exactly does this recursive regex work?

℡╲_俬逩灬. 提交于 2019-12-30 09:29:23
问题 This is a followup to this question. Have a look at this pattern: (o(?1)?o) It matches any sequence of o with a length of 2 n , with n ≥ 1. It works, see regex101.com (word boundaries added for better demonstration). The question is: Why? In the following, the description of a string (match or not) will simply be a bolded number or a bolded term that describes the length, like 2 n . Broken down (with added whitespaces): ( o (?1)? o ) ( ) # Capture group 1 o o # Matches an o each at the start

Why are stackoverflow errors chaotic?

最后都变了- 提交于 2019-12-30 09:07:08
问题 This simple C program rarely terminates at the same call depth: #include <stdio.h> #include <stdlib.h> void recursive(unsigned int rec); int main(void) { recursive(1); return 0; } void recursive(unsigned int rec) { printf("%u\n", rec); recursive(rec + 1); } What could be the reasons behind this chaotic behavior? I am using fedora (16GiB ram, stack size of 8192), and compiled using cc without any options. EDIT I am aware that this program will throw a stackoverflow I know that enabling some

Recursive VBA Precedents

我的梦境 提交于 2019-12-30 08:27:21
问题 I have an excel spreadsheet with quite a few formulas and data that I keep track of. I have a small macro that will find the Precedents for a selected cell however id like to make the macro recursive so that I can find all of the precedents. Eg Setting focus to a cell and running this function will highlight the cell and then highlight the precedents of the cell, then highlight the precedents of those cells, then highlight the precedents... The problem I am having at the moment is I am not

What does “*RECURSION*” mean when printing $GLOBALS?

一笑奈何 提交于 2019-12-30 08:23:10
问题 When I print $GLOBALS using this code: <?php print_r($GLOBALS); ?> I get this output: Array ( [_GET] => Array ( ) [_POST] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) [GLOBALS] => Array *RECURSION* ) What does " *RECURSION* " mean in this case, and why are $_SERVER , $_REQUEST , etc. not printed as well? 回答1: See this part of PHP Manual: Keep in mind that $GLOBALS is, itself, a global variable. So code like this won't work: <?php print '$GLOBALS = ' . var_export($GLOBALS, true) .

longest palindromic substring recursive solution

ぐ巨炮叔叔 提交于 2019-12-30 08:00:09
问题 I am aware of solutions that uses the bottom up dynamic programing approach to solve this problem in O(n^2). I am specifically looking for a top down dp approach. Is it possible to achieve longest palindromic substring using a recursive solution? Here is what I have tried but it fails for certain cases, but I feel I am almost on the right track. #include <iostream> #include <string> using namespace std; string S; int dp[55][55]; int solve(int x,int y,int val) { if(x>y)return val; int &ret =

A recursive function to sort through parent and child nodes in PHP using a foreach loop on an array

五迷三道 提交于 2019-12-30 07:43:06
问题 I have a data set stored in an array that references itself with parent-child ids: id , parent_id , title etc. The top tier has a parent_id of 0 , and there can be countless parent-child relationships. So I'm sorting through this array with a foreach loop within a recursive function to check each array element against its parent element, and I think I've been staring at this method too long. I do end up with the elements in the correct order, but I can't seem to get my lists nested correctly,

A recursive function to sort through parent and child nodes in PHP using a foreach loop on an array

青春壹個敷衍的年華 提交于 2019-12-30 07:43:05
问题 I have a data set stored in an array that references itself with parent-child ids: id , parent_id , title etc. The top tier has a parent_id of 0 , and there can be countless parent-child relationships. So I'm sorting through this array with a foreach loop within a recursive function to check each array element against its parent element, and I think I've been staring at this method too long. I do end up with the elements in the correct order, but I can't seem to get my lists nested correctly,

Create and traverse a binary tree recursively in C

三世轮回 提交于 2019-12-30 07:21:25
问题 I want to create a binary tree and traverse it by preorder traversal, and I use recursive method. These code can be compiled but can not run correctly, and I found it maybe can not finish the CreateBitree() function, but I don't know where the problem is. #include <stdio.h> #include <malloc.h> typedef struct BiNode{ int data; struct BiNode *lchild; struct BiNode *rchild; //left and right child pointer }BiNode; int CreateBiTree(BiNode *T); int TraverseBiTree(BiNode *T); int main() { BiNode *t;

How to recursively delete items from table?

半腔热情 提交于 2019-12-30 07:13:14
问题 I've a MySQL table "folders": CREATE TABLE IF NOT EXISTS `folders` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `folder_key` varchar(40) NOT NULL, `parent_key` varchar(40) NOT NULL, `name` varchar(16) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; I don't use integer IDs, only keys (alphanumeric hashes, which I've replaced with words to make things more clear). So, folder_key & parent_key are SHA-1 hashes (in my real application). INSERT INTO `folders` (`id`, `folder_key`, `parent_key`,