Skip to content

Commit 2740368

Browse files
Aadit KamatMadhavBahl
Aadit Kamat
authored andcommitted
Day 16 solution in Ruby (#160)
* Add @aaditkamat as a contributor * Add Ruby code for Day 1: FizzBuzz problem * Add Ruby code for Day 2: String reverse problem * Update README.md for Day 2 * Modify Ruby code and README * Add condition for nil and wrong type edge cases * Add a seperate Ruby source code file for palindrome * Modify code for reverse.rb * Add seperate palindrome and reverse code sections in README * Update gitignore * Refactor palindrome.rb and rename heading in README * Add solution for Day 3: Hamming Problem * Add condition for strings of unequal lengths * Update README * Change project name and owner in.all-contributorsrc * Remove merge conflict lines * Add @shivank86 as a contributor * Add C++ files for different patterns * Add author and date comments at the top of C++ files * Update README.md * Add solution for Day 6 Problem in Python * Update README * Refactor code files * Modify string representation of output in python files * Add Ruby solutions for Day 6 problem * Update README for Ruby code * Add first version of solutions for Day 7 problem in C++, Java & Ruby * Modify solutions * Update Day 7 README * Remove merge conflicts from CONTRIBUTORS.md * Add back removed lines in CONTRIBUTORS.md * Add code sections contributed by @imkaka to day 6 README * Update README.md * Add C++ solution * Add Day 8 solution in C++ * Add Day 8 solution in Java * Add Day 8 solution in Ruby * Add Day 8 solution in Python * Add credits at the top of the code * Update README * Update C++ implementation * Update Python implementation * Add solution for Day 10: String Permutation Problem in Python Signed-off-by: Aadit Rahul Kamat <[email protected]> * Update Day 10 README Signed-off-by: Aadit Rahul Kamat <[email protected]> * Change heading in README and remove empty directory in day 9 Signed-off-by: Aadit Rahul Kamat <[email protected]> * Update README.md * Add Ruby solutions for Day 13 * Update README * Add credits to the code * Add Python solution for Day 13 * Update Day 13 README * Update fibonacci.py * Modify Fibonacci python code section in day 13 README * Add Ruby solution to Day 16 problem Signed-off-by: Aadit Rahul Kamat <[email protected]> * Update Day 16 README with Ruby implementation Signed-off-by: Aadit Rahul Kamat <[email protected]> * Change wording of print statement Signed-off-by: Aadit Rahul Kamat <[email protected]>
1 parent 255151b commit 2740368

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

day16/README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,44 @@ console.log ('\n/* ===== for 3 disks ===== */');
4646
towerOfHanoi (3, 'A', 'C', 'B');
4747
```
4848

49+
## Ruby Implementation
50+
51+
### [Solution](./Ruby/hanoi.rb)
52+
53+
```ruby
54+
=begin
55+
@author: aaditkamat
56+
@date: 12/01/2019
57+
=end
58+
59+
def hanoi(start_rod, aux_rod, end_rod, num)
60+
if num < 0
61+
puts "The number of disks must be a non-negative integer"
62+
return
63+
end
64+
if num == 0
65+
return
66+
end
67+
if num == 1
68+
puts "Move top most disk from rod #{start_rod} to rod #{end_rod}"
69+
return
70+
end
71+
hanoi(start_rod, end_rod, aux_rod, num - 1)
72+
hanoi(start_rod, aux_rod, end_rod, 1)
73+
hanoi(aux_rod, start_rod, end_rod, num - 1)
74+
end
75+
76+
def main
77+
print "Enter number of disks: "
78+
num = gets.chomp!.to_i
79+
print "The sequence of instructions to move #{num} disks where disk 1 is the start rod"
80+
puts "disk 2 is the auxiliary rod and disk 3 is the end rod are as follows: "
81+
hanoi(1, 2, 3, num)
82+
end
83+
84+
main
85+
```
86+
4987
## Python Implementation
5088

5189
### [Solution](./Python/tower_hanoi.py)
@@ -68,5 +106,4 @@ def towerhanoi(n, from_rod, to_rod, aux_rod):
68106

69107
no_of_disks = int(input())
70108
towerhanoi(no_of_disks, 'A', 'C', 'B')
71-
72109
```

day16/Ruby/hanoi.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=begin
2+
@author: aaditkamat
3+
@date: 12/01/2019
4+
=end
5+
6+
def hanoi(start_rod, aux_rod, end_rod, num)
7+
if num < 0
8+
puts "The number of disks must be a non-negative integer"
9+
return
10+
end
11+
if num == 0
12+
return
13+
end
14+
if num == 1
15+
puts "Move top most disk from rod #{start_rod} to rod #{end_rod}"
16+
return
17+
end
18+
hanoi(start_rod, end_rod, aux_rod, num - 1)
19+
hanoi(start_rod, aux_rod, end_rod, 1)
20+
hanoi(aux_rod, start_rod, end_rod, num - 1)
21+
end
22+
23+
def main
24+
print "Enter number of disks: "
25+
num = gets.chomp!.to_i
26+
print "The sequence of instructions to move #{num} disks where disk 1 is the start rod"
27+
puts "disk 2 is the auxiliary rod and disk 3 is the end rod are as follows: "
28+
hanoi(1, 2, 3, num)
29+
end
30+
31+
main

0 commit comments

Comments
 (0)