Skip to content

Commit 2f7d952

Browse files
authored
Merge pull request #43 from sanchojaf/master
Adding Towers of Hanoi using Stack
2 parents 8595376 + d4cfc5d commit 2f7d952

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Algorithms playground for common questions solved in ruby syntax.
55
In case you want to prepare yourself for a job interview - try to solve it yourself first, then have a look here.
66

77
# Why?
8-
I interviewed with [Google](https://github.com/sagivo/algorithms/blob/master/src/google-interview-tips.md), [Facebook](https://github.com/sagivo/algorithms/blob/master/src/facebook-interview-tips.md), [LinkedIn](https://github.com/sagivo/algorithms/blob/master/src/linkedin-interview.md), Twitter and others. I also interviewed others myself.
9-
Sometimes it looks like they all ask you the same "out of the box" questions that don't really check knowledge but memorization of the same tricks.
10-
This is my way of saying - change your interview style. There are lots of smart people out there, this is not the best way to find them.
8+
I interviewed with [Google](https://github.com/sagivo/algorithms/blob/master/src/google-interview-tips.md), [Facebook](https://github.com/sagivo/algorithms/blob/master/src/facebook-interview-tips.md), [LinkedIn](https://github.com/sagivo/algorithms/blob/master/src/linkedin-interview.md), Twitter and others. I also interviewed others myself.
9+
Sometimes it looks like they all ask you the same "out of the box" questions that don't really check knowledge but memorization of the same tricks.
10+
This is my way of saying - change your interview style. There are lots of smart people out there, this is not the best way to find them.
1111

1212
# Problems
1313

@@ -53,7 +53,8 @@ This is my way of saying - change your interview style. There are lots of smart
5353
| [Eucliden and Extended Eucliden algorithm](http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm) | [click](https://github.com/sagivo/algorithms/blob/master/src/gcd.rb) |
5454
| Suggest index of a number in an array | [click](https://github.com/sagivo/algorithms/blob/master/src/sugget_index_in_array.rb) |
5555
| [Range minimum query sparse table algorithm](http://en.wikipedia.org/wiki/Range_minimum_query) | [click](https://github.com/sagivo/algorithms/blob/master/src/rmq.rb) |
56-
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [click](https://github.com/sagivo/algorithms/blob/master/src/insertion_sort.rb) |
56+
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [click](https://github.com/sagivo/algorithms/blob/master/src/insertion_sort.rb) |
57+
| [Towers of Hanoi using Stack](https://en.wikipedia.org/wiki/Tower_of_Hanoi) | [click](blob/master/src/towers_of_hanoi_with_stack.rb)
5758
| [Tarjan's strongly connected components finder](https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm) | [click](https://github.com/sagivo/algorithms/blob/master/src/tarjan.rb) |
5859

5960

@@ -62,5 +63,5 @@ Simply run `ruby some_file.rb` to execute the algorithm. At the bottom of each f
6263
example: `ruby brackets_combinations.rb` will print:
6364
`["((()))", "(()())", "(())()", "()(())", "()()()"]`.
6465

65-
# Contribute
66+
# Contribute
6667
Did you find a bug? any way to do it better? please feel free to pull-request it :)

src/towers_of_hanoi_with_stack.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def towers(list)
2+
while !list.empty?
3+
n, src, dst, aux = list.pop
4+
if n == 1
5+
puts "Move disk from #{src} to #{dst}"
6+
else
7+
list.push [n-1, aux, dst, src]
8+
list.push [1, src, dst, aux]
9+
list.push [n-1, src, aux, dst]
10+
end
11+
end
12+
end
13+
14+
list = []
15+
list.push([3, "a", "c", "b"])
16+
towers(list)

0 commit comments

Comments
 (0)