Skip to content

Instantly share code, notes, and snippets.

@marcinwyszynski
Created February 24, 2012 10:02
Show Gist options
  • Save marcinwyszynski/1899895 to your computer and use it in GitHub Desktop.
Save marcinwyszynski/1899895 to your computer and use it in GitHub Desktop.
PriorityQueue

Description and usage:

require ''
.new

Running the tests

Install the rspec gem

gem install rspec

Run the spec

rspec _spec.rb
Gem::Specification.new do |s|
s.name = ''
s.summary = ''
s.description = ''
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY
s.files = ['.rb']
s.require_path = '.'
s.author = ''
s.email = ''
s.homepage = ''
s.test_file = '_spec.rb'
s.add_development_dependency('rspec', ["~> 2.8"])
end
class PriorityQueue
def initialize() @store = {} end
# Takes element and it's priority.
# Returns an array of elements with the same priority.
def enq(el, p)
@store[p] ? @store[p].push(el) : @store[p] = [el]
return @store[p]
end
# Returns the element/priority combination of the
# element with the highest priority from the queue
def deq
key = @store.keys.sort.last
el = @store[key].shift
@store.delete(key) if @store[key].empty?
return [el, key]
rescue NoMethodError
nil
end
end
require File.expand_path('')
describe do
it 'should have tests' do
pending
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment