recursion

Convert this recursive function to iterative

瘦欲@ 提交于 2020-01-12 20:58:27
问题 How can I convert this recursive function to an iterative function? #include <cmath> int M(int H, int T){ if (H == 0) return T; if (H + 1 >= T) return pow(2, T) - 1; return M(H - 1, T - 1) + M(H, T - 1) + 1; } Well it's a 3-line code but it's very hard for me to convert this to an iterative function. Because it has 2 variables. And I don't know anything about Stacks so I couldn't convert that. My purpose for doing this is speed of the function. This function is too slow. I wanted to use map

Recursively Collect Children in Python/Django

南笙酒味 提交于 2020-01-12 17:26:20
问题 I have a model like so.... class Person(models.Model): name = models.CharField(max_length=55,null=False, blank=False) parent = models.ForeignKey('Person.Person', null=False, blank=False) I want to create a recursive function that will eventually return a dictionary of an entire persons family tree.... So for example... first_person = Person.objects.filter(name='FirstPerson') family_tree = GetChildren(first_person) Where GetChildren is my recursive function that will continuously call

Return nested list with nested level and value

不羁的心 提交于 2020-01-12 16:23:30
问题 I would like to visualize some deeply nested data using networkD3 . I can't figure out how to get the data into the right format before sending to radialNetwork . Here is some sample data: level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3) value <- letters[1:11] where level indicates the level of the nest, and value is the name of the node. By using these two vectors, I need to get the data into the following format: my_list <- list( name = "root", children = list( list( name = value[1], ## a

Computing method call stack size for checking StackOverflowException

北城以北 提交于 2020-01-12 14:07:08
问题 Today morning I answered a question which is related to StackoverflowException . The person has asked when Stackoverflow exception occurs See this link Simplest ways to cause stack overflow in C#, C++ and Java So my question is that is there any method by which we can compute the method call stacks size dynamically in our program and then applying a check before calling a method which checks whether method call stack has space to accommodate it or not to prevent StackOverflowException. As I

How to traverse JSON object locating particular property and pushing its contents to array?

本小妞迷上赌 提交于 2020-01-12 10:40:34
问题 I am working with a JSON object which can have a property ids at any leaf. I want to traverse the object and find all of the instances of the ids property and store each id in a collection. Mocked up JSON Object (the ids property could be at much deeper property locations). { "id": "b38a683d-3fb6-408f-9ef6-f4b853ed1193", "foo": { "ids": [ { "id": "bd0bf3bd-d6b9-4706-bfcb-9c867e47b881" }, { "id": "d1cc529d-d5d2-4460-b2bb-acf24a7c5999" }, { "id": "b68d0c8c-548e-472f-9b01-f25d4b199a71" } ], "baz

PHP script to traverse directory/file tree and output tree as nested ULs [closed]

Deadly 提交于 2020-01-12 08:27:10
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . I have a tree of directories, sub-directories, and files (in some but not all the directories). Here's an example of the whole tree: /food /food/drinks /food/drinks/water.html /food/drinks/milk.html /food/drinks/soda.html /food/entrees /food/entrees/hot /food/entrees/hot/hamburger

Python: flatten nested lists with indices

别来无恙 提交于 2020-01-12 06:53:29
问题 Given a list of arbitrairly deep nested lists of arbitrary size, I would like an flat, depth-first iterator over all elements in the tree, but with path indicies as well such that: for x, y in flatten(L), x == L[y[0]][y[1]]...[y[-1]]. That is L = [[[1, 2, 3], [4, 5]], [6], [7,[8,9]], 10] flatten(L) should yield: (1, (0, 0, 0)), (2, (0, 0, 1)), (3, (0, 0, 2)), (4, (0, 1, 0)), (5, (0, 1, 1)), (6, (1, 0)), (7, (2, 0)), (8, (2, 1, 0)), (9, (2, 1, 1)), (10, (3,)) I made a recursive implementation

Python: flatten nested lists with indices

ぃ、小莉子 提交于 2020-01-12 06:53:27
问题 Given a list of arbitrairly deep nested lists of arbitrary size, I would like an flat, depth-first iterator over all elements in the tree, but with path indicies as well such that: for x, y in flatten(L), x == L[y[0]][y[1]]...[y[-1]]. That is L = [[[1, 2, 3], [4, 5]], [6], [7,[8,9]], 10] flatten(L) should yield: (1, (0, 0, 0)), (2, (0, 0, 1)), (3, (0, 0, 2)), (4, (0, 1, 0)), (5, (0, 1, 1)), (6, (1, 0)), (7, (2, 0)), (8, (2, 1, 0)), (9, (2, 1, 1)), (10, (3,)) I made a recursive implementation

Recursively search nested lists

我们两清 提交于 2020-01-12 06:16:20
问题 I've read and searched and I'm yet to figure out an answer to this relatively simple issue. I have a class: public class AccessibleTreeItem { public string name; public List<AccessibleTreeItem> children; public AccessibleTreeItem() { children = new List<AccessibleTreeItem>(); } } which is populate using a series of functions that don't really matter in this context, but what I'm looking for is a way to search through ALL of the children items in the list, searching for a particular 'name'

Recursively search nested lists

℡╲_俬逩灬. 提交于 2020-01-12 06:14:27
问题 I've read and searched and I'm yet to figure out an answer to this relatively simple issue. I have a class: public class AccessibleTreeItem { public string name; public List<AccessibleTreeItem> children; public AccessibleTreeItem() { children = new List<AccessibleTreeItem>(); } } which is populate using a series of functions that don't really matter in this context, but what I'm looking for is a way to search through ALL of the children items in the list, searching for a particular 'name'