iamcoder
iamcoder

Reputation: 95

Reverse Pattern of Digits from 10. (pattern problems)

This is the pattern that I want to print.

1
3 2
6 5 4
10 9 8 7

Now I have a solution but I don't know how exactly it is working.

solution:

length = 6
start = 1
stop = 2
currentNumber = stop
for row in range(stop, length):
    for col in range(start, stop):
        currentNumber -= 1
        print(currentNumber, end=" ")
    print(" ")
    start = stop
    stop += row
    currentNumber = stop

This is How I think it is working:

# for row in range(2, 6): 2, 3, 4, 5,
# for col in range(1, 2):
#  currentnum = 2-1 = 1
#  print( 1 )
#  start = stop (start = 2)
#  stop += 2 (row = 2,3,4,5) so now stop = 2 + 2 = 4
# currentNumber = 4

# for row in range(4, 6): 4,5
# for col in range(2, 4): 2, 3 means 2 time will loop run
#  currentnum = 4-1 = 3,
#  currentnum = 3-2 = 2
#  print( 3, 2 )
#  start = stop (start = 4)
#  stop += 3 (stro = 4+3 = 7)
#  currentnum = 7

                    **PROBLEM** 

# for row in range(7, 6):    
# for col in range(4, 7): 5,6,7 means 3 time will loop run
#  currentnum = 7-1 = 6,
#  currentnum = 7-2 = 5,
#  currentnum = 7-3 = 4,
#  print( 6, 5, 4)
#  start = stop (start = 7)
#  stop += 3 (stro = 7+3 = 11)
#  currentnum = 11

Here is the problem (talking from the #commented calculations):

for i in range(11, 6):
    for j in range(4, 7):
        print("h")
    print("k")
print("j")

This For loop will not execute because of (11, 6).

That's where My calculations went wrong and know I have NO clue again how this solution is working. Anyone have better solution or explanation for the current solution will be appreciated.

just want to know how the code is working

Thankyou.

Upvotes: 1

Views: 604

Answers (1)

Rolv Apneseth
Rolv Apneseth

Reputation: 2118

I think the problem in your understanding was re-writing the first for loop every time stop was changed. This shouldn't be done, however, as the for loop only calculates at the start so changing the variable stop shouldn't have an effect on a loop once it's already running, if that makes sense. I corrected your 'understanding' section here:

#### for loop does not change
# for row in range(2, 6): 2, 3, 4, 5,

### for loop 2 ###
#     for col in range(1, 2):
#         currentNumber -= 1  # 2-1 = 1
#         print(1)
#      start = stop  # (start = 2)
#      stop += 2  # (row currently = 2) so now stop = 2 + 2 = 4
#      currentNumber = 4

### for loop 3 ###
#     for col in range(2, 4): 2, 3 means 2 time will loop run
#         currentnum = 4-1 = 3,
#         currentnum = 3-2 = 2
#         print(3, 2)
#     start = stop  # (start = 4)
#     stop += 3  # (row currently = 3) so now stop = 4 + 3 = 7
#     currentnum = 7

### for loop 4 ###
#     for col in range(4, 7):  # 4, 5, 6 i.e. loop 3 times
#         currentnum = 7-1 = 6,
#         currentnum = 6-1 = 5
#         currentnum = 5 - 1 = 4
#         print(6, 5, 4)
#     start = stop  # (start = 7)
#     stop += 4  # (row currently = 4) so now stop = 7 + 4 = 11
#     currentnum = 11

### for loop 5 ###
#     for col in range(7, 11):  # 7, 8, 9, 10 i.e. loop 4 times
#         currentnum = 11 - 1 = 10
#         currentnum = 10 - 1 = 9
#         currentnum = 9 - 1 = 8
#         currentnum = 8 - 1 = 7
#         print(10, 9, 8, 7)
#     start = stop  # (start = 11)
#     stop += 5  # (row currently = 5) so now stop = 11 + 5 = 16
#     currentnum = 16

Hope this makes sense but feel free to ask for clarification. Note that the last few lines don't matter since it is the last for loop.

Upvotes: 1

Related Questions