Code trolling sample

Sometimes it’s worth giving unexpected answers to sillier questions. Take this one, for example, clearly inspired by some homework a student received. For example, this time the question was: “What is the C# code that will print the pattern, “1 121 12321 1234321”?“.

In all honesty, the question shows clearly that there was absolutely no intention from the person asking the question to actually think things through. So my answer came, naturally, in the wonderful tradition of code trolling. I copy below the main function (the rest is code ripped off some github repository, guarded with #if ZERO).

static void Main(string[] args)
{
Func<int, Func<int, string>, string> t1 = (x, y) => { return x == 0 ? string.Empty : y(x); };
Func<int, string> t3 = x => { return t1(x, y => { return x.ToString(); }); };
Func<int, IEnumerable> f = x => { return Enumerable.Range(1, x); };
Action<IEnumerable> f2 = x => { x.ToList().ForEach(Console.Write); x.Reverse().Skip(1).ToList().ForEach(Console.Write); Console.Write(‘ ‘); };
Action m = z => Enumerable.Range(1, z).ToList().ForEach(x => f2(f(Int32.Parse(t3(x)))));
Func<string, string> temp = x => { m(4); return Environment.NewLine; };
Func<string, string, string> t2 = (x, z) => { Console.Write(z); return x; };
t2(t3(42), t2(string.Empty, temp(“Avada kedavra”)));
}

The implementation is really done in functions f2 and z, with the rest of the functions being basically fillers and leftovers from different tries. I obviously abuse lambdas because they make the code slightly unreadable. But reading the f2 function will really give away all the ‘magic’, since I was too bored to actually make that too complicated, and I really love LINQ.

Comments

Code trolling sample — 4 Comments