recursion

Why after pressing semicolon program is back in deep recursion?

强颜欢笑 提交于 2021-02-04 07:34:28
问题 I'm trying to understand the semicolon functionality. I have this code: del(X,[X|Rest],Rest). del(X,[Y|Tail],[Y|Rest]) :- del(X,Tail,Rest). permutation([],[]). permutation(L,[X|P]) :- del(X,L,L1), permutation(L1,P). It's the simple predicate to show all permutations of given list. I used the built-in graphical debugger in SWI-Prolog because I wanted to understand how it works and I understand for the first case which returns the list given in argument. Here is the diagram which I made for

Why after pressing semicolon program is back in deep recursion?

青春壹個敷衍的年華 提交于 2021-02-04 07:34:25
问题 I'm trying to understand the semicolon functionality. I have this code: del(X,[X|Rest],Rest). del(X,[Y|Tail],[Y|Rest]) :- del(X,Tail,Rest). permutation([],[]). permutation(L,[X|P]) :- del(X,L,L1), permutation(L1,P). It's the simple predicate to show all permutations of given list. I used the built-in graphical debugger in SWI-Prolog because I wanted to understand how it works and I understand for the first case which returns the list given in argument. Here is the diagram which I made for

Why after pressing semicolon program is back in deep recursion?

拈花ヽ惹草 提交于 2021-02-04 07:34:11
问题 I'm trying to understand the semicolon functionality. I have this code: del(X,[X|Rest],Rest). del(X,[Y|Tail],[Y|Rest]) :- del(X,Tail,Rest). permutation([],[]). permutation(L,[X|P]) :- del(X,L,L1), permutation(L1,P). It's the simple predicate to show all permutations of given list. I used the built-in graphical debugger in SWI-Prolog because I wanted to understand how it works and I understand for the first case which returns the list given in argument. Here is the diagram which I made for

Implement number of digits recursively in Python

别等时光非礼了梦想. 提交于 2021-01-29 18:54:31
问题 I have this code to print the numbers based on the number of digits inputted (for example printNum1(2) will output 10-99, printNum1(3) will output 100-999, printNum1(4) will output 1000-9999) iteratively in python: from time import time def printNum1(n): start = time() y = 10 ** n x = y//10 for i in range(x, y): print(i) print(f"For number {n}\nSol took: {time() - start} seconds") printNum1(5) I am trying to implement it recursively now for practice: from time import time from sys import

How to count items in list recursively

泄露秘密 提交于 2021-01-29 17:58:06
问题 I am looking to count the items in a list recursively. For example, I have a list few lists: a = ['b', 'c', 'h'] b = ['d'] c = ['e', 'f'] h = [] I was trying to find a way in which I find out the length of list 'a'. But in list 'a' I have 'b', 'c' and 'h' ... hence my function then goes into list 'b' and counts the number of elements there... Then list 'c' and then finally list 'h'. 回答1: b = ['d'] c = ['e', 'f'] h = [] a = [b,c,h] def recur(l): if not l: # keep going until list is empty

Assemble menu (tree of nodes), where only parent is known

我怕爱的太早我们不能终老 提交于 2021-01-29 17:24:40
问题 I have a menu navigation, like this: - Page 1 - Page 2 - Subpage A - Subpage B - Subpage C - Page 3 - Subpage D - Subpage E - Subsubpage I - Subsubpage II - Page 4 - Subpage F But the CMS I use spits out a flat tree, only displaying the parent (not which level they're on). Like so: $orig_tree = [ [ 'db_id' => 1, 'name' => 'Page 1', 'parent' => 0 ], [ 'db_id' => 2, 'name' => 'Page 2', 'parent' => 0 ], 'db_id' => 3, 'name' => 'Subpage A', 'parent' => 2 ], [ 'db_id' => 4, 'name' => 'Subpage B',

The empty array in this recursive code is accessible how?

。_饼干妹妹 提交于 2021-01-29 16:32:26
问题 I have looked at this long and hard. I understand recursion, I understand the call stack and the concept of “Last in, first out” but in this example. function countup(n) { if (n < 1) { return []; } else { const countArray = countup(n - 1); countArray.push(n); return countArray; } } console.log(countup(5)); // [ 1, 2, 3, 4, 5 ] When we meet the 'base condition' and we return []; , how is the array accessible if it does not have a name? and when is the name countArray attributed to this array?

how return one of the function between OR operator? [duplicate]

风流意气都作罢 提交于 2021-01-29 14:38:09
问题 This question already has answers here : How does this recursion work? (11 answers) Closed 26 days ago . I can not understand when the 'find' function will true or false in the else block. Here is the code: function findSolution(target) { function find(current, history) { if(current == target) { return history; } else if (current > target) { return null; } else { return find(current + 5, `(${history} + 5 )`) || find(current * 3, `(${history} * 3)`); } } return find(1, "1"); } console.log

Exit recursion for matplotlib events

安稳与你 提交于 2021-01-29 14:05:37
问题 I have a little matplotlib figure with a button_press_event . Inside the listener I use plt.pause to make a short animation for every click. This works fine and as expected. However if I click again before the animation is over, I enter a recursion and the remaining animations are played at the end. If you click fast enough you can even reach the RecursionError . What do I need to change, so a new click discards all remaining steps in the on_click method? import numpy as np import matplotlib

Javascript - recursively remove nodes of a certain type from tree, but reattach and spread eligible children

跟風遠走 提交于 2021-01-29 13:52:51
问题 I'm writing a recursive function on a JSON tree {name, type, [children]} to remove nodes of a certain type. However, the children of the removed node should get re-attached to the parent, if they are not of the type to be removed. I'm experiencing the following difficulty: Let's say I want to remove type b on the following tree: const sampleData = [{ name: "parent", type: "a", children: [{ name: "childA", type: "a", children: null },{ name: "childB", type: "b", children: [{ name: "grandChildA