2

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.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • 6
    That's a nice problem that I think you can solve it with a little bit more effort. Just continue trying. – nbro Feb 08 '15 at 12:56
  • 1
    The second triangle is separated from the first with *spaces*. Think about how many spaces you'd need to print on a line after the first triangle stars. – Martijn Pieters Feb 08 '15 at 12:58
  • Seems we can start a library of code to print all those shapes of asterisks used as beginner's exercise: [Pyramid](http://stackoverflow.com/questions/33179423/upside-down-pyramid-py), [M](http://stackoverflow.com/questions/28394149/draw-an-m-shaped-pattern-with-nested-loops), [Triangels](http://stackoverflow.com/questions/26352412/python-print-a-triangular-pattern-of-asterisks), [Diamond](http://stackoverflow.com/questions/31364162/print-shape-in-python), [Hollow square](http://stackoverflow.com/questions/16108446/drawing-a-hollow-asterisk-square) – cfi Oct 16 '15 at 21:17

4 Answers4

2

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:

 * . . . . . *
 * * . . . * *
 * * * . * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
eduffy
  • 39,140
  • 13
  • 95
  • 92
0

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
*       *
**     **
***   ***
**** ****
*********
*********
*********
*********
*********
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0

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$ 
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
  • (Disclaiming that I wasn't the one to down-vote), this isn't the most elegant of ways. eduffy's answer is definitely the more helpful answer (in my point of view). Since this is an exercise question, let the poster tackle the coding part themselves, the logic of how to approach the question is more important than how to print a number of spaces per row. – Eran Feb 08 '15 at 13:40
0

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.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152