1

I am working on recursion in python is trying to print asterisk using recursion in the form when an user input is given for example: 3 the program should output:

***
**
*
**
***

I have managed to print the output as followed when the user input is given as 3:

***
**
*

When printPatternRecur is called it prints the output i have managed so far

def printPattern(n):
    # Base case
    if (n < 1):
        return

    print('*', end = " ")
    printPattern(n - 1)

def printPatternRecur(n):
    # Base case
    if (n < 1):
        return 

    printPattern(n)

    print("")
    printPatternRecur(n - 1)

The expected output should call recursively as mentioned at the very beginning. Any suggestions towards a simpler approach to solving this problem using recursion is also appreciated. Also would love to know how to overcome the thinking process while working with recursive problems or any articles anyone found useful understanding the concept of recursion. Thank you!

4 Answers 4

4

You can do this, for example:

>>> def pattern(n):
...     if n == 1:
...         return print("*")
...     print("*" * n)
...     pattern(n - 1)
...     print("*" * n)
...     
>>> pattern(5)
*****
****
***
**
*
**
***
****
*****

You can also use your recursive printing function instead of print("*" * n).

The thinking process can go like this: with every recursive call you're dropping a level deeper, and when you get back from a recursive call, you're rising one level up. You can also see this V-shaped pattern in the output if you turn it 90 degrees counterclockwise: you step deeper and deeper into the recursion, then you hit the base case and then begin ascending till you get out of the very first call.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's a slight variation, for a simpler answer. The "trick" is to know where to call the recursion: in between the print statements. And when to exit: when we reach 1 (or less than 1 if we're going to be careful), but not before printing that last lonely asterisk in the middle.

def printPattern(n):
    print('*' * n)
    if n <= 1: return
    printPattern(n - 1)
    print('*' * n)

It works as expected:

printPattern(5)

*****
****
***
**
*
**
***
****
*****

Comments

0

You can use an outer wrapper function:

def outer(_d):
  def tri(s, d, flag = 0):
    if s == 1:
      return '*'+'\n'+tri(2, d, 1)
    return ('*'*s)+'\n'+tri(s-1 if not flag else s+1, d, flag) if not flag or s != d else '*'*d
  return tri(_d, _d) 

print(outer(3))

Output:

***
**
*
**
***

Comments

0

Recursion follows a “first in, last out” logical format. This means that the first function call pushed onto the stack will execute last. Function calls are pushed onto the stack and then executed one after another. A recursive function has a base case which causes function calls pushed onto the stack to return, or to be popped off the stack.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.