Approach for Hollow pattern

 Approach for Hollow pattern problem

In this cheat sheet, let's see how to print the Hollow right-angled triangle pattern

Below is the pattern of the Hollow right-angled triangle

*
* *
* *
* *
* *
* *
* * * * * * *

Now, let's see how to print the 7x7 hollow right-angled triangle pattern.

Logical approach:

Note

In the above image, each empty box contains 2 spaces (double space '  '), and each star is followed by a space ('* ').

  • While writing the code,
    • you need to print * as '* '(star followed by space).
    • you need to print 2 spaces('  ') in place of a single space.

The above image is the 7x7 pattern of the hollow right-angled triangle

  • The first row has 6 spaces and has one star.
  • The second row has 5 spaces and the first star and the second star.
  • The third, fourth, fifth, and sixth rows have decreasing spaces, followed by the first star with increasing hollow spaces, then the second star.
  • The last row has 7 stars.

Here in this problem

  • space is denoted as the '  ' ( Double Spaces)

  • star set denoted as the '* ' (Star is followed by space)

Let's now discuss line by line

PatternLine NumberNumber of Spaces
      *First line6 Double spaces1st star
     * *Second line5 Double spaces1st star-set('* ')2nd Star
    *     *Third line4 Double spaces1st star-set('* ')1 Hollow spaces2nd star
    *        *Fourth line3 Double spaces1st star-set('* ')2 Hollow spaces2nd star
  *             *Fifth line2 Double spaces1st star-set('* ')3 Hollow spaces2nd star
 *                  *Sixth line1 Double spaces1st star-set('* ')4 Hollow spaces2nd star
*  *  *  *  *  *  *Seventh line0 Double spaces7 star-sets('* ')

Now, let's see how to print the above pattern using a python program.

  • We can divide the above pattern logic into 3 parts
    • Part 1: we have spaces and star('*') in first line
    • Part 2: we have spaces, star, hollow spaces, star (middle lines)
    • Part 3: we have star set('* ') in the last line

Code:

  • We can use the For loop and if-elif-else statement to execute the particular part.
N = 7
for i in range(0, N):
if i == 0: # part- 1 logic
print(' '* (N-1) + '*')
elif i == N - 1: # part- 3 logic
print('* ' * N)
else: # part- 2 logic
left_spaces = ' ' * (N -i-1)
hollow_spaces = (' ' * ( i - 1))
print(left_spaces + '* ' + hollow_spaces + '*')
PYTHON
Collapse

Now let's see the explanation of the for loop execution, step by step:

Part - 1 (first line):

i = 0 The first row has 6 spaces followed by one star. In order to get 6 no. of spaces, we must subtract 1 from the

N
value.

spaces = ' ' * (N-1) = 6 // Here N value is 7

Output is

*

Part - 2 (logic to print the Second line to Sixth line):

i = 1 In the second row, we have 5 spaces followed by the first star and hallow spaces followed by the second star. In order to get 5 no.of spaces, we must subtract 1 and

i
value from the
N
value.

spaces = ' ' * (N-i-1) = 5
hollow_spaces = (' ' * ( i - 1)) = 0

Output is

*
* *

i = 2 In the third row, we have 4 spaces followed by the first star and hallow spaces followed by the second star. In order to get 4 no.of spaces, we must subtract 1 and

i
value from the
N
value.

spaces = ' ' * (N-i-1) = 4
hollow_spaces = (' ' * ( i - 1)) = 1

Output is

*
* *
* *

i = 3 In the fourth row, we have 3 spaces followed by the first star and hallow spaces followed by the second star. In order to get 3 no.of spaces, we must subtract 1 and

i
value from the
N
value.

spaces = ' ' * (N-i-1) = 3
hollow_spaces = (' ' * ( i - 1)) = 2

Output is

*
* *
* *
* *

i = 4 In the fifth row, we have 2 spaces followed by the first star and hallow spaces followed by the second star. In order to get 2 no.of spaces, we must subtract 1 and

i
value from the
N
value.

spaces = ' ' * (N-i-1) = 2
hollow_spaces = (' ' * ( i - 1)) = 3

Output is

*
* *
* *
* *
* *

i = 5 In the sixth row, we have 1 space followed by the first star and hallow spaces followed by the second star. In order to get 1 space, we must subtract 1 and

i
value from the
N
value.

spaces = ' ' * (N-i-1) = 1
hollow_spaces = (' ' * ( i - 1)) = 4

Output is

*
* *
* *
* *
* *
* *

Part - 3 (Seventh line):

i = 6

stars = '* ' * N

Output is

*
* *
* *
* *
* *
* *
* * * * * * *

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form