How to resolve the algorithm Priority queue step by step in the Lasso programming language
How to resolve the algorithm Priority queue step by step in the Lasso programming language
Table of Contents
Problem Statement
A priority queue is somewhat similar to a queue, with an important distinction: each item is added to a priority queue with a priority level, and will be later removed from the queue with the highest priority element first. That is, the items are (conceptually) stored in the queue in priority order instead of in insertion order.
Create a priority queue. The queue must support at least two operations:
Optionally, other operations may be defined, such as peeking (find what current top priority/top element is), merging (combining two priority queues into one), etc.
To test your implementation, insert a number of elements into the queue, each with some random priority. Then dequeue them sequentially; now the elements should be sorted by priority. You can use the following task/priority items as input data:
The implementation should try to be efficient. A typical implementation has O(log n) insertion and extraction time, where n is the number of items in the queue.
You may choose to impose certain limits such as small range of allowed priority levels, limited capacity, etc. If so, discuss the reasons behind it.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Priority queue step by step in the Lasso programming language
Source code in the lasso programming language
define priorityQueue => type {
data
store = map,
cur_priority = void
public push(priority::integer, value) => {
local(store) = .`store`->find(#priority)
if(#store->isA(::array)) => {
#store->insert(#value)
return
}
.`store`->insert(#priority=array(#value))
.`cur_priority`->isA(::void) or #priority < .`cur_priority`
? .`cur_priority` = #priority
}
public pop => {
.`cur_priority` == void
? return void
local(store) = .`store`->find(.`cur_priority`)
local(retVal) = #store->first
#store->removeFirst&size > 0
? return #retVal
// Need to find next priority
.`store`->remove(.`cur_priority`)
if(.`store`->size == 0) => {
.`cur_priority` = void
else
// There are better / faster ways to do this
// The keys are actually already sorted, but the order of
// storage in a map is not actually defined, can't rely on it
.`cur_priority` = .`store`->keys->asArray->sort&first
}
return #retVal
}
public isEmpty => (.`store`->size == 0)
}
local(test) = priorityQueue
#test->push(2,`e`)
#test->push(1,`H`)
#test->push(5,`o`)
#test->push(2,`l`)
#test->push(5,`!`)
#test->push(4,`l`)
while(not #test->isEmpty) => {
stdout(#test->pop)
}
You may also check:How to resolve the algorithm Combinations step by step in the Quackery programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the RPL programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Count in octal step by step in the zig programming language
You may also check:How to resolve the algorithm Empty string step by step in the C# programming language