recursion

How to generate a set of all tuples of given length and sum of elements?

落花浮王杯 提交于 2019-12-24 03:44:10
问题 I would like to have a function that generates a set (or a list) of all possible tuples with a given length and sum of their elements. The elements of tuples should be not negative integer. For example for the following input get_tuple(length=3, total=2) I would like to get the following output: [(1, 0, 1), (2, 0, 0), (1, 1, 0), (0, 0, 2), (0, 1, 1), (0, 2, 0)] Is the a standard library in Python that can do that? If not, how to write a function that can do it? 回答1: You can create a recursive

Dynamically create loops to iterate over a List of List<String>'s

爷,独闯天下 提交于 2019-12-24 03:43:27
问题 I have a List of List<String> 's which I get from a external API method call: List<List<String>> outerList I have to create unique key combinations by concatenating strings from each list in the same order they are in the outer list. Example: If outer list has 2 inner lists, say list1: {"A","B"} and list2: {"C","D"}. Then possible unique combinations will be AC, AD, BC and BD. But the problem is the outerList size is dynamic, it can contain any number of inner lists. If the inner list numbers

possible paths to the top of a staircase

只愿长相守 提交于 2019-12-24 03:34:46
问题 This is a pretty classic question, and I've heard Google has used this question in their interviews. Problem: Make a recursive method that prints all the possible unique ways from the base of a staircase to the top of the staircase with n stairs. You can only take 1-step or 2-steps at a time. Sample output: If it is a staircase with 3 stairs... 1 1 1 2 1 1 2 If it is a staircase with 4 stairs... 1 1 1 1 1 1 2 1 2 1 2 1 1 2 2 *order of the outputs does not matter I've seen similar questions

Size Procedure in Prolog Language

微笑、不失礼 提交于 2019-12-24 03:27:25
问题 I am new to Prolog and am not that great when it comes to recursive algorithms as is, thus I am confused with the following two clauses: size([], 0). size([H|T], N) :- size(T, N1), N is N1+1. I am having trouble tracing this problem for: ?- size([a,b,c,d], N). This will unify with the second clause to form: size([a,b,c,d], N) :- size([b,c,d], N1), N is N1+1. But I am confused with the N is N1+1 as these variables are never unified. What values do these variables take? Any help regarding this

Limiting recursion to certain level - Duplicate rows

馋奶兔 提交于 2019-12-24 03:23:31
问题 To start this isn't a duplicate of my other question with the same name, I just couldn't think of a better name for this one! I have a SQL table named Player and another called Unit. Each Player MUST belong to a team via a foreign key UnitID. Each Unit can belong to another Team via a recursive field ParentUnitID. A ParentUnit CAN be a ParentUnit (infinite recursion), but a Player can only belong to one Team. A Unit may have many children So it could be (top down)... TeamA (is the top level)

Using recursion to search all combinations of elements in an array of integers

て烟熏妆下的殇ゞ 提交于 2019-12-24 03:18:36
问题 I am working on this problem from Coderbyte using JS: Have the function ArrayAdditionI(arr) take the array of numbers stored in arr and return the string true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative

iterating through arraylists with recursion

此生再无相见时 提交于 2019-12-24 02:57:09
问题 I have written a method that basically checks to see if all the elements in two ArrayLists are equal and if so, return true. I believe this method works, however, I'd like to also write this same method solely using recursion without using any loops, meaning that I'd have to replace the for loop block. Any suggestions or hints? public static boolean isEqual(ArrayList<O> arrlist1, ArrayList<O> arrlist2) { ArrayList<O> um=new ArrayList<O>(); ArrayList<O> um2=new ArrayList<O>(); um=arrlist1; um2

Ensure only one setTimeout runs (is active) at a time?

≯℡__Kan透↙ 提交于 2019-12-24 02:54:09
问题 The recursive setTimeout function getRandomProducts is called onload in the html body tag, and so is constantly iterating. The function setCategoryTree is being called onclick from the links in a nested ul of a navigation-bar. This function then passes the variable mainCategory to getRandomProducts, in which the variable is declared globally to maintain its' initialization. ...So, what I am trying to do is reset the getRandomProducts function when a link is clicked in the navigation-bar, and

Lisp Infinite Recursion

女生的网名这么多〃 提交于 2019-12-24 02:49:45
问题 I am a new lisp programmer, I am having trouble wrapping my head around recursion in lisp. I have a series of expressions that I simplify by going through a series of methods that replace symbols with numbers and then I will evaluate the expression. Before evaluation I substitute symbols for numbers, in doing that I get a stack overflow error in my subst-bindings method and/or when I call the deep-subst method from within that method. Any help or advice on my recursive method calls that would

recursive method finding number of paths in a matrix

做~自己de王妃 提交于 2019-12-24 02:40:39
问题 i have wrote a method that calculates number of paths from a given cell in a 2-dimension array, to a given destination cell, but for some reason it returns an incorrect answer, any thoughts? private static int numParths(int [][] mat, int x1, int y1, int x2, int y2) { if(x1<0 || x1 >mat.length-1 || y1<0 || y1>mat.length-1) return 0; if(x1 == x2 && y1 == y2){ System.out.println("1"); return 1; } if(mat[x1][y1]==-1) return 0; mat[x1][y1]=-1; return numParths(mat, x1, y1+1, x2, y2) + numParths