Sha256: 4a87dcd1e3dab6cf22e8fb78cc75731241bc31ce2eecc13879092fef4313f68f

Contents?: true

Size: 559 Bytes

Versions: 30

Compression:

Stored size: 559 Bytes

Contents

# Solves the classic Towers of Hanoi puzzle.
def hanoi(n,a,b,c)
  if n-1 > 0
    hanoi(n-1, a, c, b)
  end
  puts "Move disk %s to %s" % [a, b]
  if n-1 > 0
    hanoi(n-1, c, b, a)
  end
end

i_args=ARGV.length
if i_args > 1
  puts "*** Need number of disks or no parameter"
  exit 1
end

n=3

if i_args > 0
  begin
    n = ARGV[0].to_i
  rescue ValueError, :msg
      print "** Expecting an integer, got: %s" % ARGV[0].to_s
    exit 2
  end
end

if n < 1 or n > 100
  puts "*** number of disks should be between 1 and 100"
  exit 2
end

hanoi(n, :a, :b, :c)

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/byebug-2.7.0/test/examples/hanoi.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/byebug-2.7.0/test/examples/hanoi.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/byebug-2.7.0/test/examples/hanoi.rb
byebug-3.0.0 test/examples/hanoi.rb
byebug-2.7.0 test/examples/hanoi.rb
byebug-2.6.0 test/examples/hanoi.rb
byebug-2.5.0 test/examples/hanoi.rb
byebug-2.4.1 test/examples/hanoi.rb
byebug-2.4.0 test/examples/hanoi.rb
byebug-2.3.1 test/examples/hanoi.rb
byebug-2.3.0 test/examples/hanoi.rb
byebug-2.2.2 old_doc/hanoi.rb
byebug-2.2.1 old_doc/hanoi.rb
byebug-2.2.0 old_doc/hanoi.rb
byebug-2.1.1 old_doc/hanoi.rb
byebug-2.1.0 old_doc/hanoi.rb
byebug-2.0.0 old_doc/hanoi.rb
byebug-1.8.2 old_doc/hanoi.rb
byebug-1.8.1 old_doc/hanoi.rb
byebug-1.8.0 old_doc/hanoi.rb