recursion

recursively sum the integers in an array

微笑、不失礼 提交于 2019-12-29 05:34:10
问题 I have a program that I'm trying to make for class that returns the sum of all the integers in an array using recursion. Here is my program thus far: public class SumOfArray { private int[] a; private int n; private int result; public int sumOfArray(int[] a) { this.a = a; n = a.length; if (n == 0) // base case result = 0; else result = a[n] + sumOfArray(a[n-1]); return result; } // End SumOfArray method } // End SumOfArray Class But I'm getting three error which are all related, I believe,

Longest path between 2 Nodes

非 Y 不嫁゛ 提交于 2019-12-29 04:42:14
问题 Calculate the longest path between two nodes. The path is in an arch. Signature of method is: public static int longestPath(Node n) In the example binary tree below, it is 4 (going thru 2-3-13-5-2). This is what I have right now and for the given tree it just returns 0. public static int longestPath(Node n) { if (n != null) { longestPath(n, 0); } return 0; } private static int longestPath(Node n, int prevNodePath) { if (n != null && n.getLeftSon() != null && n.getRightSon() != null) { int

Visiting a directed graph as if it were an undirected one, using a recursive query

半世苍凉 提交于 2019-12-29 04:25:10
问题 I need your help about the visit of a directed graph stored in a database. Consider the following directed graph 1->2 2->1,3 3->1 A table stores those relations: create database test; \c test; create table ownership ( parent bigint, child bigint, primary key (parent, child) ); insert into ownership (parent, child) values (1, 2); insert into ownership (parent, child) values (2, 1); insert into ownership (parent, child) values (2, 3); insert into ownership (parent, child) values (3, 1); I'd

Convert normal recursion to tail recursion

纵然是瞬间 提交于 2019-12-29 03:15:10
问题 I was wondering if there is some general method to convert a "normal" recursion with foo(...) + foo(...) as the last call to a tail-recursion. For example (scala): def pascal(c: Int, r: Int): Int = { if (c == 0 || c == r) 1 else pascal(c - 1, r - 1) + pascal(c, r - 1) } A general solution for functional languages to convert recursive function to a tail-call equivalent: A simple way is to wrap the non tail-recursive function in the Trampoline monad. def pascalM(c: Int, r: Int): Trampoline[Int]

Haskell infinite recursion in list comprehension

删除回忆录丶 提交于 2019-12-29 01:28:09
问题 I am trying to define a function that accepts a point (x,y) as input, and returns an infinite list corresponding to recursively calling P = (u^2 − v^2 + x, 2uv + y) The initial values of u and v are both 0. The first call would be P = (0^2 - 0^2 + 1, 2(0)(0) + 2) = (1,2) Then that resulting tuple (1,2) would be the next values for u and v, so then it would be P = (1^2 - 2^2 + 1, 2(1)(2) + 2) = (-2,6) and so on. I'm trying to figure out how to code this in Haskell. This is what I have so far:

.NET, C#, Reflection: list the fields of a field that, itself, has fields

不问归期 提交于 2019-12-29 01:24:10
问题 In .NET & C#, suppose ClassB has a field that is of type ClassA . One can easily use method GetFields to list ClassB 's fields. However, I want to also list the fields of those ClassB fields that themselves have fields. For example, ClassB 's field x has fields b , s , and i . I'd like to (programmatically) list those fields (as suggested by my comments in the code below). class ClassA { public byte b ; public short s ; public int i ; } class ClassB { public long l ; public ClassA x ; } class

Understanding Y Combinator through generic lambdas

不羁岁月 提交于 2019-12-28 16:09:42
问题 While building a small lambda-based metaprogramming library, I had the necessity of using recursion in a C++14 generic lambda, to implement a left-fold. My own solution was passing the lambda itself as one of its parameters, like this: template <typename TAcc, typename TF, typename... Ts> constexpr auto fold_l_impl(TAcc acc, TF f, Ts... xs) { // Folding step. auto step([=](auto self) { return [=](auto y_acc, auto y_x, auto... y_xs) { // Compute next folding step. auto next(f(y_acc, y_x)); //

is erlangs recursive functions not just a goto?

醉酒当歌 提交于 2019-12-28 15:26:10
问题 Just to get it straight in my head. Consider this example bit of Erlang code: test() -> receive {From, whatever} -> %% do something test(); {From, somethingelse} -> %% do something else test(); end. Isn't the test() call, just a goto? I ask this because in C we learned, if you do a function call, the return location is always put on the stack. I can't imagine this must be the case in Erlang here since this would result in a stackoverflow. In basic. We had 2 different ways of calling functions

Sudoku solver in Java, using backtracking and recursion

廉价感情. 提交于 2019-12-28 13:46:25
问题 I am programming a Sudoku solver in Java for a 9x9 grid. I have methods for: printing the grid initializing the board with given values testing for conflicts (if same number is in same line or 3x3 sub-grid) a method to place the digits, one by one, which requires the most work. Before I go into detail with that method, keep in mind that I have to use recursion to solve it, as well as backtracking (watch the applet here as an example http://www.heimetli.ch/ffh/simplifiedsudoku.html ) Also, I

Recursive mysql select?

半腔热情 提交于 2019-12-28 12:42:49
问题 I saw this answer and i hope he is incorrect, just like someone was incorrect telling primary keys are on a column and I can't set it on multiple columns. Here is my table create table Users(id INT primary key AUTO_INCREMENT, parent INT, name TEXT NOT NULL, FOREIGN KEY(parent) REFERENCES Users(id) ); +----+--------+---------+ | id | parent | name | +----+--------+---------+ | 1 | NULL | root | | 2 | 1 | one | | 3 | 1 | 1down | | 4 | 2 | one_a | | 5 | 4 | one_a_b | +----+--------+---------+ I