recursion

Python every possible combination of a string

纵然是瞬间 提交于 2020-12-29 08:43:03
问题 Hi so I'm working with python and I'm trying to write a method where given a string, it would find every combination of that string and append it to a list. I'll give the string and show the outcome that I want. string: x = 'god' outcome: lst = ['g', 'o', 'd', 'go', 'gd', 'og', 'od', 'dg', 'do', 'god', 'gdo', 'ogd', 'odg', 'dgo', 'dog'] A letter can only be used by the number of times it appears on the string given, so if our string is 'god' , 'gg' or 'goo' etc. cannot be appended. If this

Why Python raises RecursionError before it exceeds the real recursion limit?

柔情痞子 提交于 2020-12-29 07:29:05
问题 So I was trying to play with sys.getrecursionlimit() and sys.setrecursionlimit() methods. By default recursion limit was 3000 . I tried to check it using this code: def recursive(n): print(n) recursive(n+1) recursive(0) It does print the numbers to 2979 , it delays for a second, prints 2980 and then raises the RecursionError RecursionError: maximum recursion depth exceeded while calling a Python object I know that error should be raised when it exceeds recursion limit that sys

Rendering nested/threaded comments in React

别来无恙 提交于 2020-12-28 21:10:23
问题 Given the below array, I'd like to render comments in a threaded manner by making use of parentId . comments: [ { id: 1, parentId: null }, { id: 2, parentId: 1 }, { id: 3, parentId: 1 }, { id: 4, parentId: 3 }, { id: 5, parentId: 4 } ] I thought with the below components I'd be able to recurse through the comments, but the output is not what I'd expect (it seems to be rendering a new <ul> element for every comment.) I'm a bit new to react and javascript, so maybe I'm not implementing the

Rendering nested/threaded comments in React

老子叫甜甜 提交于 2020-12-28 21:06:01
问题 Given the below array, I'd like to render comments in a threaded manner by making use of parentId . comments: [ { id: 1, parentId: null }, { id: 2, parentId: 1 }, { id: 3, parentId: 1 }, { id: 4, parentId: 3 }, { id: 5, parentId: 4 } ] I thought with the below components I'd be able to recurse through the comments, but the output is not what I'd expect (it seems to be rendering a new <ul> element for every comment.) I'm a bit new to react and javascript, so maybe I'm not implementing the

Rendering nested/threaded comments in React

怎甘沉沦 提交于 2020-12-28 21:03:21
问题 Given the below array, I'd like to render comments in a threaded manner by making use of parentId . comments: [ { id: 1, parentId: null }, { id: 2, parentId: 1 }, { id: 3, parentId: 1 }, { id: 4, parentId: 3 }, { id: 5, parentId: 4 } ] I thought with the below components I'd be able to recurse through the comments, but the output is not what I'd expect (it seems to be rendering a new <ul> element for every comment.) I'm a bit new to react and javascript, so maybe I'm not implementing the

Understanding recursion with the Fibonacci Series

别等时光非礼了梦想. 提交于 2020-12-27 08:59:06
问题 I am trying to better understand recursion and how return statements work. As such, I'm looking at a piece of code meant to identify the fibonacci number associated with a given term -- in this case, 4. I'm have difficulty understanding the else statement. def f(n): if n == 0: return 0 if n == 1: return 1 else: return f(n-1) + f(n-2) f(4) I have tried using Visualize Python to examine what happens at each step, but I get lost when it hits the else statement. It looks like it is taking the

A recursive function to sort a list of ints

醉酒当歌 提交于 2020-12-27 07:24:10
问题 I want to define a recursive function can sort any list of ints: def sort_l(l): if l==[]: return [] else: if len(l)==1: return [l[-1]] elif l[0]<l[1]: return [l[0]]+sort_l(l[1:]) else: return sort_l(l[1:])+[l[0]] Calling this function on a list [3, 1, 2,4,7,5,6,9,8] should give me: [1,2,3,4,5,6,7,8,9] But I get: print(sort_l([3, 1, 2,4,7,5,6,9,8]))--> [1, 2, 4, 5, 6, 8, 9, 7, 3] Please help me to fix the problem, actual code would be appreciated. Thanks! 回答1: The quick sort is recursive and

How to find indirect relation? [Python]

自古美人都是妖i 提交于 2020-12-27 07:18:48
问题 So I'm trying to find indirect relations in a dictionary but I can't seem to find a general code for my program: this is what I have #find if A is related to E data = {"A": {"B": 5, "C": 7}, "B": {"E": 8}, "C": {}, "D": {}, "E": {"D": 9}} if "E" in data["A"]: result = True if "E" in data["B"] or "D" in data["C"]: result = True else: result = False print(result) #output = True because "E" is in data["A"] For this one example it works and ofcourse I've could generalize this with x's and y's but

How to find indirect relation? [Python]

谁说我不能喝 提交于 2020-12-27 07:18:31
问题 So I'm trying to find indirect relations in a dictionary but I can't seem to find a general code for my program: this is what I have #find if A is related to E data = {"A": {"B": 5, "C": 7}, "B": {"E": 8}, "C": {}, "D": {}, "E": {"D": 9}} if "E" in data["A"]: result = True if "E" in data["B"] or "D" in data["C"]: result = True else: result = False print(result) #output = True because "E" is in data["A"] For this one example it works and ofcourse I've could generalize this with x's and y's but

Repeat a function composition n times in Python like Haskell's repeat

只谈情不闲聊 提交于 2020-12-26 18:51:18
问题 This code does NOT work: def inc(x): return x + 1 def repeat(f, n): if n == 0: return lambda x: x else: return f( repeat(f, n - 1 ) ) inc_10 = repeat(inc, 10) #TypeError: unsupported operand type(s) for +: 'function' and 'int' ''' # Ideally print(inc_10(0)) # 10 ''' How can I write it in a more Pythonic way or in lambda calculus way ? 回答1: You still need to return a function, not the result of calling f , in the recursive case. # repeat :: (a -> a) -> Integer -> a -> a # repeat _ 0 = id #