Draw an M-shaped pattern with nested loops

前端 未结 4 1140
慢半拍i
慢半拍i 2020-12-12 02:11

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 in

相关标签:
4条回答
  • 2020-12-12 02:34

    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
    *       *
    **     **
    ***   ***
    **** ****
    *********
    *********
    *********
    *********
    *********
    
    0 讨论(0)
  • 2020-12-12 02:38

    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:

     * . . . . . *
     * * . . . * *
     * * * . * * *
     * * * * * * *
     * * * * * * *
     * * * * * * *
     * * * * * * *
    
    0 讨论(0)
  • 2020-12-12 02:53

    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.

    0 讨论(0)
  • 2020-12-12 02:56

    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$ 
    
    0 讨论(0)
提交回复
热议问题