Posts Tagged ‘C#’

        private static void Carpets()
        {
            // 4. Carpets
            int nWidth = int.Parse(Console.ReadLine());
            int nHalf = nWidth / 2;
            string lastChar = "";
            //string[] arrAllRows2Half = new string[nHalf-1];
            string[][] jaggedArray = new string[nHalf][];

            // rows & columns
            // I. rows - upper
            for (int row = 1; row <= nHalf; row++)
            {
                string[] arrChars2Half = new string[nWidth];
                // II cols
                for (int col2Half = 0; col2Half < nHalf; col2Half++)
                {
                    if (col2Half < nHalf - row)
                    {
                        arrChars2Half[col2Half] = ".";
                        lastChar = arrChars2Half[col2Half];
                    }
                    else if (col2Half == nHalf - row)
                    {
                        arrChars2Half[col2Half] = "/";
                        lastChar = arrChars2Half[col2Half];
                    }
                    else if (col2Half > nHalf - row)
                    {
                        // inner rombs
                        if (lastChar == " ")
                        {
                            arrChars2Half[col2Half] = "/";
                            lastChar = arrChars2Half[col2Half];
                        }
                        else if (lastChar == "/")
                        {
                            arrChars2Half[col2Half] = " ";
                            lastChar = arrChars2Half[col2Half];
                        }
                        // inner rombs
                    }

                }   // for (int col2Half = 0; col2Half < nHalf; col2Half++)

                // second part of the string - backslash
                for (int col2Half = nHalf; col2Half < nWidth; col2Half++)
                {
                    if (col2Half < nHalf + row - 1)
                    {
                        // inner rombs
                        if (arrChars2Half[nHalf - 1] == "/" && col2Half == nHalf)
                        {
                            arrChars2Half[col2Half] = @"\";
                            lastChar = arrChars2Half[col2Half];
                        }
                        else if (arrChars2Half[nHalf - 1] == " " && col2Half == nHalf)
                        {
                            arrChars2Half[col2Half] = " ";
                            lastChar = arrChars2Half[col2Half];
                        }
                        else if (lastChar == @"\" && col2Half != nHalf)
                        {
                            arrChars2Half[col2Half] = " ";
                            lastChar = arrChars2Half[col2Half];
                        }
                        else if (lastChar == " " && col2Half != nHalf)
                        {
                            arrChars2Half[col2Half] = @"\";
                            lastChar = arrChars2Half[col2Half];
                        }
                        // inner rombs
                    }
                    else if (col2Half == nHalf + row - 1)
                    {
                        arrChars2Half[col2Half] = @"\";
                        lastChar = arrChars2Half[col2Half];
                    }
                    else if (col2Half > nHalf + row - 1)
                    {
                        arrChars2Half[col2Half] = ".";
                        lastChar = arrChars2Half[col2Half];
                    }

                }       // for (int col2Half = nHalf; col2Half < nWidth; col2Half++)

                //jaggedArray[row][arrChars2Half.Length] = new string[1, arrChars2Half.Length];

                //jaggedArray[row] = new string[nWidth-1];
                for (int i = 0; i < arrChars2Half.Length; i++)
                {
                    Console.Write(arrChars2Half[i]);
                    //jaggedArray[row][i] = arrChars2Half[i];
                }
                jaggedArray[row-1] = new string[nWidth - 1];
                jaggedArray[row-1] = arrChars2Half;
                Console.WriteLine();
            }   //for (int row = 1; row <= nHalf; row++)

            // to print jaggedArr
            for (int row = nHalf-1; row >= 0; row--)
            {
                for (int i = 0; i < nWidth; i++)
                {
                    if (jaggedArray[row][i] == @"\")
                    {
                        Console.Write("/");
                    }
                    else if (jaggedArray[row][i] == "/")
                    {
                        Console.Write(@"\");
                    }
                    else
                    {
                        Console.Write(jaggedArray[row][i]);
                    }
                }
                Console.WriteLine();
            }

        }   // end of carpets
Advertisements

A fast and beautiful solution.  🙂

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

namespace WorkDays
{
class WorkDays
{
private static DateTime[] holidays = { new DateTime(2012, 12, 24), new DateTime(2012, 12, 25), new DateTime(2012, 12, 26),
new DateTime(2012, 12, 30), new DateTime(2012, 12, 31), new DateTime(2013, 01, 01) };

static void Main(string[] args)
{
// 5. Write a method that calculates the number of workdays between today and given date, passed as parameter.
// Consider that workdays are all days from Monday to Friday except a fixed list of public holidays
// specified preliminary as array.

Console.WriteLine(“Method that calculates the number of workdays between today and given date, passed as parameter.  \n\n”);

Console.WriteLine(“What is the past date:”);
DateTime StartDate = DateTime.Parse(Console.ReadLine());

int nWorkingDays = CalculateWorkDays(StartDate);
if (nWorkingDays > 0)
Console.WriteLine(“Number of work days is {0}”, nWorkingDays);
else Console.WriteLine(“Invlid dates!”);

}

private static int CalculateWorkDays(DateTime StartDate)
{
DateTime TodayDate = DateTime.Now;
int nWorkingDays = 0;

if (TodayDate.Date > StartDate.Date)
{
DateTime day = StartDate.Date;
while (day <= TodayDate.Date)
{
if (day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday
&& !holidays.Contains(day))
{
nWorkingDays++;
}

day = day.AddDays(1);
}
}

return nWorkingDays;
}

}
}