user4542936
user4542936

Reputation:

Draw an M-shaped pattern with nested loops

I just started Python 2.7 very recently, but I'm stuck at this a problem:

Make a program that prints an "M" pattern of asterisks. The user shall input the height of the pattern.

Here's the picture of the problem:

Image of problem description with sample output

h=raw_input("Enter the height of MStar here:")
h=int(h)

for row in range(0,(h-1)/2):
     for column in range(row+1):
         print "*",
    print
for row in range((h-1)/2,h):
    for column in range(h):
        print "^",
    print

It is also suggested that I can do two loops for the pattern because it can be seen as two parts, the upper one with the stars and spaces, and the second part that looks like a rectangle which I've done. I need some help with my code, because I really don't know how I can add the second triangle, I've can only make the first.

Upvotes: 2

Views: 3503

Answers (4)

ivan_pozdeev
ivan_pozdeev

Reputation: 36036

The dumbest approach is to print an increasing number of stars justified left and right until their number reaches N//2 (floor division), the rest is easy.

for n in range(1,N//2+1):
    line = bytearray(' ' * N)
    line[:n] = line[-n:] = '*' * n
    print line

Ha, with this, you can even loop all the way to N instead of N//2 - since assigning to intersecting ranges will do no harm.

Upvotes: 0

Hugh Bothwell
Hugh Bothwell

Reputation: 56654

Sometimes it helps to sketch out a table of the values you need to generate:

h notch_rows  no_notch_rows  row   "*"s  " "s  "*"s

3     1                       1     1     1     1
                     2        1     3
                              2     3

5     2                       1     1     3     1
                              2     2     1     2
                     3        1     5
                              2     5
                              3     5

7     3                       1     1     5     1
                              2     2     3     2
                              3     3     1     3
                     4        1     7
                              2     7
                              3     7
                              4     7

This results in an implementation like

def mstar(h, star_ch="*", space_ch=" "):
    assert h % 2, "h must be odd"

    notch_rows    = h // 2
    no_notch_rows = notch_rows + 1
    rows          = []

    for row in range(1, notch_rows + 1):
        stars  = star_ch  * row
        spaces = space_ch * (h - 2 * row)
        rows.append(stars + spaces + stars)

    for row in range(1, no_notch_rows + 1):
        stars  = star_ch * h
        rows.append(stars)

    return "\n".join(rows)

def main():
    h = int(input("Height of M-Star? "))    # use raw_input for Python 2.x
    print(mstar(h))

if __name__ == "__main__":
    main()

and runs like

Height of M-Star? 5
*   *
** **
*****
*****
*****

Height of M-Star? 9
*       *
**     **
***   ***
**** ****
*********
*********
*********
*********
*********

Upvotes: 0

Vivek Sable
Vivek Sable

Reputation: 10223

Limitation: Enter only old number greater than 3

Logic:

  1. Get input from user by raw_input().
  2. Get number of space count by subtracting 2 from the user input. e.g. (i) for user input is 3 only first line have 1 space, (ii) for input 5--> first line have space 3 and second line have space 1.
  3. Run for loop n time where n is user value.
  4. If space count is greater then 0 then create print_line where add space value according to space count in the middle of string and * at start and end according to for loop count.
  5. If space count is less then 0 then print string to * according to user value.

Code:

no = int (raw_input("Enter a number: "))
space_no = no - 2
print_line = "*"*no
for i in xrange(1,no+1):
    if space_no>0:
        print_line_n = "*"*i+" "*space_no+"*"*i
        space_no -=2
        print print_line_n
    else:
        print print_line

output:

vivek@vivek:~/Desktop/stackoverflow$ python 9.py 
Enter a number: 3
* *
***
***
vivek@vivek:~/Desktop/stackoverflow$ python 9.py 
Enter a number: 5
*   *
** **
*****
*****
*****
vivek@vivek:~/Desktop/stackoverflow$ python 9.py 
Enter a number: 9
*       *
**     **
***   ***
**** ****
*********
*********
*********
*********
*********
vivek@vivek:~/Desktop/stackoverflow$ 

Upvotes: 0

eduffy
eduffy

Reputation: 40222

I imagine this problem as two triangles overlapping each other. You can write two functions that checks whether a coordinate is in a triangle. For example, this code

 for i in range(n):
   for j in range(n):
     if left(i,j):
       print '*',
     else:
       print '.',
   print

gives this output:

 * . . . . . .
 * * . . . . .
 * * * . . . .
 * * * * . . .
 * * * * * . .
 * * * * * * .
 * * * * * * *

Changing left to right gives the mirror image:

 . . . . . . *
 . . . . . * *
 . . . . * * *
 . . . * * * *
 . . * * * * *
 . * * * * * *
 * * * * * * *

Once you've figured out the correct implementation of left and right, just combine the two as left(i,j) or right(i,j) to get the M shape:

 * . . . . . *
 * * . . . * *
 * * * . * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *

Upvotes: 2

Related Questions