Last active
December 13, 2015 18:18
-
-
Save robertjwhitney/4953751 to your computer and use it in GitHub Desktop.
A lesson in recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LETS GET READY TO RECURSE!!!!!! | |
# | |
# Tower of Hanoi | |
# | |
# You have 4 discs which vary in size from small to large | |
# There are 3 pegs, A, B, C, respectively | |
# | |
# The discs are all currently on peg A, with the largest disc on the bottom, | |
# and the smallest disc on the top. | |
# | |
# | |
# | - 1 - | | | | |
# | -- 2 -- | | | | |
# | --- 3 --- | | | | |
# | ---- 4 ---- | | | | |
# _________A________________B_________________C_________ | |
# | |
# | |
# Get the discs from peg A to peg C. | |
# | |
# Rules: | |
# - Only one disk may be moved at a time. | |
# - Each move consists of taking the upper disk from one of the pegs and sliding | |
# it onto another peg, on top of the other disks that may already be present on that peg. | |
# - No disk may be placed on top of a smaller disk (can't put disc 4 on top of disc 1). | |
# | |
# One method for setting this up might look like: | |
# | |
# a = [1,2,3,4] | |
# b = [] # empty Array | |
# c = [] # empty Array | |
# | |
# Then, as you move items(discs) between Arrays(pegs), you can't ever allow | |
# a larger number occurs before a smaller one in any Array, like: | |
# | |
# c = [2,1] # WRONG! | |
# | |
# Write a method that will call itself recursively until the discs are stacked on the C peg | |
# with the largest (4) on the bottom and the smallest (1) on top. So, write that method so | |
# that in the end the numbers are in the same order but in Array c. | |
# | |
a = [1,2,3,4] | |
b = [] | |
c = [] | |
# Move the disks from one peg to another following the rules of Hanoi. | |
# | |
# number_of_disks - the total number of disks | |
# from - the starting peg | |
# to - the ending (goal) peg | |
# via - the remaining peg (b in this case) | |
# | |
def move_disk(number_of_disks,from,to,via) | |
# Do your magic here | |
# your method should return to | |
to | |
end | |
# here we go! | |
puts move_disk(4, a, c, b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment