C# – Basic Exam – Wine Glass

As mentioned in the previous two articles, here (Biggest Triple) and here (Fruit Market) I have participated in one competition for C# basic programmers. So, in this article I will present the 3rd exam problem, which I have managed to solve really quick – for about 17 minutes I have managed to obtain 100 points on it! Quite satisfactory time, including the fact, that I needed about 7 minutes to read the problem and some technical time to type it! 🙂

WineGlass

 

Pretty much, that is probably because the previous two weeks I have managed to solve some similar C# problems, concerning console painting and there was nothing that could have amazed me. Anyway, this kind of problem can be difficult for people who have no experience with such matter.

Here is my solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        int N = int.Parse(Console.ReadLine());
        int fill = N -2;
        int dots = 0;
        for (int i = 0; i < N/2; i++)
        {
            Console.Write(new string('.', dots));
            Console.Write(new string('\\', 1));
            Console.Write(new string('*', fill));
            Console.Write(new string('/', 1));
            Console.Write(new string('.', dots));
            Console.WriteLine();
            fill -= 2;
            dots++;
        }
        if (N>=12)
        {
            for (int ZZ = 0; ZZ < N/2-2; ZZ++)
            {
                            Console.Write(new string('.', N/2-1));
                            Console.Write(new string('|', 2));

            Console.Write(new string('.', N/2-1));
            Console.WriteLine();
            }
            Console.WriteLine(new string('-',N));
            Console.WriteLine(new string('-',N));
        }
        if (N <12)
        {
            for (int ZZ = 0; ZZ < N / 2-1 ; ZZ++)
            {
                Console.Write(new string('.', N / 2 - 1));
                Console.Write(new string('|', 2));

                Console.Write(new string('.', N / 2 - 1));
                Console.WriteLine();
            }
            Console.WriteLine(new string('-', N));
        }

    }
}

The author’s solution:

using System;

class WineGlass
{
	const char GlassFillingSymbol = '*';
	const char EmptySpaceSymbol = '.';

	static void Main()
	{
		// Read a positive, even number 
		int n = int.Parse(Console.ReadLine());
		int borderSpacing = 0;
		int middleSpacing = n - 2;

		for (int i = 0; i < n; i++)
		{
			// For the first half we print the upper part (bowl) of the glass
			if (i < n / 2)
			{
				string dots = new string(EmptySpaceSymbol, borderSpacing);
				string asterisks = new string(GlassFillingSymbol, middleSpacing);
				Console.WriteLine(dots + '\\' + asterisks + '/' + dots);
				borderSpacing++;
				middleSpacing -= 2;
			}
			// After that we print the stem according to the rules
			else if (n < 12 && i < n - 1 || n >= 12 && i < n - 2)
			{
				string dots = new string(EmptySpaceSymbol, n / 2 - 1);
				Console.WriteLine(dots + '|' + '|' + dots);
			}
			// Finally we print the foot of the glass
			else
			{
				Console.WriteLine(new string('-', n));
			}
		}
	}
}

So long! 🙂