Skip to content

Commit

Permalink
Day 16 solution in Ruby (#160)
Browse files Browse the repository at this point in the history
* 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 <aadit.k12@gmail.com>

* Update Day 10 README

Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com>

* Change heading in README and remove empty directory in day 9

Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com>

* 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 <aadit.k12@gmail.com>

* Update Day 16 README with Ruby implementation

Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com>

* Change wording of print statement

Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com>
  • Loading branch information
Aadit Kamat authored and MadhavBahl committed Jan 12, 2019
1 parent 255151b commit 2740368
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
39 changes: 38 additions & 1 deletion day16/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,44 @@ console.log ('\n/* ===== for 3 disks ===== */');
towerOfHanoi (3, 'A', 'C', 'B');
```

## Ruby Implementation

### [Solution](./Ruby/hanoi.rb)

```ruby
=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
```

## Python Implementation

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

no_of_disks = int(input())
towerhanoi(no_of_disks, 'A', 'C', 'B')

```
31 changes: 31 additions & 0 deletions day16/Ruby/hanoi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,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

0 comments on commit 2740368

Please sign in to comment.