recursion

Understanding Peter Norvig's permutation solution in PAIP

為{幸葍}努か 提交于 2021-02-20 19:00:28
问题 Peter Norvig's PAIP book contains this code as a solution to the permutation problem (some sections are removed for brevity) (defun permutations (bag) ;; If the input is nil, there is only one permutation: ;; nil itself (if (null bag) '(()) ;; Otherwise, take an element, e, out of the bag. ;; Generate all permutations of the remaining elements, ;; And add e to the front of each of these. ;; Do this for all possible e to generate all permutations. (mapcan #'(lambda (e) (mapcar #'(lambda (p)

Integer partitioning in Java

这一生的挚爱 提交于 2021-02-20 04:14:48
问题 I'm trying to implement a program that returns the number of existing partitions of an integer n as part of an assignment. I wrote the code below, but it returns the wrong number (Partitions n returns the result of Partitions n-1). I don't get why this happens. I've tried many things and still don't know how to fix it, can anyone please help me? [edited code out to avoid plagiarism from my colleagues :p] m stands for the biggest number allowed in a partition, so partition(4,4) would be 5 = 4,

Recursively find the second derivative

佐手、 提交于 2021-02-19 23:37:10
问题 I'm attempting to write a program in C which finds the numeric value of the second derivative of f(x) where x is given. When I use my function to find the first derivative, everything seems to be working okay, but the recursive second derivative part always gives me 0.0. Here's my function: double derivative(double (*f)(double), double x0, int order) { if(order == 1){ const double delta = 1.0e-6; double x1 = x0-delta; double x2 = x0+delta; double y1 = f(x1); double y2 = f(x2); return (y2 - y1

find all possible paths in a tree in python

馋奶兔 提交于 2021-02-19 08:33:57
问题 I am trying to create a list with all possible paths in a tree. I have following structure given (subset from DB): text = """ 1,Product1,INVOICE_FEE, 3,Product3,INVOICE_FEE, 7,Product7,DEFAULT, 2,Product2,DEFAULT,7 4,Product4,DEFAULT,7 5,Product5,DEFAULT,2 """ where the columns are: ID, product-name, invoice-type, reference-to-parent-ID. I would like to create list with all possible paths, like in the example: [[Product1],[Product3],[Product7,Product2,Product5],[Product7,Product4]] I do

find all possible paths in a tree in python

ε祈祈猫儿з 提交于 2021-02-19 08:33:25
问题 I am trying to create a list with all possible paths in a tree. I have following structure given (subset from DB): text = """ 1,Product1,INVOICE_FEE, 3,Product3,INVOICE_FEE, 7,Product7,DEFAULT, 2,Product2,DEFAULT,7 4,Product4,DEFAULT,7 5,Product5,DEFAULT,2 """ where the columns are: ID, product-name, invoice-type, reference-to-parent-ID. I would like to create list with all possible paths, like in the example: [[Product1],[Product3],[Product7,Product2,Product5],[Product7,Product4]] I do

String to multidimensional/recursive array in PHP

女生的网名这么多〃 提交于 2021-02-19 08:25:06
问题 I have a problem with creating a recursive array with PHP. I need to format this string to a multidimensional array with the dot-separated elements indicating multiple levels of array keys. $str = "code.engine,max_int.4,user.pre.3,user.data.4"; The output for this example would be: $str = array( "code" => "engine", "max_int" => 4, "user" => array( "pre" => 3, "data" => 4 ) ); I will start with an explode function, but I don't know how to sort it from there, or how to finish the foreach. 回答1:

Recursive function in R with ending point varying by group

核能气质少年 提交于 2021-02-19 08:01:41
问题 I wish to use a recursive structure in my dplyr change that iterates on the number of lags used on certain operations. The thing is that I am not sure how to set its ending point since it resembles more a while than a for loop, which makes me a bit insecure. Here is some sample data. Groups are not necessarily the same size and are indexed by id df <- data.frame(id = c(1, 1, 1, 1, 2, 2, 3, 4, 5, 5, 5), p201 = c(NA, NA, "001", NA, NA, NA, "001", "001", "001", NA, NA), V2009 = c(25, 11, 63, 75,

Recursively remove nullish values from a JavaScript object

匆匆过客 提交于 2021-02-19 07:39:10
问题 I searched for this but couldn't find a satisfactory answer so I'm posting my own answer here. Basically I wanted a function that: takes an object as its argument recursively removes properties whose values are null , undefined , [] , {} or '' retains 0 and false values returns a new object with those properties removed preferably in a functional style without mutations 回答1: Here's what I came up with. (Thanks to Nina for providing a sample ;) const is_obj = x => x !== null && typeof x ===

Runtime Error in python: “Maximum recursion depth exceeded”

∥☆過路亽.° 提交于 2021-02-19 06:16:31
问题 I have a program that searches through a 2D list that represents a maze like so: #################################### #S# ## ######## # # # # # # # # # # # # # # ##### ## ###### # ####### # # ### # ## ## # # # #### # # # # ####### # ### #E# #################################### I understand what a recursion error is but I don't know why this code causes it as it should simply lead to finding the "E". Does anyone know how this possibly generates the error? def solve(x,y): mazeList = loadMaze(

Recursive function of Bezier Curve python

别来无恙 提交于 2021-02-19 05:44:17
问题 I am asked to design a recursive function called Bezier which parametres are a list of points given, and the point that must be evaluated t. It returns the point in the Bezier curve defined by the control points of the list of points. This is the algorithm that I have done: def Bezier(point_list, t): if len(point_list)==1: return point_list[0] else: P1=Bezier(point_list[0:-1],t) P2=Bezier(point_list[1:],t) P=(1-t)*P1 + t*P2 return P and this is the list of points given: point_list=[ (0,0),