StairNways

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;

namespace stairNways
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int noOfPossibilities = 2;
            int noOfSteps = 5;
			List<int> combinationsList = new List<int>(){1,2};
            List<int> noOfWaysList = new List<int>();
            bool reached = false;
            //Console.Write("Enter the number of steps: ");
			Console.WriteLine("Number of steps: {0}", noOfSteps);
            //noOfSteps = Convert.ToInt32(Console.ReadLine());
            //Console.Write("Enter the number of possible step overs that you want to make: ");
			Console.WriteLine("Number of possible stepovers: {0}", noOfPossibilities);
            //noOfPossibilities = Convert.ToInt32(Console.ReadLine());
            if (noOfPossibilities > 0)
            {
                //Console.WriteLine("Enter the step over values: ");
                //for (int i = 0; i < noOfPossibilities; i++)
                //{
                    //Console.Write("> ");
                    //combinationsList.Add(Convert.ToInt32(Console.ReadLine()));
                //}				
				combinationsList.ForEach(x => {Console.WriteLine("> {0}", x);});
                Console.WriteLine("You can reach your destination in following possible ways");
                do
                {
                    noOfWaysList = GetArray(noOfWaysList, combinationsList, noOfSteps);
                    reached = checkReached(noOfWaysList, noOfSteps);
                } while (!reached);
                Console.WriteLine(">>>>>>>>>>>>>>>>{0}<<<<<<<<<<<<<<<", noOfWaysList.Count());
            }
            else
            {
                Console.WriteLine("you entered an invalid nuber");
            }
            
        }

        private static bool checkReached(List<int> noOfWaysList, int noOfSteps)
        {
            bool reached = true;
            foreach (var nwl in noOfWaysList)
            {
                if (nwl != noOfSteps)
                    reached = false;
            }
            return reached;
        }

        private static List<int> GetArray(List<int> possibleWaysList, List<int> stepOvers, int noOfSteps)
        {
            if (possibleWaysList == null || possibleWaysList.Count() == 0)
            {
                foreach (var stepOver in stepOvers)
                {
                    possibleWaysList.Add(stepOver);
                }
            }
            else
            {
                List<int> dummyList = new List<int>();
                foreach (var pwl in possibleWaysList)
                {
                    if (pwl == noOfSteps)
                    {
                        dummyList.Add(pwl);
                        continue;
                    }

                    foreach (var so in stepOvers)
                    {
                        if ((pwl + so) <= noOfSteps)
                        {
                            dummyList.Add(pwl + so);
                        }
                    }
                }
                return dummyList;
            }
            return possibleWaysList;
        }
    }
}
;