Monday, September 27, 2010

Riddle: What Does This Program Do?

On the occasion of the 100th post I prepared a little riddle.
The presented class describes the function of a "gadget" of the field of technical computer science, whoever gets this first, is the winner.
The class is programmed chaotically on purpose, when using it the function ABC() is called with 3 parameters.
Now the code:

    public class CrazyTool
    {
        public string ABC(string a, string b, string c)
        {
            if (!DEF(a, b, c))
                return "d";

            if (a == "x")
            {
                int d = 0;
                if (b == "x")
                    d = 0;
                else
                    d = 1;
                int e = d + int.Parse(c.Replace('x', '0').Replace('y', '1'));
                switch (e * 3)
                { 
                    case 0: return ("aa");
                    case 3: return ("ba");
                    case 6: return ("ab");
                }
            }
            else
            {
                int f = 0;
                if (b == "x")
                    f = 0;
                else
                    f = 1;
                int g = f + int.Parse(c.Replace('x', '0').Replace('y', '1'));
                switch (g * 3)
                { 
                    case 0: return ("ba");
                    case 3: return ("ab");
                    case 6: return ("bb");
                }
            }

            return "d";
        }

        private bool DEF(string a, string b, string c)
        {
            if (a != "x" && a != "y")
                return false;
            if (((byte)(b.ToCharArray()[0]) != 120 && ((byte)(b.ToCharArray()[0]) != 121)))
                return false;
            Regex r = new Regex("[xy]");
            if (!r.IsMatch(c))
                return false;
            return true;
        }
    }

No comments:

Post a Comment