recursion

How to display parent id for itself and children with T-sql recursive query

旧时模样 提交于 2019-12-24 01:18:28
问题 I am working on recursive query which take table with parent-child relation ID | ParentID | description 1 | null | Company 2 | 1 | Department 3 | 2 | Unit1 4 | 2 | Unit2 5 | 4 | Unit3 6 | 4 | Unit4 and is suppose to display following result: ID | ParentID | description 1 | null | Company 2 | 2 | Department 3 | 2 | Unit1 4 | 2 | Unit2 5 | 2 | Unit3 6 | 2 | Unit4 Of course the number of Deparments and units is larger. The basic quest is to display parentId for parent and its child level. Do you

Trying to get my head around recursion in Haskell?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 01:18:13
问题 I have used many recursive functions now but still have trouble getting my head around how such a function exactly works (i'm familiar with the second line (i.e. | n==0 = 1 ) but am not so familiar with the final line (i.e. | n>0 = fac (n-1) * n )). fac :: Int -> Int fac n | n==0 = 1 | n>0 = fac (n-1) * n 回答1: Recursive algorithms are very closely linked to mathematical induction. Perhaps studying one will help you better understand the other. You need to keep two key principles in mind when

Time bound for recursive algorithm with constant combination time

主宰稳场 提交于 2019-12-24 01:14:48
问题 Suppose I have a recursive algorithm that splits input into 2 inputs of size n-1 and solves them recursively. It combines the results in a constant time say c. So formulating an equation for this, T(n) = 2T(n-1) + c In order to find the complexity of this one, I used the recursive tree method. Since input is divided into 2 at each step, the number of nodes will get squared at each step while since only one element is getting eliminated, every level will lose only one element from the list. So

How do determine Big-O of recursive code?

做~自己de王妃 提交于 2019-12-24 01:08:15
问题 I have the following code, which is an answer to this question: https://leetcode.com/problems/add-digits/ class Solution { public: int addDigits(int num) { if (!num/10) return num; long d = 1; int retVal = 0; while(num / d){ d *= 10; } for(d; d >= 1; d/=10){ retVal += num / d; num %= d; } if (retVal > 9) retVal = addDigits(retVal); return retVal; } }; As a follow-up to this though, I'm trying to determine what the BigO growth is. My first attempt at calculating it came out to be O(n^n) (I

Could an iterative function call itself?

て烟熏妆下的殇ゞ 提交于 2019-12-24 01:01:55
问题 When viewing the below MIT 6.001 course video, at 28:00 the instructor marks this algorithm as iteration. But, at 30.27 he says both this and the actual "recursive" algorithms are recursive. The function is calling itself with a base case, so how is this iteration? https://www.youtube.com/watch?v=dlbMuv-jix8&list=PLE18841CABEA24090&index=2 private int iterativeSum(int x, int y) { System.out.println("x "+x+" y "+y); if(x == 0) { return y; } return iterativeSum(--x, ++y); } 回答1: He seems to be

Get recursive group membership of all AD users using Powershell

血红的双手。 提交于 2019-12-24 00:59:37
问题 I'm trying to make a PS script which would list all Active Directory user group membership (recursive). I already have working script: import-module activedirectory $users = get-aduser -Filter {Name -Like "*"} -Searchbase "ou=Users, dc=Domain" | Where-Object { $_.Enabled -eq 'True' } $targetFile = "D:\users.csv" rm $targetFile Add-Content $targetFile "User;Group" foreach ($user in $users) { $groups = Get-ADPrincipalGroupMembership $user foreach ($group in $groups) { $username = $user

Updated: Applying a Math Formula in a more elegant way (maybe a recursive call would do the trick) [duplicate]

梦想的初衷 提交于 2019-12-24 00:58:06
问题 This question already has answers here : How to translate this Math Formula in Haskell or Python? (Was translated in PHP) (3 answers) Closed 2 years ago . ******Last news**** I've posted the same question in Math.StackExchange and got a really interesting answer on how to do this recursively; I have no experience on using matrixes in php neither with recursive calls. Could you please give me a hint on how to code in PHP what he said? https://math.stackexchange.com/questions/92942/applying-a

Tail-Recursive Power Function in Scheme

时间秒杀一切 提交于 2019-12-24 00:57:55
问题 I am having trouble writing a tail-recursive power function in scheme. I want to write the function using a helper function. I know that I need to have a parameter to hold an accumulated value, but I am stuck after that. My code is as follows. (define (pow-tr a b) (define (pow-tr-h result) (if (= b 0) result pow-tr a (- b 1))(* result a)) pow-tr-h 1) I edited my code, and now it works. It is as follows: (define (pow-tr2 a b) (define (pow-tr2-h a b result) (if (= 0 b) result (pow-tr2-h a (- b

Print n-length combinations from char list using recursion

折月煮酒 提交于 2019-12-24 00:49:00
问题 I have to solve this exercise using recursion. Implement a function in Python which recieves as parameters list of characters and an integer n. The function has to print all the possible combinations in the length of n, where every character can be shown more than one time. It's very mind-blowing for me, all this thinking recursively generally. For example, for this problem, I think already one and a half hour, not knowing what I'm thinking about. I don't know how to start thinking

Add up all elements of compile-time sized array most efficiently

大兔子大兔子 提交于 2019-12-24 00:36:36
问题 I'm trying to efficiently add everything up in an compile-time sized array, using least amount of instructions. Naturally I'm using templates. I created this. template<unsigned int startIndex, unsigned int count> int AddCollapseArray(int theArray[]) { if(count == 1) { return theArray[startIndex]; } else if(count == 2) { return theArray[startIndex] + theArray[startIndex + 1]; } else if(count % 2 == 0) { return AddCollapseArray<startIndex, count / 2>(theArray) + AddCollapseArray<startIndex +