Closed ghost closed 1 year ago
(For the future please assign labels & yourself as assignee and name the pr according to the readme.)
Should I also use the branch name in the commit message in the future?
Why does n-1 seem to be such a crucial thing? I understand why it has to be in the recursive function but why also in the loop?
The recursive function needs a array and the length of the respective array. (Since the newA is one shorter because of one higher level) The n-1 in the loop and the recursive call have the same reason. Its tricky to grasp but it helps going through the call stack in your head from main onwards.
Why does this work, i.e., why do I have to put the recursive call between for-loops?
Because you want to print the pyramid from top to bottom. By putting the recursive call between the computer first goes through the recursive calls and computes all the new arrays until the recursive call is terminated by n == 0, then the computer executes the code after the recursive call (print loop) backwards (call stack, bottom -> up).
I tried to pull off this code with only the original array but that didn't work, is it because the original array would contain other values?
Yes. Its impossible with the original array only, unless you print the pyramid on its head, because of the printing after the recursive call (the previous array will be overwritten and cannot be printed in its original form).
Should I also use the branch name in the commit message in the future?
For the commit message please see "Contributing" > "Step 5" in the README.md
(Step 1 for branch naming, and pr naming same as commit but we're going to write it in Step 6)
@yuirike i recommend, you watch this https://youtu.be/CFRhGnuXG-4 and the other videos on the channel.
PS: Generally assign us 3 as reviewers, so that the one who has time can look at it. A specific one rarely has time and with multiple, the PR gets reviewed most quickly.
👍 but minor changes
I think I applied all your requested changes but there were things I was unsure about: If the for-loop or the if-conditional only have one statement in their block, I think C can run it without the curly brackets. Though, I still keep them because it looks more structured to me. The unconventional part might me putting them fully around that one statement instead of making new lines. I did this because it is really just one statement and adding new lines feels like a waste.
For example:
if(var == 0)
{return;}
I do see some people do:
if(var == 0){
...
}
I personally prefer:
if(var == 0)
{
...
}
This makes sense to me for longer blocks, it might take more lines but it looks the most structured to me. If it is just one statement, I just completely wrap it up as shown in the first example.
Thanks for the changes. Doing this:
if(var == 0){ ... }
or this:
if(var == 0) { ... }
comes down to personal preference / codestyle guidlines.
I would strongly advice against the braces around code on one line. Its very rarely done in the industry and goes against consistency / readability. But isn't a reason in this case not to approve.
PS: https://softwareengineering.stackexchange.com/questions/16528/single-statement-if-block-braces-or-no
The recursive function needs a array and the length of the respective array. (Since the newA is one shorter because of one higher level) The n-1 in the loop and the recursive call have the same reason. Its tricky to grasp but it helps going through the call stack in your head from main onwards.
Two more questions about Recursion:
Conceptually: Can you think of it as a storage? In this code, the calculation part is done but not printed, it is as if it is stored "somewhere" in the order from earlier, descending order (number of elements). When the termination condition is reached, it is as if it picks the "last stored value" and prints it, hence the full output would be in ascending order (number of elements increase).
Time Complexity: Can you view Recursion like a for loop? Given that the termination condition would be something like n == 0 and there would be somewhere an n-- inside the function. Would the number of conditional statements make a difference in that case (if those were inside the function.)
Conceptually: Can you think of it as a storage? In this code, the calculation part is done but not printed, it is as if it is stored "somewhere" in the order from earlier, descending order (number of elements). When the termination condition is reached, it is as if it picks the "last stored value" and prints it, hence the full output would be in ascending order (number of elements increase).
If i understand correctly, yes.
Time Complexity: Can you view Recursion like a for loop? Given that the termination condition would be something like n == 0 and there would be somewhere an n-- inside the function. Would the number of conditional statements make a difference in that case (if those were inside the function.)
Depends on the complexity of the recursive function. If the recursion is only called once inside itself, yes, else its mostly way more time complex than a simple for loop.
Confusing. I did solve this exercise, was confused about how to print the pyramid correctly, figured it out with the help of a friend but don't really understand why it works, so I'll repeat my questions here and if someone is good at code analysis, please enlighten me.
Why does n-1 seem to be such a crucial thing? I understand why it has to be in the recursive function but why also in the loop?
Why does this work, i.e., why do I have to put the recursive call between for-loops?
I tried to pull off this code with only the original array but that didn't work, is it because the original array would contain other values?