recursion

python recursion list combinations without using generator

浪子不回头ぞ 提交于 2021-02-08 05:45:18
问题 I am learning python3. To think more about recursion, I want to implement a function comb(n, k) that returns a list consisting of all the combinations of kk elements out of a set {1,2,…,n}. I think it's not wise to use the loop since the number of the nested loop depends on k. So I consider it with recursion. I try to write the function inspired by This question while I can't get the right answer. def combinations(sub, data_set, index, still_needed): if still_needed == 0: return sub for i in

Updating state from recursively rendered component in React.js

寵の児 提交于 2021-02-08 05:22:50
问题 I am in need of updating deeply nested object in a React state from a recursively rendered component. The items look like this and can be nested dynamically: const items = [ { id: "1", name: "Item 1", isChecked: true, children: [] }, { id: "3", name: "Item 3", isChecked: false, children: [ { id: "3.1", name: "Child 1", isChecked: false, children: [ { id: "3.1.1", name: "Grandchild 1", isChecked: true, children: [] }, { id: "3.1.2", name: "Grandchild 2", isChecked: true, children: [] } ] }, {

Updating state from recursively rendered component in React.js

徘徊边缘 提交于 2021-02-08 05:22:06
问题 I am in need of updating deeply nested object in a React state from a recursively rendered component. The items look like this and can be nested dynamically: const items = [ { id: "1", name: "Item 1", isChecked: true, children: [] }, { id: "3", name: "Item 3", isChecked: false, children: [ { id: "3.1", name: "Child 1", isChecked: false, children: [ { id: "3.1.1", name: "Grandchild 1", isChecked: true, children: [] }, { id: "3.1.2", name: "Grandchild 2", isChecked: true, children: [] } ] }, {

combine two list and sort recursively

感情迁移 提交于 2021-02-08 04:57:05
问题 I'm trying to define a function named combine to take two lists as parameters and combine them into one sorted list recursively. def combine (l1,l2): if l1 == []: return l2 elif l2 == []: return l1 elif l1 == [] and l2 == []: return [] sort_l = [] index = 0 for num in l1: if num <= l2[0]: sort_l.append(num) index += 1 else: return combine(l2, l1[index:]) return sort_l + l2 It gives me: combine([1,3,5,7,9],[0,2,4,6,8]) -> [8, 9] but should: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Any advice??? 回答1:

Find all subsets that sum to a particular value and then pick the most valuable combination of those subsets

半世苍凉 提交于 2021-02-08 04:48:38
问题 Im trying to create a simple dice game. To calculate the score i have to find all subsets that sum to a particular value and then pick the most valuable combination. All numbers can only be picked once. Probably easiest to describe with an example: Values = {1, 1, 1, 2, 4, 4} Target value = 5 Possible subsets = {1, 1, 1, 2} and {1, 4} Now its possible to either choose: {1, 1, 1, 2} (= worth 5) or {1, 4} {1, 4} (= worth 10) So in this example I would like the algorithm to return 10 and not 5 .

Javascript Object Assignment Infinite recursion

◇◆丶佛笑我妖孽 提交于 2021-02-08 03:33:06
问题 I have an issue that I am struggling to grasp. Any help would be greatly appreciated. I have an Object, and I assign the current object state to a property on the current object. example below: var product = { ropeType: 'blah', ropePrice: 'blah', ropeSections: { name: 'blaah', price: 'blaah' }, memory: false } product.memory = product; Now when I look at the product object within the console I get a inifinite recursion of Product.memory.Product.memory.Product.... screenshot below: I know its

Iterative logarithmic exponentiation

。_饼干妹妹 提交于 2021-02-08 02:01:55
问题 I bombed an interview (phone screen with collabedit) recently. Here is the question: Write an interative O(lg n) algorithm for finding the power of x^y (x is a double, y>0 is an int). I first did the recursive divide and conquer one and tried to convert it to iterative... and I couldn't :S Is there a method to convert recursion to iterative (it is easy for tail recursion, but how about recursive functions with two possible recursive calls which depend on conditions to decide which call will

Iterative logarithmic exponentiation

六眼飞鱼酱① 提交于 2021-02-08 02:01:36
问题 I bombed an interview (phone screen with collabedit) recently. Here is the question: Write an interative O(lg n) algorithm for finding the power of x^y (x is a double, y>0 is an int). I first did the recursive divide and conquer one and tried to convert it to iterative... and I couldn't :S Is there a method to convert recursion to iterative (it is easy for tail recursion, but how about recursive functions with two possible recursive calls which depend on conditions to decide which call will

Stack Overflow Error on recursion java

╄→гoц情女王★ 提交于 2021-02-08 01:51:54
问题 I need to find the longest way (from bigger to lower) between numbers in array. I've tried to write recursive function and got java.lang.StackOverflowError , but for the lack of knowledge I didn't understand why this happened. Firstly, I've initialize array and fill it with random numbers: public long[] singleMap = new long[20]; for (int i = 0; i < 20; i++) { singleMap[i] = (short) random.nextInt(30); } Then, I try to find the longest route of counting down numbers (e.g. { 1, 4, 6, 20, 19, 16

Stack Overflow Error on recursion java

别来无恙 提交于 2021-02-08 01:50:29
问题 I need to find the longest way (from bigger to lower) between numbers in array. I've tried to write recursive function and got java.lang.StackOverflowError , but for the lack of knowledge I didn't understand why this happened. Firstly, I've initialize array and fill it with random numbers: public long[] singleMap = new long[20]; for (int i = 0; i < 20; i++) { singleMap[i] = (short) random.nextInt(30); } Then, I try to find the longest route of counting down numbers (e.g. { 1, 4, 6, 20, 19, 16