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:
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
Pattern | Line Number | Number of Spaces | |||
---|---|---|---|---|---|
* | First line | 6 Double spaces | 1st star | ||
* * | Second line | 5 Double spaces | 1st star-set('* ') | 2nd Star | |
* * | Third line | 4 Double spaces | 1st star-set('* ') | 1 Hollow spaces | 2nd star |
* * | Fourth line | 3 Double spaces | 1st star-set('* ') | 2 Hollow spaces | 2nd star |
* * | Fifth line | 2 Double spaces | 1st star-set('* ') | 3 Hollow spaces | 2nd star |
* * | Sixth line | 1 Double spaces | 1st star-set('* ') | 4 Hollow spaces | 2nd star |
* * * * * * * | Seventh line | 0 Double spaces | 7 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.
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
Nvalue.
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
ivalue from theNvalue.
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
ivalue from theNvalue.
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
ivalue from theNvalue.
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
ivalue from theNvalue.
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
ivalue from theNvalue.
Output is
Part - 3 (Seventh line):
i = 6
Output is