You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more
Hi Hive mind. My daughter has stumped me, I have no idea what is going on with this Algorithms worksheet. Please explain it to me in Wally plain language.
[url= https://s14.postimg.org/xb0m5atq9/algorithms.jp g" target="_blank">https://s14.postimg.org/xb0m5atq9/algorithms.jp g"/> [/img][/url]
thanks.
Well P1(1) is easy 🙂
Just a "*" on a line on the blackboard.
For values other than 1 then the function recursively calls itself. You need to follow the call path through.
recursion ftw 8)
a)
*
b)
*
**
*
c)
*
**
*
***
*
**
*
d)
*
**
*
***
*
**
*
****
*
**
*
***
*
**
*
Oddly my daughter has the correct answers, but neither of us can work out how we got there. Please explain "For values other than 1 then the function recursively calls itself. You need to follow the call path through."
I am looking forward to the enlightenment.
Many thanks.
Sorry, that wasn't very helpful - If you are still struggling, just try following the code as explicit instructions line by line, writing the output as you go.
Remember that once you've done (a), you already know the output for P1(1), and so on, so you don't necessarily need to work it all out again.
Hopefully my answer will help you to trace it through.
In the else bit, the algorithm is calling itself (recursion as mentioned above) but subtracting 1 from N. Don't forget that the code continues to run after the recursive call is complete.
"For values other than 1 then the function recursively calls itself. You need to follow the call path through."
'recursion' is the function calling itself, so 'recursively calls itself' is just confusing you.
What that is telling you is that when you call it with (1), it just does something then stops. When you call it with something other than (1), it will go to the other half of the 'if' statement and work out the answer by calling itself.
Trace it through until you get to a recursive call - like P1(n-1); write down your value of 'n', go back to the start and run through with the new value of n. When you have finished, you will return to where you were before, and use your previous value of n.
This is called pushing and popping the call stack, it is how most languages manage control flow.
You need to understand recursion.
Here are some examples: https://stackoverflow.com/questions/25676961/understanding-how-recursive-functions-work
Who writes code like that? I mean seriously?
Have we gone back to APL? Or perhaps [url= https://en.wikipedia.org/wiki/Befunge ]Befunge[/url] ?
EDIT: Anyhow, the question is "could you write how the blackboard would look after the following statments".
To which the answer is clearly "No" unless you have the ability to recreate the look of a blackboard, complete with chalk powder, scratched out stuff from yesterday's class, and bits of snot from the grotty kid at the back of the class.
Ok, we are still struggling after everyone's very kind efforts.
Why does P1(3) start with a * ?
Why does P1(3) start with a * ?
Because the first thing that P1(3) does is call P1(2)
and the first thing P1(2) does is call P1(1)
and P1(1) is just *
I think you get that P1(1) is just '*'
P1(2) is then:
P1(1)
**
P1(1)
and because we already know P1(1) is * then the whole thing becomes
* <-This * is P1(1)
**
* <- and this * is P1(1)
Then do the same for P1(3). First time through it is
P1(2)
***
P1(2)
But we just calculated P1(2). So replacing that answer where we have P1(2) above you get
* <-The next three lines are just P1(2)
**
*
<-This is the part of P1(3)
* <- and the next three lines are just P1(2) again
**
*
Then do the same for P1(3)
Look at the logic - what happens when n != 1? It calls itself again, ie.
P1(3)
Calls P1(2)
Calls P1(1)
write *
.
.
.
and of course for fun, just Google 'recursion' 🙂
holly moses we get it!!! WHooppy whoop whoop!
Great explanation Leffeboy. Enjoy a Wheatbeer on me.
Does your email in profile take paypal gift?
You need to understand recursion.
If you want to understand recursion, Google it. (Seriously, do it.)
Unfortunately not but I'll crack one open in celebration anyway 🙂Does your email in profile take paypal gift?
and of course for fun, just Google 'recursion'
Gods damn you.
🙂
also, see [url= http://singletrackmag.com/forum/topic/homework-help-computer-science-algorithms ]this thread[/url]
Who writes code like that? I mean seriously?
You’re not a computer scientist unless you understand recursion.
And you’re not a software engineer unless you understand why using recursion is almost always a bad idea.
also, see this thread
🙂
And you’re not a software engineer unless you understand why using recursion is almost always a bad idea.
Welcome to the world of infinite loops...many hours spent trying to debug/fix awful code, oh the joy!
also, see this thread
I saw that coming, but well played nonetheless.
you’re not a software engineer unless you understand why using recursion is almost always a bad idea.
you’re not a software engineer unless you understand why using recursion is almost always a bad idea, and choose to do it anyway. (-:
Welcome to the world of infinite loops
Is it just me that thinks that sounds like it should be an Iron Maiden album?
Recursion is great, in the right place.
For tree traversal it is hard to beat, for safety of life systems, not so much.
Is it just me that thinks that sounds like it should be an Iron Maiden album?
\m/
Ah Infinite Dreams - great track.