forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhanoi.rb
More file actions
31 lines (28 loc) · 725 Bytes
/
hanoi.rb
File metadata and controls
31 lines (28 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
=begin
@author: aaditkamat
@date: 12/01/2019
=end
def hanoi(start_rod, aux_rod, end_rod, num)
if num < 0
puts "The number of disks must be a non-negative integer"
return
end
if num == 0
return
end
if num == 1
puts "Move top most disk from rod #{start_rod} to rod #{end_rod}"
return
end
hanoi(start_rod, end_rod, aux_rod, num - 1)
hanoi(start_rod, aux_rod, end_rod, 1)
hanoi(aux_rod, start_rod, end_rod, num - 1)
end
def main
print "Enter number of disks: "
num = gets.chomp!.to_i
print "The sequence of instructions to move #{num} disks where disk 1 is the start rod"
puts "disk 2 is the auxiliary rod and disk 3 is the end rod are as follows: "
hanoi(1, 2, 3, num)
end
main