I wrote this for python code and got an unexpected output. The output was a number of zeros then it said “restarting kernel”. Why is the kernel restarting? On the other hand, I tried with if and there was no problem: So why is it not working with for? Answer In your code above, each function call will call itself
Tag: recursion
Generating all possibilities of a string given a list of chars but want only the strings up to n length
I have written the below code to recursively generate all of the possible combinations of a string. However I want to only produce all the possibilities of the string up to a length of n. Say the string is ‘abc’, and n=2. Instead of outputting all of the possible strings of ‘abc’ I only want to output the possible strings
Python: Building list of lists using dictionary recursion
I have a very complicated problem that I am sort of hoping to rubberduck here: I have a dictionary: whose structure wont always be the same (i.e., there could be further nesting or more keys in any sub-dictionary). I need to be able to generate a specifically ordered list of lists (contained sub-lists need not be ordered) based on some
Values don’t change as they should because of recursion depth
I am making checkers. I encountered quite a big problem. The code below describes cells king can move on. 1 cell is 100×100. He starts from (550, 50). This picture describes current_battlefield: And this dict describes what moves AI will take The king is a purple checker and he has to eat right now. So, about the actual problem. If
Python Trouble with matrix pathfinding (DFS)
I am having issues with dfs, probably from a RecursionError when facing a wall. Instead of continuously running an attempt which can only lead to a wall, it is supposed to return to its previous position and try another path. Also, it leans heavily on the order in which attempts are made (N,S,E,W or other combinations) Thanks in advance Answer
Algorithm verification: Get all the combinaison of possible word
I wanted to know if the algorithm that i wrotte just below in python is correct. My goal is to find an algorithm that print/find all the possible combinaison of words that can be done using the character from character ‘!’ (decimal value = 33) to character ‘~’ (decimal value = 126) in the asccii table: Here the code using
Find root to leaf path with given sum. Code succeeds with string, fails with list
I am looking at a binary tree problem. The goal is to find all path(s) from root to leaf, such that the values on the path sum up to a given k. For that purpose I have written the same algorithm in two different ways: The first version uses a string parameter a. The second version uses a list parameter
Using recursion to determine the index path of a nested function
Im trying to make a function which finds a value from a list (xs) using another list (index_list) as an index path. My function should work like this: So far I have: This however just returns 0 for everything, but I don’t know what else the base case should be. Answer You’re quite close, but you forgot that at each
Python DFS nested dictionary
I’ve written a function which should be able to search a nested dictionary, using DFS, to find a specific value. The recursive element seems to be working fine, however, when the base case should return True, it simply doesn’t. And the results. As you can see, the standard output “i==i” shows that this element was evaluated correctly but the return
Wrong result if a recursive function is called twice successively with different parameters
I have this recursive function: When I print this: It shows -1, which is correct. But, when I print this: It shows 3 then 2. As you can see, the result of print(coinChange([2], 3)) weirdly changed from -1 to 2. The memo is the reason for causing that wrong answer. But I don’t know how to update the function so