recursion

Python - Find the “depth” of an element in list in a recursive loop

核能气质少年 提交于 2021-02-10 17:01:25
问题 I want to know the depth of an element in a list in Python using a recursive loop (not a function) but it doesn't work. I find some answers with functions but it's not the point here. Something like 'a' is depth 2 and 'd' is depth 3 in the list below Here is my code: list1 = [['x','y'], ['z','p'], ['m',['a','b','c',['d','e']]]] level = 0 def print_list(l): for e in l: global level if type(e) == list: print_list(e) level +=1 else: print(str(e) + ",", str(level)) print_list(list1) Result: x, 0

How can i create a multi level menu in MVC core?

守給你的承諾、 提交于 2021-02-10 14:33:19
问题 The HTML menu rendered from my helper function is Category 1 Category 2 Category 2 Category 3 Category 4 I have set Category 2 as a child category of Category 1 . Unfortunately, the current HTML helper displays again Category 2 as a parent afterwards. Id ParentCategoryId Description 1 NULL Category 1 2 1 Category 2 3 NULL Category 3 4 NULL Category 4 5 NULL Category 5 How should I modify my helper function? @{ Func<dynamic, IHtmlContent> ShowMenu(List<Category> cats) => @<ul> @foreach (var

Multidimensional Arrays, Vuex & Mutations

人走茶凉 提交于 2021-02-10 13:23:37
问题 I'm attempting to both add and remove items in a multidimensional array stored in Vuex. The array is a group of categories, and each category and have a sub-category (infinity, not simply a two dimensional array). Example data set is something like this: [ { id: 123, name: 'technology', parent_id: null, children: [ id: 456, name: 'languages', parent_id: 123, children: [ { id:789, name: 'javascript', parent_id: 456 }, { id:987, name: 'php', parent_id: 456 } ] }, { id: 333, name: 'frameworks',

Multidimensional Arrays, Vuex & Mutations

风格不统一 提交于 2021-02-10 13:23:11
问题 I'm attempting to both add and remove items in a multidimensional array stored in Vuex. The array is a group of categories, and each category and have a sub-category (infinity, not simply a two dimensional array). Example data set is something like this: [ { id: 123, name: 'technology', parent_id: null, children: [ id: 456, name: 'languages', parent_id: 123, children: [ { id:789, name: 'javascript', parent_id: 456 }, { id:987, name: 'php', parent_id: 456 } ] }, { id: 333, name: 'frameworks',

How to recursively sum and store all child values in a tree

喜欢而已 提交于 2021-02-10 12:43:12
问题 Given a tree, what's the easiest way to calculate the sum of all children at a given node? Say a tree like this... The red values represent the sum of the node and its children. Let's say the node structure look like this (an example): class Node: def __init__(self, name): self.children = [] self.weight = 100 self.weight_plus_children = 295 How can I do this in an efficient, single pass (in Python)? Thanks! 回答1: Just judge if a node is a leaf and add the sum to the weight, here is an example:

Java Recursive Method how does it work?

和自甴很熟 提交于 2021-02-10 12:40:44
问题 I'm relatively new to Java programming and I've just started learning recursion, but I can't seem to figure out how this method works in my head. private static int mystery(int w) { { if (w < 0) return 0; int x = mystery (w-2); return w - x; } } Whenever a variable like 100 is put in, it outputs 50. When 200 is input, it outputs 100. When 2 is input, it outputs 2. When 25 is input, 13 is output. I'm not sure how this method works, and I'm trying to wrap my head around it. The way I currently

Convert List comprehension into recursive call

落爺英雄遲暮 提交于 2021-02-10 12:10:40
问题 sieve [] = [] sieve (a:x) = a : sieve [y| y <- x, y `mod` a > 0] I want to convert this code to recursive implementation or using higher order functions such as map and filter. I can't figure out how do I do this. I have tried this way but it wont seem to work sieve (a:x) = f x : map f xs where f = y `mod` a > 0 回答1: Is this the kind of thing you want? The list comprehension is only being used to filter the list anyway, so we can convert to a form that manually applies a filter. sieve [] = []

Convert List comprehension into recursive call

本秂侑毒 提交于 2021-02-10 12:04:41
问题 sieve [] = [] sieve (a:x) = a : sieve [y| y <- x, y `mod` a > 0] I want to convert this code to recursive implementation or using higher order functions such as map and filter. I can't figure out how do I do this. I have tried this way but it wont seem to work sieve (a:x) = f x : map f xs where f = y `mod` a > 0 回答1: Is this the kind of thing you want? The list comprehension is only being used to filter the list anyway, so we can convert to a form that manually applies a filter. sieve [] = []

Is it always a good practice to wrap a recursive function?

拈花ヽ惹草 提交于 2021-02-10 11:51:37
问题 I am using a recursive function to calculate the number of possible traversals through a graph starting from a node and ends with another given a set of rules (eg: minimum/maximum/exact number of stops). I am wondering if it is a good practice to call a wrapper function that calls the recursive function instead of calling it directly. Most of the time I see people use a wrapper function. Just wondering why and what are the pros and cons and in what situations must we wrap it? 回答1: No, it's

How await a recursive Promise in Javascript

廉价感情. 提交于 2021-02-10 06:54:18
问题 I have written a recursive Promise in javascript which seems to be working fine but I wanted to test it using setTimeout() to be sure that I'm awaiting correctly before continuing with the execution. Here is the gist of my code: try{ await renameFiles(); // <-- await here console.log("do other stuff"); } catch(){ } const renameFiles = (path) => { return new Promise(resolve => { console.log("Renaming files..."); fs.readdirSync(path).forEach(file) => { // if file is a directory ... let newPath