Skip to content

Commit b1f0cf7

Browse files
committed
Towers of Hanoi using Stack
1 parent aca7bb1 commit b1f0cf7

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

README.md

Lines changed: 7 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

@@ -48,13 +48,15 @@ This is my way of saying - change your interview style. There are lots of smart
4848
| [Eucliden and Extended Eucliden algorithm](http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm) | [click](https://github.com/sagivo/algorithms/blob/master/src/gcd.rb) |
4949
| Suggest index of a number in an array | [click](https://github.com/sagivo/algorithms/blob/master/src/sugget_index_in_array.rb) |
5050
| [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) |
51-
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [click](https://github.com/sagivo/algorithms/blob/master/src/insertion_sort.rb) |
51+
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [click](https://github.com/sagivo/algorithms/blob/master/src/insertion_sort.rb)
52+
53+
| [Towers of Hanoi using Stack](https://en.wikipedia.org/wiki/Tower_of_Hanoi) | [click](blob/master/src/towers_of_hanoi_with_stack.rb) |
5254

5355

5456
# How?
5557
Simply run `ruby some_file.rb` to execute the algorithm. At the bottom of each file there are some test samples.
5658
example: `ruby brackets_combinations.rb` will print:
5759
`["((()))", "(()())", "(())()", "()(())", "()()()"]`.
5860

59-
# Contribute
61+
# Contribute
6062
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)