Thursday 18 April 2013

Interview Series - Dazzling Stars


Iterations are vital for performing umpteen numbers of functionality. To make creative designs purely by intelligent looping is challenging.

Here in this setup of an Interview, we’ll see how to loop effortlessly with seamless and “readable” logic to generate a scenic clump of stars!


We start-off by modelling the star’s shape. We begin with the generation of a simple nXn array of stars. After that, we shape the pattern of stars resembling a Right-Angled Triangle!

Next, using the building blocks as stars, we generate variants of a right-pointing arrow. The shape is enhanced by varying the indentations.

As text is naturally left-aligned, it’s easy to generate a right-pointing arrow as its base is towards the left. Now, we venture into generating the mirror image of a right-pointing arrow, i.e., a left-pointing arrow. This is akin to clicking the “Right-Align” button on a left-aligned text in a word processor! Quite simple to say, but a tad complex to execute.

Finally, we wind up by generating a great pattern; a pattern resembling a Diamond! This is generated by the unison of the left-pointing arrow and right-pointing arrow. All the coding done to generate these creative designs were crisp and elegant loops with a basic math background!

Please check out the 5 video-links where I go through this simulated Interview in more detail.

C# Experiments: Dazzling Stars (Part 1)


C# Experiments: Dazzling Stars (Part 2)


C# Experiments: Dazzling Stars (Part 3)


C# Experiments: Dazzling Stars (Part 4)


C# Experiments: Dazzling Stars (Part 5)



The code typed-in during the interview series is as follows for your reference:-

//The shape of a pair of stars
// ^   ^
//< > < >
// V   V

int max = 9;

using (var sw = new StreamWriter(@"C:\Users\Sandeep\Desktop\dazzling stars.txt"))
{
    //Equal number of stars horizontally and vertically
    //for (int i = 0; i < max; i++)
    //{
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" ^  ", max)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat("< > ", max)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" V  ", max)));
    //    sw.WriteLine();
    //}

    //Right-Triangle: Increasing number of stars after every line (1-Step Increment)
    //for (int i = 1; i < max; i++)
    //{
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" ^  ", i)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat("< > ", i)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" V  ", i)));
    //    sw.WriteLine();
    //}

    //Right-Arrow: Increasing till midway, Decreasing after that (1-Step increment)
    //for (int i = 1; i <= Math.Ceiling(max / 2.0); i++)
    //{
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" ^  ", i)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat("< > ", i)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" V  ", i)));
    //    sw.WriteLine();
    //}

    //for (int i = Convert.ToInt32(max / 2.0); i >= 1; i--)
    //{
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" ^  ", i)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat("< > ", i)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" V  ", i)));
    //    sw.WriteLine();
    //}

    //Thick Right-arrow: Increasing till midway, Decreasing after that (2-Step Increments)

    //for (int i = 1; i < max; i+= 2)
    //{
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" ^  ", i)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat("< > ", i)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" V  ", i)));
    //    sw.WriteLine();
    //}

    //for (int i = max; i >= 1; i-= 2)
    //{
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" ^  ", i)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat("< > ", i)));
    //    sw.WriteLine(String.Concat(Enumerable.Repeat(" V  ", i)));
    //    sw.WriteLine();
    //}

    //Thick Left-arrow: Increasing till midway, Decreasing after that (2-Step Increments)
    //Pushed by 2-Star Increment-Spaces before&after centre line

    //for (int i = 1; i < max; i += 2)
    //{
    //    sw.WriteLine(new String(' ', 4 * (max - i)) + String.Concat(Enumerable.Repeat(" ^  ", i)));
    //    sw.WriteLine(new String(' ', 4 * (max - i)) + String.Concat(Enumerable.Repeat("< > ", i)));
    //    sw.WriteLine(new String(' ', 4 * (max - i)) + String.Concat(Enumerable.Repeat(" V  ", i)));
    //    sw.WriteLine();
    //}

    //for (int i = max; i >= 1; i -= 2)
    //{
    //    sw.WriteLine(new String(' ', 4 * (max - i)) + String.Concat(Enumerable.Repeat(" ^  ", i)));
    //    sw.WriteLine(new String(' ', 4 * (max - i)) + String.Concat(Enumerable.Repeat("< > ", i)));
    //    sw.WriteLine(new String(' ', 4 * (max - i)) + String.Concat(Enumerable.Repeat(" V  ", i)));
    //    sw.WriteLine();
    //}

    //The Diamond: Both arrows merged. Increasing till midway, Decreasing after that (2-Step Increments)
    //Pushed by 2-Star Increment-Spaces before&after centre line
    //Pushed by 1-Star Increment-Spaces on all lines except top&bottom lines.

    for (int i = 1; i < max; i += 2)
    {
        sw.WriteLine(new String(' ', 4 * ((max - i) + (i / 2))) + String.Concat(Enumerable.Repeat(" ^  ", i)));
        sw.WriteLine(new String(' ', 4 * ((max - i) + (i / 2))) + String.Concat(Enumerable.Repeat("< > ", i)));
        sw.WriteLine(new String(' ', 4 * ((max - i) + (i / 2))) + String.Concat(Enumerable.Repeat(" V  ", i)));
        sw.WriteLine();
    }

    for (int i = max; i >= 1; i -= 2)
    {
        sw.WriteLine(new String(' ', 4 * ((max - i) + (i / 2))) + String.Concat(Enumerable.Repeat(" ^  ", i)));
        sw.WriteLine(new String(' ', 4 * ((max - i) + (i / 2))) + String.Concat(Enumerable.Repeat("< > ", i)));
        sw.WriteLine(new String(' ', 4 * ((max - i) + (i / 2))) + String.Concat(Enumerable.Repeat(" V  ", i)));
        sw.WriteLine();
    }

}

MessageBox.Show("Done");

Thank you for reading this post and watching the videos. Please Subscribe, Comment and Rate the channel if you liked the video.

Goto C# Experiments to access more of such content! Thanks again!

No comments:

Post a Comment