Hacker News new | past | comments | ask | show | jobs | submit login

> What's the most elegant piece of code you've seen?

Well, not the most elegant, but a very elegant piece of code, solves the Towers of Hanoi puzzle in a few ingenious lines (Python as shown):

    def printMove(disk,src,dest):
      print("Move disk %d from %s to %s" % (disk,src,dest))
    
    def moveDisk(disk,src, dest, using):
      if disk >= 1:
        moveDisk(disk-1,src,using,dest)
        printMove(disk,src,dest)
        moveDisk(disk-1,using,dest,src)
    
    count = 3
    
    moveDisk(count,'A','B','C')
    
Result:

    Move disk 1 from A to B
    Move disk 2 from A to C
    Move disk 1 from B to C
    Move disk 3 from A to B
    Move disk 1 from C to A
    Move disk 2 from C to B
    Move disk 1 from A to B



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

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

Search: