Skip to content

Instantly share code, notes, and snippets.

@rosiehoyem
Last active December 25, 2015 10:59
Show Gist options
  • Save rosiehoyem/6965732 to your computer and use it in GitHub Desktop.
Save rosiehoyem/6965732 to your computer and use it in GitHub Desktop.
Tower of Hanoi, Day 7
#Tower of Hanoi
# Recursive solution:
# http://www.sparknotes.com/cs/recursion/examples/section6.rhtml
def toh(n, source = 'A', temp = 'B', dest = 'C')
toh(n - 1, source, dest, temp) if n > 1 # move all n - 1 disks to temp pole
puts "Move top disc #{n}: #{source} => #{dest}." # move bottom disk to destination pole
toh(n - 1, temp, source, dest) if n > 1 # move all n - 1 disks to destination pole
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment