recursion

Can we use the output of one recursive query into another recursive query?

元气小坏坏 提交于 2019-12-23 05:29:05
问题 I wanted to find the topological sort of a DAG. create table topo( v1 int, v2 int ); Insert into topo values (1,3),(2,5),(3,4),(4,5),(4,6),(5,7),(6,5),(7,null) WITH RECURSIVE path(S,d) AS( select t1.v1, 0 from topo t1 left outer join topo as t2 on t1.v1=t2.v2 where t2.v2 IS null UNION ALL select distinct t1.v2, path.d + 1 from path inner join topo as t1 on t1.v1=path.S ) select S from path group by S order by MAX(d); This code gives the output of the topological order of a graph. Now i want

Java Array Recursion

假装没事ソ 提交于 2019-12-23 05:26:11
问题 The purpose of this assignment is to learn recursive methods. For this particular problem, I need to print the values of list , one per line. The skeleton of the method I need to complete cannot be changed and is as follows: public void list (String[] list) { } The instructions say that helper methods may make this easier to write. Here is what I have for my helper method: public void print(String[] list, int index) { if (index < list.length) { System.out.println(list[index]); print(list,

How to convert this non-tail-recursion function to a loop or a tail-recursion version?

跟風遠走 提交于 2019-12-23 05:18:06
问题 I've been curious for this for long. It is really very easy and elegant for human to create a non-tail-recursive function to get something complicated, but the speed is very slow and easily hit the limit of Python recursion: def moves_three(n, ini=0, med=1, des=2): '''give a int -> return a list ''' if n == 1: return ((ini,des),) return moves_three(n-1, ini=ini, med=des, des=med) + \ ((ini, des),) + \ moves_three(n-1, ini=med, med=ini, des=des) if __name__ == '__main__': moves_three(100) #

async + recursive + database-update operation with nodejs

爷,独闯天下 提交于 2019-12-23 05:14:39
问题 Here is the algorithm which returns the position(in form of a callback) at which an item can be inserted and also update the position of the previous items. Put the item into infinite length array (assumed), 0<=index<INFINITY, and for empty array largest_index_occupied = 0. ALSO largest_index_occupied is dynamically retrieved from database.(assumed). No two item can share same index. No item must be lost during any operation. indexes are in increasing order. 1. if index not provided OR index

All possible combinations of numbers of length 5 in a 4X4 matrix

爷,独闯天下 提交于 2019-12-23 05:14:15
问题 i have a matrix 1 9 2 3 5 0 0 6 8 4 4 8 2 3 7 8 I need to find all possible combinations of numbers of length 5. constraints: Starting form a any position in the matrix you can only move to your next immediate neighbor i.e if u start form say (0,0) your neighbor must be (0,1),(1,1),(1,0) and if you pick a position then form that position you can only move to its immediate neighbor and so on. The length of the number must be 5 digit's i.e for example if i start from (0,0) with value 1 i can

How to create as many nested loops as you want?

一曲冷凌霜 提交于 2019-12-23 05:13:50
问题 I am trying to create a program that can take many numbers as I want in C++ language. Then it find what operators can make the equation true and show all correct possible operation. Example: If I put 3 5 15 Then it output 3x5 = 15 If I put 1 2 3 4 4 Then it outputs 1+2-3+4 =4 The following code is my written program: The problem about it is that when I want to reduce the number of input or increase the number of input I need to add/reduce nested loops EVERYTIME. I want to know what is a more

Finding all possible words from inputted character arrays (permutations)

天大地大妈咪最大 提交于 2019-12-23 05:11:51
问题 After reading through several posts I am still stumped with permutations and recursive functions. I am trying to create all possible 3 letter permutations of characters in an unequal length 2D array where the first letter is from the set {‘l’, ‘m’, ‘n’}, the second letter is from the set {‘q’, ‘r’} and the third letter is from the set {‘a’, ‘e’, ‘i’, ‘o’}. My code goes through the correct permutation pattern but does not print out the correct output. For example, if the first 8 permutations

Recursion: Understanding (subset-sum) inclusion/exclusion pattern

浪子不回头ぞ 提交于 2019-12-23 04:59:12
问题 I need to understand how this recursion work, I understand simple recursion examples but more advanced ones is hard. Even thought there are just two lines of code I got problem with... the return statement itself. I just draw a blank on how this works, especially the and/or operator. Any insight is very welcome. bool subsetSumExists(Set<int> & set, int target) { if (set.isEmpty()) { return target == 0; } else { int element = set.first(); Set<int> rest = set - element; return subsetSumExists

How to get data from the local graph?

大憨熊 提交于 2019-12-23 04:57:19
问题 I've got the component which displays the hierarchical data. It's a recursive data structure - a hierarchical classifier. Each category in the classifier consists of child categories and so on. Here's the playground for it: Relay playground I've got a component showing this classifier in a third party html form. It is used to pick a category. At some point in time a user will pick a category from this recursive classifier and I will have to get the full path of data that led to the current

print fibo big numbers in c++ or c language

隐身守侯 提交于 2019-12-23 04:52:39
问题 I write this code for show fibonacci series using recursion.But It not show correctly for n>43 (ex: for n=100 show:-980107325). #include<stdio.h> #include<conio.h> void fibonacciSeries(int); void fibonacciSeries(int n) { static long d = 0, e = 1; long c; if (n>1) { c = d + e; d = e; e = c; printf("%d \n", c); fibonacciSeries(n - 1); } } int main() { long a, n; long long i = 0, j = 1, f; printf("How many number you want to print in the fibonnaci series :\n"); scanf("%d", &n); printf("