Hacker News new | past | comments | ask | show | jobs | submit login
Brainfuck in one line of Python (princeton.edu)
80 points by JJMalina on Dec 4, 2011 | hide | past | favorite | 27 comments



And for our next trick, Python in one line of Brainfuck...


I must admit I was disappointed - v2 of the original AmigaOS Brainfuck compiler is a 240 bytes binary, and the C interpreter is 1047 bytes of unobfuscated C source.

1400+ bytes of tightly packed Python just isn't in the same class.


The goal wasn't "tightly packed" the goal was "one line."

In Python that means, essentially, one expression. That's what makes it a stunt.


My point with mentioning "tightly packed" was that if you'd tried to make the C version small, you'd come out with something much tinier than the 1047 bytes I mentioned.

I see he's set himself a very different goal, which is fine but not that interesting to those of us that aren't Python programmers, I guess.

What disappointed me was clicking through from a headline that seemed to indicate it'd be a tiny implementation, when it's in fact rather bloated.


In this case "one line" doesn't mean much. You could have a 1mb file in one line. Nowadays a "line" is between 80-140 characters.


You are not entirely right. In python, one line means something as it is only one statement (as mentioned above). Author is also strict about not using eval -- with eval you could snuck multiple lines into one line (using \n in the evaluated string).


Last time when I used python I could separate statements with a semicolon and put as many as i want in a line. Did that change ? ;-)


This did not change, but the author also didn't use semicolons. As stated many times above, this is a single Python expression.


I'm not sure you can write function definitions, (nested) loops and other things that rely on indentation in single line using semicolons. At least I never managed to do that.


You can do it with lambdas, but it's going to be absurdly ugly: http://blog.sigfpe.com/2008/09/on-writing-python-one-liners....


I reckon that the challenge involved in reducing the problem to one line (as in one single Python statement) has little to do with how many characters long it is.


That isn't the normal way of measuring the length of code in code golfing contests. It's number of normal-sized lines, for some measurement of normal that is less than 1400 characters. Pretty neat, though; just a misleading title.


Maybe "in one Python statement" would be more appropriate.


Every Lisp program can be rewritten to be just "one expression"

  `(progn ,@program)
What have we achieved here?


I think the first paragraph explained it quite well.

Whether or not the feat itself is impressive or not I don't really know but the reason for the quest was motivated.


My point was that "one-line" is a faulty criterion for measuring compactness if we allow for arbitrarily long lines: or "long cat is long" considered harmful.


I'm guessing you don't deal with Python much. Python actively discourages this sort of thing, it's actually hard to create obfuscated code of the kind you see in most other languages.

That this was even possible surprised me somewhat, that he actually figured it out I find quite impressive.


Fair point, and the magnitude of this feat was not lost on me. I was just speaking in general, we need a better metric for one-linerness than s/\n//g.


you can't parse any non-regular language on any given computer

It is nice how the author completely ignored LALR parsers[1], peg parsers[2], and so on.

[1] http://en.wikipedia.org/wiki/LALR_parser

[2] http://en.wikipedia.org/wiki/Parsing_expression_grammar


You can only imperfectly emulate anything more than a regular language in a finite reality (since a pushdown automaton has an infinite stack). A PC is just a big DFA.


Any program can be called a "one liner" if you just strip newlines and don't limit the line length.


It's generally accepted that a semicolon (in most languages) means two lines no matter how the spacing goes. In Python, it's particularly tricky to have a complex program evaluate to one statement because of limitations like no assignments in expressions. It's true, though, that this is basically the same as writing Lisp in Python.


Not in Python.


So how long would it be if you used a better python interpreter with tail calls?


I'm still appalled at the fact that the term 'brainfuck' is used in a Princeton website.


Lots of people can put things on URLs under princeton.edu, or the domains of most universities. In this case, note the ~username in the URL--this is the personal page of a graduate student. Usually universities do not censor pages of their students. I do agree, though, that it would be surprising to see this on a departmental page or anything else owned by a PR team.


Why? It is the name of the language.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: