Hacker News new | past | comments | ask | show | jobs | submit login
What is the coolest thing you can do in ten lines of code? (stackoverflow.com)
73 points by sadiq on Dec 30, 2011 | hide | past | favorite | 36 comments



I don't about you guys but when I did

0) Open notepad 1) Write <html>My name is Ely <img src=''></html> 2) CTRL + S as index.htm 3) FTP to server 4) Then went to site.com/

That's when I knew I was going to be an amateur programmer for life. Yeah, yeah I know, it's HTML. But that gave me the courage to later jump into server-side languages.


If we could teach this simple process in school, we would inspire tons of people to program little things for themselves. That would be awesome.


Is 30 close enough to 10?

I got a job with 30 lines of code appended to my resume, with a screenshot of the resultant output.

Perhaps the first two pages had something to do with it but I was told that the third page was what stood out.

This was a sysadmin position, the code includes bash, sed, awk, grep, tcpdump, netcat and gnuplot.

Hint to anyone who tries this sort of resume stunt: You WILL be asked what the code does.

Just the code: http://bbookkss.com/CharlesPerkinsCV3of3Pages.pdf


That's indeed an interesting example because, by any standard, this is terrible code.

Yet for a sysadmin position it still provides valuable insight into your a) pain threshold, b) familiarity with the tools, c) willingness and ability to bend reality for the boss.

I'd just add a big disclaimer that this kind of code only happens in emergencies.

(disclaimer: I'm guilty of these gnuplot hacks myself and they always stick around way longer than they should...)


> They always stick around way longer than they should...

But isn't that true for all 'terrible' code? Most of the elegant and well-formed code I write ends up getting butchered for the sake of 'features,' while the quick hacks that 'just work' stick around forever.


That's indeed an interesting example because, by any standard, this is terrible code.

It's not nice to criticize without being constructive. Care to explain why the code is bad, and offer better suggestions?


Useless 'cat' detected ;)

But seriously, nice work. Being able to put something useful together that easily (in terms of lines of code) is what most attracted me to UNIX/Linux coming from Windows. The amount of CLI tools one can take advantage of is simply astonishing.


How long would it have taken you to write that?


Back in real-mode x86 times the coolest thing could've been done in 2 lines

  CLI     ; Disable interrupts
  HLT     ; Halt CPU until next interrupt
This hung the machine dead.


Unless there's a memory error (which is hooked up to the NMI line), or on the PCjr, a key was hit on the keyboard (which was hooked up to the NMI).

Neat hack though (and question: why do I remember this stuff?)


This is not cool in my book.


... nor is downvoting nerdy posts on HN of all places :)


Not to forget the F00F bug, which did the same thing in full-blown protected mode.


http://stackoverflow.com/a/811081

This person claims that simple variable usage and stdout should be all that is needed to impress. While I studied enough low-level software development to still be impressed by an adder circuit, I just don't think this will impress enoug of the people we NEED to convert. Just about everyone takes it for granted that when they log onto FB, they will see their name...and that doesnt seem like "programming" to them


Less than 10 bytes of code on an Apple II:

  ] CALL -151
  * 300: AD 30 CO 20 ED FD 4C 00 03
  * 300G
Enjoy! For bonus points, get this down to 8 bytes.


; initialize

  CALL -151
  1000:BA 8A A8 49 80 AA 9A A9 2 48 A9 FF 48 BA 86 6 98 AA 9A A6 6 A9 30 85 6 A9 C0 85 7 A0 0 0 N 1000G
; six bytes!

  300:9A B1 6 4C ED FD
  300G


I used to do that kind of hacking in the 80's. I recognized the CALL-151. I have an old Apple II+ in the other room I'm tempted to try firing up just to solve this mystery.


  NEW
  1HOME:A=9200:B=13168:C=100:FORI=768TO796:READN:POKEI,N:NEXT:HGR:POKE-16302,0:HCOLOR=4:HPLOT0,0:CALL62454:HCOLOR=2:HPLOT0,63TO279,63:HPLOT0,62TO279,62:HCOLOR=6:HPLOT0,61TO279,61:HPLOT0,60TO279,60:HCOLOR=1:HPLOT0,59TO279,59:HPLOT0,58TO279,58
  2HCOLOR=5:HPLOT0,55TO279,55:HPLOT0,54TO279,54:HCOLOR=0:HPLOT0,123TOC,123:HPLOT0,122TOC,122:HPLOT0,119TOC,119:HPLOT0,118TOC,118:COLOR=0:FORI=0TO39:VLIN0,39ATI:NEXT:COLOR=13:HLIN0,39AT14:FORI=0TO7:POKEA+I,0:POKEB+I,0:NEXT:COLOR=1
  3HLIN0,39AT13:VTAB21:PRINTTAB(16)"RAINBOW":PRINT:PRINT"MIXED GRAPHICS (HI-RES/COLOR)":CALL768:DATA173,87,192,173,83,192,173,84,192,173,80,192,208,251,173,86,192,160,22,136,208,253,234,173,87,192,76,9,3
  RUN


For people who didn't hack apple IIs, can you expand on what's going on there?


; enter the machine language monitor

  CALL -151
; toggle the speaker and load the accumulator with the "floating bus" value--the byte last read by the video refresh controlled by the video scanner.

  0300-   AD 30 C0   LDA $C030
; print the accumulator as a character

  0303-   20 ED FD   JSR $FDED
; repeat (loop by going to 0x300)

  0306-   4C 00 03   JMP $0300
; run it! (0x300 Go; just like G in your debugger)

  300G
Running this will display random characters and make noise.


Should be able to use a two-byte BEQ, BNE, BCC, BCS, or something like that instead of the jump, depending on what COUT returns.


A colleague of mine wrote a spreadsheet in one line (253 bytes) of BASIC. I wonder if I still have it somewhere ...


That's pretty interesting. What features did it offer? Just a grid of cells, or some crude facility for summing/counting rows and columns as well?


It was a grid of cells. Each cell could contain a constant or a formula, strings or numbers. You selected the cell to change with a mouse, and it recomputed once per click on a button. In essence it was fully functional, just lacking file load and save.

It didn't have charts or graphics, but the cell functionality was pretty complete. Using "EVAL" meant you had access to any function in the underlying language.

I'll see if I can dig it out.


This is sorta evil. But was fun to write tonight.

    ;; run this on SBCL
    (sb-ext:unlock-package :common-lisp)
    (let (syms) (do-symbols (s :common-lisp) (if (fboundp s) (push s syms)))
      (labels ((worm (sym)
    	     (let ((next (elt syms (random (length syms))))
    		   (old (symbol-function sym)))
    	       (format t "~%Worm attacks ~a, next target is ~a!~%" sym next)
    	       (setf (symbol-function sym)
    		     (lambda (&rest args) 
    		       (when next (worm next) (setf next nil)) (apply old args))))))
      (worm '+)))
    ;; now try adding some numbers (and so on)
Long ago I also wrote a quine-based payload version of the above, moving towards doing the same for NewtonScript. But I've long since lost it.


Maybe not cool, but practical, I'd like to nominate some OCaml bitstring matching code:

    let bits = Bitstring.bitstring_of_file "data" in
    bitmatch bits with
    | { width : 5;
        data : width } ->
        printf "width %d data %Ld\n" width data
It reads the first five bits of the file, interprets these bits as a number, then reads the next 0-31 bits from the file (note: not aligned, the first 3 bits come from the first byte in the file).

The nice thing is that if everything can be shown at compile time to be aligned to byte/word boundaries, then it all turns into relatively efficient calls to C.

There are some more realistic examples on the web page:

https://code.google.com/p/bitstring/


Just about any Forth Haiku would probably qualify. Here's a nice Mandelbrot set (not my code):

  : z^2 2dup z* ;
  : layer push push z^2 pop pop 2dup push push z+ pop pop ;
  : many layer layer layer layer layer layer layer layer layer layer ;
  : len dup * swap dup * + ;
  : mant y 0.7 - 3 * x 0.5 - 3 * many drop drop len dup 1 < * ;
  0 0 mant
  0.1 0.1 mant
  0.2 0.2 mant
(http://www.forthsalon.appspot.com/haiku-view/agpmb3J0aHNhbG9...)


Coming late to the party, but Simon Tatham's .sig is pretty cool:

    for k in [pow(x,37,0x13AC59F3ECAC3127065A9) for x in [0x195A0BCE1C2F0310B43C,
    0x73A0CE584254AB23D5A0, 0x12878657EA814421CC92, 0x7373445BB3DA69996F4A,
    0x77A7ED5BC3AA700E80B2, 0xE9C71C94ED87ADCF7367, 0xFE920395F414C1A5DB50]]:
    print "".join([chr(32+3*((k>>x)&1))for x in range(79)]) # <anakin at pobox.com>
It won't be completely obvious to most people why this is cool, but it's effectively RSA-signed.


Viznut's classic signature:

    #include <math.h> /* outputs 8 kHz 8-bit unsigned pcm to stdout */
    main(v,i,z,n,u,t){for(v=-1;;)for(n=pow(/* gcc -lm sig.c;./a.out>/dev/dsp */
    1.06,"`cW`g[`cgcg[eYcb^bV^eW^be^bVecb^"[++v&31]+(v&64)/21),i=9 99;i;putchar(
    128+((8191&u)>i?0:i/8)-((8191&(z+=n))*i-->>16)))u+=v&1?t/2: (t=v&6?t:n/4);}


You can generate the Sierpinski pattern in a lot less code than posted:

    iterate (\xs -> zipWith xor ([0] ++ xs) (xs ++ [0])) [1]


Mandelbrot fractal set viewer in brainfuck would fit the bill nicely. It's not 10 lines exactly, but reflowing the BF code with no loss of clarity is not a big deal :)

http://esoteric.sange.fi/brainfuck/utils/mandelbrot/mandelbr...


In Lua:

  function Y(f)
    local function g(...) return f(g,...) end
    return g
  end

  print(Y(function(rec,x)
            if x < 2 then
              return 1
            else
              return x * rec(x-1)
            end
           end)(5))


I'd have said that for impressing people with small pieces of code something like SecondLife scripting would be ideal. You can manipulate objects in a 3d environment using simple code. Who doesn't enjoy programming a gun?


Also COM scripting on Windows (and presumably Applescript on the Mac).

Sure there's a lot of special keyword magic involved, but nothing will get people more excited about programming than seeing how they can easily automate their routine tasks to make their life easier - that's what programming is supposed to be about anyway.


You could probably stuff Node into this category too, if your target is the type to be impressed with writing their own basic web server in 10 lines of code.


Conways game of life animated in APL:

http://uk.youtube.com/watch?v=a9xAKttWgP4&fmt=18

I didn't count the number of lines but I guess it's even less than ten.




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

Search: