Home > Blockchain >  Can someone write the code for n = 19 rows and get this following figure? For Python
Can someone write the code for n = 19 rows and get this following figure? For Python

Time:03-19

Write a function that accepts a number n as an input, and it returns n rows that look like the following pattern. Run your function for n = 19 (the output below is for n=19).

n = int(input("enter number of rows:"))
for i in range(1, n 1):
    for j in range(1, n-i 1):
       print(end=' ')
    for j in range(i,0, -1):
        print('' str(j),end='')
    for j in range(2,i 1):
        print(str(j) '_',end='')
    print()

Ouput

                  1
                 212_
                3212_3_
               43212_3_4_
              543212_3_4_5_
             6543212_3_4_5_6_
            76543212_3_4_5_6_7_
           876543212_3_4_5_6_7_8_
          9876543212_3_4_5_6_7_8_9_
         109876543212_3_4_5_6_7_8_9_10_
        11109876543212_3_4_5_6_7_8_9_10_11_
       1211109876543212_3_4_5_6_7_8_9_10_11_12_
      131211109876543212_3_4_5_6_7_8_9_10_11_12_13_
     14131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_
    1514131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_15_
   161514131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_15_16_
  17161514131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_
 1817161514131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_
191817161514131211109876543212_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19_

enter image description here

CodePudding user response:

Here's a solution that works well. Since this looks like homework, I'll just give the algorithm.

For each row create a list of row * 2   1 size and fill with underscores
  Ex: row 0 -> [_], row 1 -> [___], row 2 -> [_____], etc
Find the middle of row and put 1 at the index
Then put 2 and indexes middle-2 and middle   2
Put 3 and indexes middle-4 and middle 4
Repeat to finish row
   Ex: [_________] ->
       [____1____] ->
       [__2_1_2__] ->
       [3_2_1_2_3]
Print spaces - 1st row gets n spaces. 2nd row gets n-1 spaces, etc
Print the list
Repeat for next row

A slight improvement to this would be to just build the last line and then print slices of the last line in a loop. Starting from the middle and widening out.

CodePudding user response:

This will work and will give you a good answer on your assignment.

n = int(input("enter number of rows:")) 
r = 0
for i in range(1, int((n 1)/2 1)):
    r  = 1
    for j in range(1, n-i 1):
        print(end='  ') 
    for kk in range(i,0, -1):
        if i==1:
            print(' ' str(i))
        else:
            if kk==i:
                print(end= ' ')
            if kk==1:
                print(str(kk), end='')
            else:
                print(str(kk) '_', end='')
    for ll in range(2,i 1):
        if i>1:
            print('_' str(ll), end='')
    if i>1:
        print()
    r  = 1
    if r > n:
        break
    for jj in range(1, n-i 1):
        print(end='  ')
    for k in range(i,0, -1):
        if i==1:
            print('_' str(i) '_', end='')
        else:
            print('_' str(k), end='')
    for l in range(2,i 1):
        if l==2:
            print('_' str(l) '_',end='') 
        else:
            print(str(l) '_',end='') 
    print()

Result:

                                 1
                                _1_
                               2_1_2
                              _2_1_2_
                             3_2_1_2_3
                            _3_2_1_2_3_
                           4_3_2_1_2_3_4
                          _4_3_2_1_2_3_4_
                         5_4_3_2_1_2_3_4_5
                        _5_4_3_2_1_2_3_4_5_
                       6_5_4_3_2_1_2_3_4_5_6
                      _6_5_4_3_2_1_2_3_4_5_6_
                     7_6_5_4_3_2_1_2_3_4_5_6_7
                    _7_6_5_4_3_2_1_2_3_4_5_6_7_
                   8_7_6_5_4_3_2_1_2_3_4_5_6_7_8
                  _8_7_6_5_4_3_2_1_2_3_4_5_6_7_8_
                 9_8_7_6_5_4_3_2_1_2_3_4_5_6_7_8_9
                _9_8_7_6_5_4_3_2_1_2_3_4_5_6_7_8_9_
               10_9_8_7_6_5_4_3_2_1_2_3_4_5_6_7_8_9_10
  • Related