MATLAB Community

MATLAB, community & more

The Great Ans Hack

Cody is a game that rewards you for calculating the same answer with less code. The very first Cody problem you encounter is called Times 2. It’s a starter problem, so it’s meant to be super simple: write a function that multiplies the input by two. As of this writing, something like 17,000 people have solved this problem. If you were asked to solve this problem right now, you’d probably write something like this.

 function y = times2(x)
   y = 2*x;

The size of the code is given by the number of nodes in the code’s underlying parse tree. Without getting into the details of the parse tree, this function has a size of 12. That’s just about the shortest function possible. You might think it’s the very shortest times2 program one could possibly write. Nevertheless, on June 27, 2010, Dirk Engel submitted a solution that decreased the size from 12 to 10.


He wasn’t cheating, and he got the right answer. How did he do it?

You could scratch your head about this for a long time, so I’ll just tell you. Dating back to Cleve Moler’s original version of MATLAB, whenever you don’t specify a left-hand side for a calculation, the result is placed into a default variable called “ans” (for “answer”, get it?). It’s just one of those quirky things that MATLAB does. Here’s where you might see it at the command line.

 >> 2*27
 ans =

Dirk’s solution exploits this feature. It looks like this.

 function ans = times2(x)

See what he did there? The result of the calculation is automatically shoved into ans, which happens to be the return variable shown in the function definition. You probably never want to do this with “real” code. But Cody’s just a game, and we’re confident that people know the difference between competitive code-shrinking and wise practice out in the real world.

You can see from the graph that once this trick gets out, it is widely copied. In fact, it shows up all across the Cody site. Dirk himself was a prolific practitioner of the Ans Hack. But he was not, as it happens, the first. That honor goes to the Old Fox, none other than top Cody player and all-around MATLAB virtuoso Alfonso Nieto-Castanon. It first appears in a solution to Problem 495, Formatting Currency Numbers. Alfonso even adds this comment: “While on the topic of ‘undocumented trivia that will earn you a couple of Cody points for no good reason’, have you seen this one?”

Let’s turn back to the Times 2 problem. If you look farther along the graph, you’ll notice a curious thing. The Ans Hack appears for the last time on September 2, 2015. Why did it go away? Did the great mass of Cody players, with one collective will, swear off this nasty habit and consign it to the dustbin of history? Nope. In fact, the Ans Hack stopped working with the R2015b release, which came out in September of 2015. You won’t find any ans hacks after that.


Actually, that’s not completely true. The ans variable can still be used inside the function. It just can’t be passed out as an output variable. Ugly as it is, this would still work.

 function y = times2(x)
   y = ans*x;

So it’s still used for point-shaving all over Cody. Just not as an output argument.

But wherever you see an implicit “ans” as the output argument of a passing entry, you can request a re-score and have the satisfaction of seeing it wither from green to red.

What Do You Think?

Do you miss this Ans Hack? Do you wish you could get it back? What are your feelings about point-shaving hacks like this? Harmless hackery in the name of good fun? Or a corrosive influence on the impressionable minds of our coding youth? Let me know in the comments!

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.