educative.io

Does it matter whether my code is very different from solution?

My code passed the test, but is very different from the solution. For instance, does it matter that the author used a string literal for “########” and I used a for-loop to iterate eight individual char ‘#’'s? Also, the author declared and initialized ‘Cols’ but it appears it is never used in the program. Just want to make sure I’m not missing something. Thank you! :nerd_face:

import std.stdio;

void printParallelogram() 
{
	int Rows = 5;
	int Cols = 8;

	for(int i = 0; i < Rows; ++i) 
	{
		for(int j = 0; j <= i; ++j) 
		{
			write(' ');
		}

		for(int k = 0; k < Cols; ++k) 
		{
			write('#');
		}

		writeln;		
	}
}

Also, the solution presented in the ’ Challenge: Print a Parallelogram Pattern’ section is slightly different than the one presented in the ‘Solution: Print a Parallelogram Pattern’ section.

One uses the not equal operator !=, and the other uses the greater than operator >

(int i = 0; i != line; ++i) vs (int i = 0; i < line; ++i)

The effect appears to be the same.

Your code uses two for loops in the main for loop. Although your solution is correct and should pass the test case, your 2nd loop is running for 8 times at every iteration, which is not an efficient solution. And for your second question, both i != line and i<line are the same. In both cases, the loop will break at the same iteration.

Thank you Faizan for your input. Since you stated that the ‘2nd loop is running for 8 times at every iteration, which is not an efficient solution.", it begs the question why the author declared and initialized "int cols = 8’, but not use it?

As an aside, I hope someone is noting the errata that I’ve discovered in this lesson.