2

I am trying to get a simple function which takes n and prints

If n > 0:
    print((n*'*')+(n*'!'), end=' ')

and trying to get the same solution but recursively. I am a beginner to recursion, and often I get the "higher level of thinking", but I am having trouble understanding the code that must follow.

My base case is that when n is 0 it prints nothing. When n is more than 1 it will print n copies of * + n copies of!

def repeat(n):
    if n <= 0:
        pass
    else:
        repeat(n-1)
        print((n*'*')+(n*'!'), end=' ')

right now it prints n, and then n-1 successively until 0. I have tried breaking it up into two print statements and using more than one recursion .. but it becomes a messy pattern.

I am also not allowed to use loops. This one is driving me insane; I have come up with several solutions to it besides the easy one line statement, but none that use recursion.

2

5 Answers 5

3

It's simpler if you build and return a string and print it outside of the function, like this:

def printPattern(n):
    if n <= 0:
        return ''
    return '*' + printPattern(n-1) + '!'

Or as a one-liner:

def printPattern(n):
    return '*' + printPattern(n-1) + '!' if n > 0 else ''

Either way, this works:

print printPattern(5)
> *****!!!!!
Sign up to request clarification or add additional context in comments.

6 Comments

I think, OP doesn't want this. He said, when n is more than 1, then print n copies of * + n copies of !.
So, for n = 5 :- 5 * and 5 !, then n = 4 : - 4 * and 4!, and so on, till n = 0.
Or did I misunderstood the question? :(
@RohitJain I believe my answer is correct, "n copies of *" plus "n copies of !" means just that, say for n==3: ***!!!
Thank you Oscar! That was it, I was using that technique but not in an iteration before! This was it. Thank you!
|
1

Assume you have a solution for n - 1. Prepend * and append !.

def repeat(n):
    if n > 0:
        print("*", end=" ")
        repeat(n - 1)
        print("!", end=" ")

Comments

1

The following does what you appear to want.

def repeat(n):
  def stars(n):
    return '*'+stars(n-1)+'!' if n > 0 else ''
  print stars(n)

For example, repeat(5) prints *****!!!!! and repeat(8) prints ********!!!!!!!!.

Comments

0

I don't actually know what you're asking... If there's a more efficient or better way of doing this? This would be quite obvious:

def repeat(n):
    if n >= 0:
        print((n*'*')+(n*'!'), end=' ')
        return repeat(n-1)

Comments

0

I would use two strings here, and return a concatenated string of those two strings when n<=0 and use return instead of printing inside function:

def repeat(n, strs1="", strs2=""):     # The Default value of strings is ""

    if n <= 0:
        return strs1 + strs2   # if n<=0 then concatenate the two strings and return them
    else:
        strs1 += "*"          # Add * to strs1
        strs2 += "!"          # Add ! to strs2
        return repeat(n-1, strs1, strs2)  # Pass n-1, as well as the two strings to a recursive call

print(repeat(5))
print(repeat(3))

Output:

*****!!!!!
***!!!

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.