Skip to content

Instantly share code, notes, and snippets.

@zedalaye
Created August 3, 2011 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zedalaye/1123874 to your computer and use it in GitHub Desktop.
Save zedalaye/1123874 to your computer and use it in GitHub Desktop.
Try to make a hash stack in Ruby
$ rbx stack.rb
[4, 8, 12, 16, 20, 24, 28, 32, 36]
[{:done=>[9, 0, 5, 9, 6, 5, 2, 0],
:last=>nil,
:cells=>[1, nil, 1, nil, nil, 1, 1, nil, nil, 1],
:values=>[]},
{:done=>[9, 0, 5, 9, 6, 5, 2, 0],
:last=>9,
:cells=>[1, nil, 1, nil, nil, 1, 1, nil, nil, 1],
:values=>[]},
{:done=>[9, 0, 5, 9, 6, 5, 2, 0],
:last=>0,
:cells=>[1, nil, 1, nil, nil, 1, 1, nil, nil, 1],
:values=>[]},
{:done=>[9, 0, 5, 9, 6, 5, 2, 0],
:last=>5,
:cells=>[1, nil, 1, nil, nil, 1, 1, nil, nil, 1],
:values=>[]},
{:done=>[9, 0, 5, 9, 6, 5, 2, 0],
:last=>9,
:cells=>[1, nil, 1, nil, nil, 1, 1, nil, nil, 1],
:values=>[]},
{:done=>[9, 0, 5, 9, 6, 5, 2, 0],
:last=>6,
:cells=>[1, nil, 1, nil, nil, 1, 1, nil, nil, 1],
:values=>[]},
{:done=>[9, 0, 5, 9, 6, 5, 2, 0],
:last=>5,
:cells=>[1, nil, 1, nil, nil, 1, 1, nil, nil, 1],
:values=>[]},
{:done=>[9, 0, 5, 9, 6, 5, 2, 0],
:last=>2,
:cells=>[1, nil, 1, nil, nil, 1, 1, nil, nil, 1],
:values=>[]},
{:done=>[9, 0, 5, 9, 6, 5, 2, 0],
:last=>0,
:cells=>[1, nil, 1, nil, nil, 1, 1, nil, nil, 1],
:values=>[]}]
#!/usr/bin/env ruby
require 'pp'
stack=[]
stack.push( { :cells => (1..9).map{|i| nil }, :done => [], :last => nil, :values => [] } )
while stack.count < 9
state = stack.last.dup
state[:last] = rand(10)
state[:cells][state[:last]] = 1
state[:done] << state[:last]
stack.push(state)
end
pp stack.map{|s| s.object_id}
pp stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment