Can anyone tell me how I will print a right angled triangle star pattern recursively? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can anyone tell me how I will print a right angled triangle star pattern recursively?

https://code.sololearn.com/cn1FbuKUsNpV/?ref=app

23rd May 2021, 7:06 PM
Atul [Inactive]
44 Answers
+ 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
23rd May 2021, 8:20 PM
visph
visph - avatar
+ 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.
25th May 2021, 11:40 AM
Oma Falk
Oma Falk - avatar
+ 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..
24th May 2021, 12:01 PM
Anon Fox
Anon Fox - avatar
+ 3
For practice Anon Fox
24th May 2021, 12:28 PM
Atul [Inactive]
+ 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 }
25th May 2021, 10:58 AM
Oma Falk
Oma Falk - avatar
25th May 2021, 11:22 AM
NEZ
NEZ - avatar
+ 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.
25th May 2021, 1:14 PM
Anon Fox
Anon Fox - avatar
+ 3
Anon Fox Frogged visph NEZ thank you for your answers
25th May 2021, 1:20 PM
Atul [Inactive]
+ 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.
26th May 2021, 9:28 AM
Tibor Santa
Tibor Santa - avatar
+ 2
* ** *** **** ***** //Required pattern
23rd May 2021, 7:35 PM
Atul [Inactive]
+ 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)
23rd May 2021, 8:28 PM
visph
visph - avatar
+ 2
NEZ thanks. Don't know who is downvoting? Many a times it happens
25th May 2021, 11:26 AM
Atul [Inactive]
+ 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.
25th May 2021, 11:28 AM
Oma Falk
Oma Falk - avatar
+ 2
Frogged according to you what should be used the most?
25th May 2021, 11:33 AM
Atul [Inactive]
+ 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
25th May 2021, 12:56 PM
visph
visph - avatar
+ 2
visph in competitive programming what is being used the most loops or recursion?
25th May 2021, 1:04 PM
Atul [Inactive]
+ 2
Tibor Santa thanks for the explanation
26th May 2021, 9:36 AM
Atul [Inactive]
+ 2
visph me too
28th May 2021, 2:57 PM
Atul [Inactive]
+ 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
3rd Jun 2021, 6:02 AM
Tibor Santa
Tibor Santa - avatar
+ 1
visph what more changes I want to bring in my program?
23rd May 2021, 8:22 PM
Atul [Inactive]