recursion

Implementing Recursive Proxy pattern in C++11

荒凉一梦 提交于 2020-01-05 12:30:42
问题 Suppose we have some Foo object that allows: cout << myFoo[3]; myFoo[5] = "bar"; This calls for a proxy design-pattern (detailed by Scott Meyers here) But now let us suppose every myFoo[i] is also a Foo instance. myFoo[7] = Foo{...}; myFoo[5] = "bar"; // Foo has a Foo(std::string) non-explicit constructor I'm close to having an implementation, but I can't get rid of one final pesky "forward declaration/ incomplete type" error. Firstly, let's get the easy one out of the way: // x =

Understanding a recursive solution to print out an array in reversed order

一笑奈何 提交于 2020-01-05 11:47:31
问题 I'm still new to C++ and as of right now learning recursion. I wanted to display the elements in reversed order without starting the index of the array at the end using recursion. Obviously it would be very easy to do using loops, but using recursion is a different matter. I found this solution to the problem online, but can't understand how exactly its able to print out each value. void recArrayBackPrint(int array[],int size) { if (size > 0) { recArrayBackPrint(array+1,size-1); cout << array

Find value of scrabble word

只谈情不闲聊 提交于 2020-01-05 08:35:30
问题 I have little experience in any higher level language. Fiddled with basic and dos-batch when I was a kid. I'm trying to find the point value of a word in scrabble. This kind of recursive structure seems slow and inefficient. What would be a better way to address this problem in terms of program structure/concept? value_list = {'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1,

Why does this function example from Eloquent JavaScript return the value 24?

最后都变了- 提交于 2020-01-05 08:35:22
问题 I am new to JavaScript and am trying to understand an example from the book. When I step through this example, I expect the value of fac(4) to be 12. That would be the result of (4 - 1) * 4. However, when I run the code from the online book (near bottom of page) at http://eloquentjavascript.net/00_intro.html, I get the value 24. What am I missing here? function fac(n) { if (n == 0) return 1; else return fac(n - 1) * n; } console.log(fac(4)); I have tried to find an answer through Twitter and

Calling a function recursively for user input

本秂侑毒 提交于 2020-01-05 08:26:14
问题 I'm trying to make a rock-paper-scissors game, and am trying to verify the input. def player1(): x = (raw_input("please select: Rock(r)/Paper(p)/Scissors(s): ")).lower() if x == 'r' or x == 'p' or x == 's' or x == 'rock' or x == 'paper' or x == 'scissors': return x[0] else: print "Error - wrong input!" player1() #I know I can run a While loop, but I need to run it this way. print(player1()) If I enter the right input on the first try, everything works fine. But if I enter wrong input on first

PHP recursion: How to create a recursion in php

六月ゝ 毕业季﹏ 提交于 2020-01-05 08:18:11
问题 I have a little problem with a recursion in php. I have read many articles but the solution doesn't come. I have this array: [59] => Array ( [ID] => REL000000 [Name] => RELIGIONE / Generale [Description] => [IdParent] => ) [799] => Array ( [ID] => REL102000 [Name] => RELIGIONE / Teologia [Description] => [IdParent] => REL000000 ) [800] => Array ( [ID] => REL068000 [Name] => RELIGIONE / Teosofia [Description] => [IdParent] => REL000000 ) [801] => Array ( [ID] => REL103000 [Name] => RELIGIONE /

Explanation of List Comprehensions

北城以北 提交于 2020-01-05 08:10:26
问题 I'm attempting to learn about dynamic programming and recursion for Python 3.x. The code below is a simple example from my textbook: def recMC(coinValueList, change): minCoins = change if change in coinValueList: return 1 else: for i in [c for c in coinValueList if c <= change]: numCoins = 1 + recMC(coinValueList, change-i) if numCoins < minCoins: minCoins = numCoins return minCoins print(recMC([1,5,10,25], 63)) I'm really struggling to understand line 6 here, the line comprehension. I'm

Looping through Active Directory to get managers and direct reports

筅森魡賤 提交于 2020-01-05 08:03:29
问题 Go easy on me, this is my first question ;) I've spent a lot of time looking, but I haven't found what I'm looking for. I've got an intranet based reporting tool (VB.Net + ASP.Net Integrated Windows Authentication) that looks up users and managers from a SQL Server 2005 table to roll up the reporting to manager level. This table is currently manually maintained and I've been asked to make it more dynamic as it is going to be up-scaled for far more users. Therefore I'm looking to link in with

Representing an amount of money with specific bills

笑着哭i 提交于 2020-01-05 07:59:53
问题 I want to write a function in Racket which takes an amount of money and a list of specific bill-values, and then returns a list with the amount of bills used of every type to make the given amount in total. For example (calc 415 (list 100 10 5 2 1)) should return '(4 1 1 0 0) . I tried it this way but this doesn't work :/ I think I haven't fully understood what you can / can't do with set! in Racket, to be honest. (define (calc n xs) (cond ((null? xs) (list)) ((not (pair? xs)) (define y n)

recursion Think Python 2 exercise 5.5

孤街浪徒 提交于 2020-01-05 07:43:39
问题 This question regarding exercise 5.5 from Think Python 2 has been asked and answered already, but I still don't understand how this function works. Here's the function in question: def draw(t, length, n): if n == 0: return angle = 50 t.fd(length*n) t.lt(angle) draw(t, length, n-1) t.rt(2*angle) draw(t, length, n-1) t.lt(angle) t.bk(length*n) I see a recursive loop here: def draw(t, length, n): if n == 0: return angle = 50 t.fd(length*n) t.lt(angle) draw(t, length, n-1) After the function