I'd like some feedback on an idea for a new type of challenge that I have, namely Abstract Syntax Tree golfing.
Code Golfing is a lot of fun. But many of the tricks that get used in a given language are the same for every challenge. I'd like to ask a challenge which, instead of counting bytes, counts pieces of code. "Pieces of code" isn't very specific, so to make it more objective, I will use the size of the abstract syntax tree of the program.
This idea has a major problem: Not every language has an abstract syntax tree (e.g. Seeds), and the same program in the same language may be converted to different ASTs by different interpreters or compilers. To solve this problem, my initial challenge will only allow one language, namely Python. Python is chosen because it has a canonical abstract syntax tree for any piece of code, namely the one returned by the
The scoring code for this challenge will be as follows:
import ast print(len(list(ast.walk(ast.parse(code)))))
Note that this code gives the desired result in both Python 2 and 3, though a given piece of code may have a different scoring in the two versions.
Now, there are certain types of code which I am worried may trivialize the challenge:
exec "stuff"will do anything in 2 tokens, but it's not very interesting.
evalhas the same issue, as well as
ast.literal_evalto a lesser extent, and likely many other functions. I'd like to ban all such functions.
Very long data-tokens. More specifically, I'm worried about very long string and numeric literals. For instance, for the challenge "Print all primes under a million", I don't want a literal string with a million lines to win. To handle this, I'd like to ban all tokens longer than 15 characters of source code. This also handles worries about code like the following:
def crazylongfunctionnameitsreallygettingoutofcontrol(): pass print(crazylongfunctionnameitsreallygettingoutofcontrol.__name__)
So I'll just ban it all. If code legitimately needs long strings or large numbers, it can build it out of smaller strings or numbers at some overhead.
Since I'd like submissions to be fully automatically checked and scored, I'll use Python's tokenize module for this. Code can be found here: simple_counter.py
I'm thinking of posting the classic FizzBuzz challenge as the first challenge of this type.
What do people think of this challenge? Would it produce interesting submissions? Interesting winning submissions? Are there other ways to trivialize the challenge that I haven't thought of?