recursion

data.table | faster row-wise recursive update within group

非 Y 不嫁゛ 提交于 2020-07-04 05:40:05
问题 I have to do the following recursive row-by-row operation to obtain z : myfun = function (xb, a, b) { z = NULL for (t in 1:length(xb)) { if (t >= 2) { a[t] = b[t-1] + xb[t] } z[t] = rnorm(1, mean = a[t]) b[t] = a[t] + z[t] } return(z) } set.seed(1) n_smpl = 1e6 ni = 5 id = rep(1:n_smpl, each = ni) smpl = data.table(id) smpl[, time := 1:.N, by = id] a_init = 1; b_init = 1 smpl[, ':=' (a = a_init, b = b_init)] smpl[, xb := (1:.N)*id, by = id] smpl[, z := myfun(xb, a, b), by = id] I would like

Tower of Hanoi with forbidden move from source to destination (C)

烂漫一生 提交于 2020-06-29 03:57:28
问题 I am trying to write a recursive C function which would solve the tower of Hanoi , but with an extra restriction , that moving a disc from A (the source tower) to C (the destination tower) is forbidden , and vice versa. For instance , moving a single disc from A to C or C to A , would require using the auxiliary tower (B). I found a normal recursive Hanoi tower code from geeksforgeeks and checked a CS page which discussed the same problem , but I cant understand the mathematical algorithm

Should I avoid recursion everywhere it's possible?

痴心易碎 提交于 2020-06-29 03:39:14
问题 This is not a case where I can use both without problems, because I'm already convinced that loops are much easier to understand and I try to always use them. But then I stumble upon this (C++ function for binary search tree): Node* Insert(Node* &rootptr,Node* data) { if (rootptr == nullptr) { rootptr = data; } else if (data->number <= rootptr->number) { rootptr->leftptr = Insert(rootptr->leftptr,data); } else { rootptr->rightptr = Insert(rootptr->rightptr,data); } return rootptr; } And my

Tracing Recursion in C

限于喜欢 提交于 2020-06-28 07:01:18
问题 I have a basic understanding of recursive functions and tracing, but something is going haywire when I try to trace the following program: #include <stdio.h> #include <stdlib.h> int f1(int *a, int c); int main(void) { int a=2, b=3, c=4, d=5; a = f1(&c, f1(&b,d)); printf("a= %d b= %d c= %d d= %d\n",a,b,c,d); system("pause"); return 0; } int f1(int *a, int c) { *a = c - 2; c = c*2 - (*a); printf("a= %d c= %d\n", *a, c); return c - *a; } When I trace, I get two calls to f, f(4,4) and f(3,5). I

A better way to trim all elements in an object recursively?

江枫思渺然 提交于 2020-06-27 18:48:11
问题 If I have an object like, const obj = { field: { subfield: { innerObj: { a: ' asdasd asdas . ' }, innerArr: [' s ', ' ssad . '], innerArrObj: [ { b: ' adsad ' } ], } } } I've come up with something like this, const trimFields = (data) => _.mapValues(data, (element, k) => _.isArray(element) ? element.map((value) => _.isObject(value) ? trimFields(value) : trimText(value) ) : _.isObject(element) ? trimFields(element) : trimText(element) ); But I'm wondering if there is a better / more efficient

Drawing Bowling Pins (pyramid) with Recursion in Ada

≡放荡痞女 提交于 2020-06-27 18:37:06
问题 I know this is pushing the good will of the community by presenting my least elaborate work expecting someone to come and save me but I simply have no choice with nothing to lose. I've gone through packets, files, types, flags and boxes the last few weeks but I haven't covered much of recursion. Especially not drawing with recursion. My exam is in roughly one week and I hope this is ample time to repeat and learn simple recursion tricks like that of drawing bowling pins or other patterns: I I

How to handle recursion in member functions?

天涯浪子 提交于 2020-06-27 18:35:27
问题 E.g., I have an empty function to clear a linked list: void empty(Node* head) { if (head->next) { empty(head->next); } delete head; head = nullptr; } But then I created a class for the linked list, so now I don't need to pass the head argument: void empty() { if (head->next) { empty(head->next); } delete head; head = nullptr; } But empty(head->next) line is obviously wrong as empty doesn't take any arguments. The idea comes to my mind to create a function inside a function (with lambda),

Recursive challenge in JS combining all possible array keys in true | false versions, I attach the input and output

自闭症网瘾萝莉.ら 提交于 2020-06-27 16:39:10
问题 I have found many solutions about all posible combinations between array values but I need something different, I hope you could support me. Basically is to create all posible objects that combine array keys with true|false values, something like this: Input: (Should return an array of 32 objects, 2exp5, two posible values in 5 keys) let properties = ['arm','lens','season','food','size']; Output: let combinations = [ {"arm": "false","lens": "false","season": "false","food": "false","size":

How do I find a prime number using recursion in Python

寵の児 提交于 2020-06-27 16:30:10
问题 I have to find out whether number(N) is a prime or not using recursion, no loops are allowed. I've tried converting the usual code that uses a for loop to a recursive one, but it's not behaving the same. This function is included in another function, which is part of another function. only parameters a and N should be used and passed Here is my function. a=2 def is_prime(a,N): prime = True if N <=1: return else: if a >= N: return else: if N == 2: prime = True print(N) return elif (N % a) == 0

MEF recursive plugin search

旧时模样 提交于 2020-06-27 06:48:11
问题 Let's say that I have a few applications in a folder (each application has subfolders where plugins can be located): Clients Application A ... Application B ... Application C ... ... Some files in these applications have an Export-attribute applied, others don't. Now, I want to be able to load these plugins in some of these applications. Is there a proper way to let MEF search recursively in every subfolder of a specified folder? 回答1: No, you will need to recurse through the directories