Skip to content

Commit

Permalink
towers of hanoi
Browse files Browse the repository at this point in the history
  • Loading branch information
anjshenoy committed Apr 20, 2012
1 parent cad8c83 commit cb3171c
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 25 deletions.
162 changes: 137 additions & 25 deletions linked_list.rb
Expand Up @@ -60,6 +60,7 @@ def size
end
count
end
alias_method :number_of_nodes, :size

def exists?(node)
x = self.head
Expand All @@ -69,55 +70,166 @@ def exists?(node)
x == node
end


# delete a node given access only to that node
def delete(node)
x = self.head
until(x == node)
x = x.next
end
if (x == node)
next_node = x.next
x.data = next_node.data
x.next = next_node.next
next_node = node.next
if next_node.nil?
self.drop_last
else
node.data = next_node.data
node.next = next_node.next
end
self
end

def print
def to_s
x = self.head
str = ""
until(x == self.last)
x = x.next
str << "#{x.data} -> "
end
puts str << "nil"
str << "nil"
end

def print(from, to=self.last)
n = from
str = ""
until (n == to)
str << "#{n} -> "
n = n.next
end
puts str << to.to_s
end

def at(position=1)
n = self.head.next
count = 1
while (count < position && !n.nil?)
n = n.next
count += 1
end
n
end

def remove_duplicates
hash = {}
n = self.head.next
until(n.nil?) do
if hash.has_key?(n.data)
self.delete(n)
else
hash[n.data] = 1
end
n = n.next
end
self
end

def remove_duplicates_without_buffer
n = self.head.next
until(n.next == self.last) do
n1 = n.next
until(n1.nil?) do
if n.data == n1.data
if n1 == self.last
self.delete(n1)
break
else
self.delete(n1)
end
else
n1 = n1.next
end
end
n = n.next
end
self
end

def add_numbers_from_another_ll(ll)
x, y = self.head.next, ll.head.next
newll = LinkedList.new
carry_over = 0
while (x && y)
result = x + y + carry_over
if result >= 10
result -= 10
carry_over = 1
else
carry_over = 0
end
newll.push(result)
x = x.next
y = y.next
end
newll
end

def make_circle(start_position=1, end_position=self.size)
n = self.head.next
count = 1
while(count < start_position && !n.nil?)
n = n.next
count += 1
end
start_node = n
puts "start_node = #{start_node}"
while(count < end_position && !n.nil?)
n = n.next
count += 1
end
end_node = n
puts "end_node = #{end_node}"
end_node.next = start_node
puts "end.node.next = #{end_node.next}"
puts self
self
end

end

class Node
attr_accessor :data, :next

def initialize(data=nil)
def initialize(data="")
self.data = data
end

def to_s
self.data
end
end

#stack
l = LinkedList.new
l.push("a").push("x").push("c").print
puts l.pop
l.print
l = LinkedList.new
l.enqueue("a").enqueue("x").enqueue("c").print
puts l.dequeue
l.print



def ===(other)
self.data = other.data
end

def +(another_node)
self.data + another_node.data
end
end

##stack
#l = LinkedList.new
#puts l.push("a").push("x").push("c")
#puts l.pop
#puts l
l = LinkedList.new
l.enqueue("a").enqueue("x").enqueue("c")
#l.dequeue
l.enqueue("a").enqueue("b").enqueue("x")
puts l.make_circle(3, 6)
puts l.circular?
##puts l.remove_duplicates
##puts l.remove_duplicates_without_buffer
#puts l
#puts l.at(3)
#l.print(l.at(3))

#a = LinkedList.new
#a.push(3).push(8).push(6)
#puts a
#b = LinkedList.new.push(4).push(9).push(2)
#puts b
#puts a.add_numbers_from_another_ll(b)
#
73 changes: 73 additions & 0 deletions towers_of_hanoi.rb
@@ -0,0 +1,73 @@
class TowersOfHanoi
attr_accessor :disks, :towers
def initialize(number_of_disks=3, *tower_names)
self.towers = []
tower_names.each do |name|
self.towers << Tower.new(name)
end
self.towers.first.load((1..number_of_disks).to_a)
end


def tower_a
self.towers.first
end

def tower_b
self.towers[1]
end

def tower_c
self.towers.last
end

def to_s
"TOWER STATUS:: " + self.towers.map(&:to_s).join(" || ")
end

end

class Tower
attr_accessor :name, :disks

def initialize(name)
self.name = name
self.disks = []
end

def load(disks)
disks.each do |d|
self.disks.push(d)
end
end

def to_s
"#{self.name}: #{self.disks.inspect}"
end

def move(*disks, to, aux)
disks.flatten!
return if disks.empty?
puts "moving #{disks}, from: #{self.name} to: #{to.name}, aux: #{aux.name}, #{self}, #{to}, #{aux}"
if disks.size == 1
to.push(self.pop)
return
end
self.move([disks[0...disks.size-1]], aux, to)
self.move([disks.last], to, aux)
aux.move([disks[0...disks.size-1]], to, self)
end

def push(disk)
self.disks.insert(0, disk)
end

def pop
self.disks.delete_at(0)
end
end

h = TowersOfHanoi.new(3,"a","b","c")
h.tower_a.move(h.tower_a.disks, h.tower_c, h.tower_b)
puts h
puts "DONE!!!"

0 comments on commit cb3171c

Please sign in to comment.