Coder Social home page Coder Social logo

towers-of-hanoi-web-0916's Introduction

Towers of Hanoi

LETS GET READY TO RECURSE!!!!!!

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.

Remember that there is more than one way to solve this problem. Don't try and bulldoze your way through it. Write comments and psuedo code. Write it out on paper. Work your way through the problem logically. Watch the video below if you need help.

Here's a little extra help if you need an idea of how to get started.

Resources:

Video Explanation

hanoi

http://en.wikipedia.org/wiki/Tower_of_Hanoi

http://anabecker.tumblr.com/post/43500989356/ways-we-tried-to-solve-the-hanoi-tower-problem

This is just an example of how you might get started with this problem.

# tower_of_hanoi.rb

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)

Resources

View Towers of Hanoi on Learn.co and start learning to code for free.

towers-of-hanoi-web-0916's People

Contributors

aviflombaum avatar kthffmn avatar loganhasson avatar fislabstest avatar arelenglish avatar fs-lms-test-bot avatar sarogers avatar lshapz avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.