+ 2
Can anyone tell me how I will print a right angled triangle star pattern recursively?
44 Antworten
+ 4
first at all, you should better space/indent your code for readability ^^
completly rewritten, here is my solution:
https://code.sololearn.com/cnojSro2LChW/?ref=app
+ 4
Atul you must create your own style of coding.
Sebastian Keßler once mentioned, he believes he can guess the right author if he sees a code (well... for 3 or 4 coder he knows pretty good)
I prefer the the elegance of recursion but would switch to loops if performance plays a role.
Also there are further concepts like dynamical programming.
In Python there is an extra decorator to speed up recursion.
But for sure you should practice both and further concepts until you feel comfortable with them.
+ 3
Atul dude then just replace the for loop with a recursive function but i don't get why would you want to do that..
+ 3
For practice
Anon Fox
+ 3
In general a formal approach for many recursion.
1. When is recursion to stop?
2. Which is the next easier case?
In your case:
1. the easiest case is a row of one star.
So recursion stops at 0 or 1....as u want
2. The next easier case is a row with one star less.
So
Function stars(n){
if n = 0 return
else return stars(n-1)
print n stars
}
+ 3
Atul
This will help you:
https://betterprogramming.pub/when-to-loop-when-to-recurse-b786ad8977de
+ 3
if you are confused on "why or where recursion rocks " search for binary trees, you'll see how the codes become more clear and more simple, like instead of using stacks/queues to implement the functionalities of a binary tree, you can easily use recursion for that.
+ 3
At the most basic level (machine code) everything is translated to loops.
Recursion is a higher level programming tool, that can make your code more concise and expressive. It can also make your code a bit more difficult to comprehend. So there is a trade-off.
Some programming languages have much better support for recursion. One such feature is tail call optimization - this prevents the program from running out of memory because of too deep stack calls. Also, memoization (caching) is often used with recursion to improve the efficiency.
+ 2
*
**
***
****
*****
//Required pattern
+ 2
your code is a mess...
proper indent it, and maybe I will take a deeper look... but you should rather study my code and figure yourself what you're doing wrong in your code ^^
however, what don't you understand? my code or your?
... and about what code want you have the answer to "can you tell me it's dry run" (wich I even don't understand what you mean by that ;P)
+ 2
NEZ thanks.
Don't know who is downvoting? Many a times it happens
+ 2
Hmmm
It is a bit at your taste.
Recursion makes a short readable code.
But the performance is worse normally.
Also there is a maximum depth of recursion.
Pure functional code would not use a loop.
But this is no point in Java, which can have some functional code but is mainly OOP.
+ 2
Frogged according to you what should be used the most?
+ 2
in time I guess that does not make a great difference in this case ^^
however, it is more efficient to use linear versus recursion, as with the second you add some complexity (stack handling for recursive function calls)...
whenever I could do simply without recursion, I don't use it. I only use it if that improve at least readability and/or simplify the logic ;)
in this case, appart for practice purpose, there's no reason to complexify the problem by using recursion ;P
+ 2
visph in competitive programming what is being used the most loops or recursion?
+ 2
Tibor Santa thanks for the explanation
+ 2
visph me too
+ 2
After a few days I got a response email from SoloLearn, they asked for a video that demonstrates the bug. I gave them instructions instead, where to click. But it is still not fixed. So I just saved it with another name as a new code, and now I can link it.
https://code.sololearn.com/c1d91ag6inqb/?ref=app
+ 1
visph what more changes I want to bring in my program?