recursion

recursion with a list in python

这一生的挚爱 提交于 2021-02-05 04:55:36
问题 I just started learning python and there are some recursion questions that I can't seem to figure out. The most annoying one is this: I need to build a function ind(e,L) where e is an int and L is a list. By entering e if it is in the list the output needs to be its index For example: ind(42,[0,14,52,42,15]) -> 3 This is the code I wrote this far but the index I get is always 0. Can someone please explain to me what I am doing wrong? def location(e,L): if L == []: return False elif e == L[0]:

How to split object into nested object? (Recursive way)

僤鯓⒐⒋嵵緔 提交于 2021-02-04 22:18:12
问题 I have a data set containing underscore(_) variable name. Such as below: const data = { m_name: 'my name', m_address: 'my address', p_1_category: 'cat 1', p_1_name: 'name 1', p_2_category: 'cat 2', p_2_name: 'name 2' } I want to split them into nested object/array, Below is the result I want. { m: { name: "my name", address: "my address" }, p: { "1": {category: 'cat 1', name: 'name 1'}, "2": {category: 'cat 2', name: 'name 2'} } } How can I write a recursive method to achive it instead of

Converting flat hierarchy to nested hierarchy in XSLT depth

*爱你&永不变心* 提交于 2021-02-04 21:13:48
问题 I have some frustratingly flat XML that looks like this: <groups> <group id="1" depth="1" /> <group id="2" depth="2" /> <group id="3" depth="2" /> <group id="4" depth="3" /> <group id="5" depth="2" /> <group id="6" depth="3" /> </groups> The maximum value of depth appears to be 5 . Which I'd like to turn into XML that looks like this: <groups> <group id="1" depth="1"> <group id="2" depth="2"> </group> <group id="3" depth="2"> <group id="4" depth="3"> </group> </group> <group id="5" depth="2">

Converting flat hierarchy to nested hierarchy in XSLT depth

偶尔善良 提交于 2021-02-04 21:13:06
问题 I have some frustratingly flat XML that looks like this: <groups> <group id="1" depth="1" /> <group id="2" depth="2" /> <group id="3" depth="2" /> <group id="4" depth="3" /> <group id="5" depth="2" /> <group id="6" depth="3" /> </groups> The maximum value of depth appears to be 5 . Which I'd like to turn into XML that looks like this: <groups> <group id="1" depth="1"> <group id="2" depth="2"> </group> <group id="3" depth="2"> <group id="4" depth="3"> </group> </group> <group id="5" depth="2">

Transducer flatten and uniq

£可爱£侵袭症+ 提交于 2021-02-04 20:58:49
问题 I'm wondering if there is a way by using a transducer for flattening a list and filter on unique values? By chaining, it is very easy: import {uniq, flattenDeep} from 'lodash';| const arr = [1, 2, [2, 3], [1, [4, 5]]]; uniq(flattendDeep(arr)); // -> [1, 2, 3, 4, 5] <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.core.min.js"></script> But here we loop twice over the list (+ n by depth layer). Not ideal. What I'm trying to achieve is to use a transducer for this

Transducer flatten and uniq

戏子无情 提交于 2021-02-04 20:58:26
问题 I'm wondering if there is a way by using a transducer for flattening a list and filter on unique values? By chaining, it is very easy: import {uniq, flattenDeep} from 'lodash';| const arr = [1, 2, [2, 3], [1, [4, 5]]]; uniq(flattendDeep(arr)); // -> [1, 2, 3, 4, 5] <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.core.min.js"></script> But here we loop twice over the list (+ n by depth layer). Not ideal. What I'm trying to achieve is to use a transducer for this

Transducer flatten and uniq

妖精的绣舞 提交于 2021-02-04 20:58:16
问题 I'm wondering if there is a way by using a transducer for flattening a list and filter on unique values? By chaining, it is very easy: import {uniq, flattenDeep} from 'lodash';| const arr = [1, 2, [2, 3], [1, [4, 5]]]; uniq(flattendDeep(arr)); // -> [1, 2, 3, 4, 5] <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.core.min.js"></script> But here we loop twice over the list (+ n by depth layer). Not ideal. What I'm trying to achieve is to use a transducer for this

Curried javascript sum function.

半腔热情 提交于 2021-02-04 16:28:11
问题 I came across this interesting problem. Write a javascript function that returns sum of all the arguments passed to it, through multiple calls to that same function. Here are the ways function can be called - sum(1, 2, 3, 4); sum(1, 2)(3, 4); sum(1, 2)(3)(4); sum(1, 2, 3)(4); sum(1)(2, 3, 4); All the calls above should work and return 10. Here's what I have written so far, but it only works for first two function calls sum(1, 2, 3, 4) and sum(1, 2)(3, 4) and shits the bed for rest of it.

Algorithm to print all valid combations of n pairs of parenthesis

旧巷老猫 提交于 2021-02-04 16:17:32
问题 I'm working on the problem stated in the question statement. I know my solution is correct (ran the program) but I'm curious as to whether or not I'm analyzing my code (below) correctly. def parens(num) return ["()"] if num == 1 paren_arr = [] parens(num-1).each do |paren| paren_arr << paren + "()" unless "()#{paren}" == "#{paren}()" paren_arr << "()#{paren}" paren_arr << "(#{paren})" end paren_arr end parens(3), as an example, will output the following: ["()()()", "(()())", "(())()", "()(())

Finding the minimum of an array using recursion?

我们两清 提交于 2021-02-04 15:28:45
问题 Ok, so I've been trying to wrap my head around recursion in Java and I can accomplish easy tasks such as sum, reversing etc. but I have been struggling to do this exercise: I'm trying to find the minimum number in an array using recursion but keep getting an answer of 0.0. My understanding for recursion is that there I need to increment one element and then provide a base case that will end the recursion. I think I'm messing up when I have to return a value and when is best to call the