It is possible to create the star patterns from the loops chapter without a single for
or while
loop by using recursive functions (=functions that call themselves).
Let me see your best attempts!
1 Like
def star_pattern(maxn, n = 0, growing = True):
print('*'*n)
if 0 == n or n == maxn:
star_pattern(maxn, n-1 if n == maxn else n+1, not growing)
elif growing:
star_pattern(maxn, n+1)
else:
star_pattern(maxn, n-1, False)
This will show a simple pattern forever until you block the execution, try something like star_pattern(10)
3 Likes
Liked the challenge to be honest
Not sure if it’s the optimal way, tried to do it without any optional arguments, but no time at the moment to tinker with it.
5 Likes
Here is my solution:
def star_pattern(iteration=0):
if iteration >= 5: # maximum amount of stars is 5
return
else:
print("*" * (iteration+1))
star_pattern(iteration+1)
if not iteration == 4:
print("*" * (iteration+1))
star_pattern() # the initial function call
The resulting pattern looks like this:
*
**
***
****
*****
****
***
**
*
4 Likes
Wow, you solved all the patterns! Great job!
1 Like
Hi! Ispired by Sebgolos the rhombus stars pattern
:grin:
4 Likes