vendredi 5 novembre 2021

How to print a pattern of a square wave of asterisks that is 3 units long on each side with input being the number of asterisks?

I'm a beginner to programming and started off with python.

I need to print the following pattern:

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

This just looks like a square wave made of asterisks. The input is the number of asterisks. So the above example would have an input of 31.

Similarly, an input of 13 would have an output that looks like:

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

I came up with the following code but it fails for most odd inputs and I can't figure out where I'm going wrong and honestly feel frustrated after trying for so long.

n=11
count = 1
if n%4 == 0:
    add = 1
else: 
    add = 2
for i in range(1,4):
    for j in range(1,2*(n//4)+add):
        if(i==2 and (i+j)%2==0) or ((i+j-1)%4 == 0 and i!=2):
            print("-", end="")
        elif count<=n and j<n//2:
            count +=1
            print("*", end="")
        else:
            break
    print() 

P.S: I've used a hyphen instead of space for separation because it was better discernable on my terminal.

Aucun commentaire:

Enregistrer un commentaire