BLKWDO - Editorial

PROBLEM LINK:

Practice

Author: Ashraf Khan
Tester: Ashraf Khan
Editorialist: Ashraf Khan

DIFFICULTY:

EASY, MEDIUM

PREREQUISITES:

Basic programming knowledge, Loops, Understanding patterns

PROBLEM:

Chef is inspired from the Batman′s Signal and now wish to make a similar Signal for Black Widow.

For this, he picks a number N>2 and prints a pattern of (N*2)−1 lines. For example, for N=3 the pattern will have 5 lines comprising of only ‘#’ symbols separated by single white−space and the complete pattern will look like:

# # # # #     
  # # #       
    #         
  # # #       
# # # # #     

Note: There’s a single space after every ‘#’ symbol.

Help Chef to print the Signal for Black Widow.

EXPLANATION:

For a given number N, you are supposed to print (N*2)-1 lines of the pattern.
Multiple loops will be needed to achieve this kind of pattern.
First take an input N.
Then initialize a variable to zero (say i = 0).
Now, start our first major loop which will go from i to N-1 (i.e. i < N). Inside this loop, print " " (two white-spaces without double-quotes) i-times, then print "# " (‘#’ and ’ ’ without double-quotes) ((N*2)-1-(2*i))-times, then print a next-line element (like \n) and then increment i by 1. Then finally end this loop.
After coming out of this first loop, decrement i by 1.
Now, start the second major loop which will go from i to 1 (i.e. i > 0). Inside this loop, first decrement i by 1 and then print " " (two white-spaces without double-quotes) i-times, then print "# " (‘#’ and ’ ’ without double-quotes) ((N*2)-1-(2*i))-times, then print a next-line element (like \n). Now end this loop.
Print a next-line element (like \n) one last time after the second major loop ends.

Here, the first major loop is used to print first half of the pattern (i.e. first N lines) and the second major loop is used to print the rest of the pattern.

SOLUTIONS:

Setter's Solution
N = int(input())
i = 0
while(i<N):
    print('  '*i, end="")
    print('# '*((N*2)-1-(2*i)))
    i += 1
i -= 1
while(i>0):
    i -= 1
    print('  '*i, end="")
    print('# '*((N*2)-1-(2*i)))
print()
Tester's Solution
#include <iostream>
using namespace std;

int main() {
	int N;
	cin >> N;
	
	int i = 0;
	while(i < N) {
	    for(int j = 0; j < i; j++) {
	        cout << "  ";
	    }
	    for(int j = 0; j < ((N*2)-1-(2*i)); j++) {
	        cout << "# ";
	    }
	    cout << endl;
	    i++;
	}
	i--;
	while(i > 0) {
	    i--;
	    for(int j = 0; j < i; j++) {
	        cout << "  ";
	    }
	    for(int j = 0; j < ((N*2)-1-(2*i)); j++) {
	        cout << "# ";
	    }
	    cout << endl;
	}
	cout << endl;
	return 0;
}

Editorialist's Solution
N = int(input())
i = 0
while(i<N):
    print('  '*i, end="")
    print('# '*((N*2)-1-(2*i)))
    i += 1
i -= 1
while(i>0):
    i -= 1
    print('  '*i, end="")
    print('# '*((N*2)-1-(2*i)))
print()