Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
18
Task/Priority-queue/EchoLisp/priority-queue.echolisp
Normal file
18
Task/Priority-queue/EchoLisp/priority-queue.echolisp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
(lib 'tree)
|
||||
(define tasks (make-bin-tree 3 "Clear drains"))
|
||||
(bin-tree-insert tasks 2 "Tax return")
|
||||
(bin-tree-insert tasks 5 "Make tea")
|
||||
(bin-tree-insert tasks 1 "Solve RC tasks")
|
||||
(bin-tree-insert tasks 4 "Feed 🐡")
|
||||
|
||||
(bin-tree-pop-first tasks) → (1 . "Solve RC tasks")
|
||||
(bin-tree-pop-first tasks) → (2 . "Tax return")
|
||||
(bin-tree-pop-first tasks) → (3 . "Clear drains")
|
||||
(bin-tree-pop-first tasks) → (4 . "Feed 🐡")
|
||||
(bin-tree-pop-first tasks) → (5 . "Make tea")
|
||||
(bin-tree-pop-first tasks) → null
|
||||
|
||||
;; similarly
|
||||
(bin-tree-pop-last tasks) → (5 . "Make tea")
|
||||
(bin-tree-pop-last tasks) → (4 . "Feed 🐡")
|
||||
; etc.
|
||||
22
Task/Priority-queue/FunL/priority-queue.funl
Normal file
22
Task/Priority-queue/FunL/priority-queue.funl
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import util.ordering
|
||||
native scala.collection.mutable.PriorityQueue
|
||||
|
||||
data Task( priority, description )
|
||||
|
||||
def comparator( Task(a, _), Task(b, _) )
|
||||
| a > b = -1
|
||||
| a < b = 1
|
||||
| otherwise = 0
|
||||
|
||||
q = PriorityQueue( ordering(comparator) )
|
||||
|
||||
q.enqueue(
|
||||
Task(3, 'Clear drains'),
|
||||
Task(4, 'Feed cat'),
|
||||
Task(5, 'Make tea'),
|
||||
Task(1, 'Solve RC tasks'),
|
||||
Task(2, 'Tax return')
|
||||
)
|
||||
|
||||
while not q.isEmpty()
|
||||
println( q.dequeue() )
|
||||
59
Task/Priority-queue/Lasso/priority-queue.lasso
Normal file
59
Task/Priority-queue/Lasso/priority-queue.lasso
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
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)
|
||||
}
|
||||
60
Task/Priority-queue/Nim/priority-queue-1.nim
Normal file
60
Task/Priority-queue/Nim/priority-queue-1.nim
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
type
|
||||
PriElem[T] = tuple
|
||||
data: T
|
||||
pri: int
|
||||
|
||||
PriQueue[T] = object
|
||||
buf: seq[PriElem[T]]
|
||||
count: int
|
||||
|
||||
# first element not used to simplify indices
|
||||
proc initPriQueue[T](initialSize = 4): PriQueue[T] =
|
||||
result.buf.newSeq(initialSize)
|
||||
result.buf.setLen(1)
|
||||
result.count = 0
|
||||
|
||||
proc add[T](q: var PriQueue[T], data: T, pri: int) =
|
||||
var n = q.buf.len
|
||||
var m = n div 2
|
||||
q.buf.setLen(n + 1)
|
||||
|
||||
# append at end, then up heap
|
||||
while m > 0 and pri < q.buf[m].pri:
|
||||
q.buf[n] = q.buf[m]
|
||||
n = m
|
||||
m = m div 2
|
||||
|
||||
q.buf[n] = (data, pri)
|
||||
q.count = q.buf.len - 1
|
||||
|
||||
proc pop[T](q: var PriQueue[T]): PriElem[T] =
|
||||
assert q.buf.len > 1
|
||||
result = q.buf[1]
|
||||
|
||||
var qn = q.buf.len - 1
|
||||
var n = 1
|
||||
var m = 2
|
||||
while m < qn:
|
||||
if m + 1 < qn and q.buf[m].pri > q.buf[m+1].pri:
|
||||
inc m
|
||||
|
||||
if q.buf[qn].pri <= q.buf[m].pri:
|
||||
break
|
||||
|
||||
q.buf[n] = q.buf[m]
|
||||
n = m
|
||||
m = m * 2
|
||||
|
||||
q.buf[n] = q.buf[qn]
|
||||
q.buf.setLen(q.buf.len - 1)
|
||||
q.count = q.buf.len - 1
|
||||
|
||||
var p = initPriQueue[string]()
|
||||
p.add("Clear drains", 3)
|
||||
p.add("Feed cat", 4)
|
||||
p.add("Make tea", 5)
|
||||
p.add("Solve RC tasks", 1)
|
||||
p.add("Tax return", 2)
|
||||
|
||||
while p.count > 0:
|
||||
echo p.pop()
|
||||
18
Task/Priority-queue/Nim/priority-queue-2.nim
Normal file
18
Task/Priority-queue/Nim/priority-queue-2.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import tables
|
||||
|
||||
var
|
||||
pq = initTable[int, string]()
|
||||
|
||||
proc main() =
|
||||
pq.add(3, "Clear drains")
|
||||
pq.add(4, "Feed cat")
|
||||
pq.add(5, "Make tea")
|
||||
pq.add(1, "Solve RC tasks")
|
||||
pq.add(2, "Tax return")
|
||||
|
||||
for i in countUp(1,5):
|
||||
if pq.hasKey(i):
|
||||
echo i, ": ", pq[i]
|
||||
pq.del(i)
|
||||
|
||||
main()
|
||||
30
Task/Priority-queue/Sidef/priority-queue.sidef
Normal file
30
Task/Priority-queue/Sidef/priority-queue.sidef
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
class PriorityQueue {
|
||||
has tasks = []
|
||||
|
||||
method insert (Number priority { _ >= 0 }, task) {
|
||||
for n in range(tasks.len, priority) {
|
||||
tasks[n] = []
|
||||
}
|
||||
tasks[priority].append(task)
|
||||
}
|
||||
|
||||
method get { tasks.first { !.is_empty } -> shift }
|
||||
method is_empty { tasks.all { .is_empty } }
|
||||
}
|
||||
|
||||
var pq = PriorityQueue()
|
||||
|
||||
[
|
||||
[3, 'Clear drains'],
|
||||
[4, 'Feed cat'],
|
||||
[5, 'Make tea'],
|
||||
[9, 'Sleep'],
|
||||
[3, 'Check email'],
|
||||
[1, 'Solve RC tasks'],
|
||||
[9, 'Exercise'],
|
||||
[2, 'Do taxes'],
|
||||
].each { |pair|
|
||||
pq.insert(pair...)
|
||||
}
|
||||
|
||||
say pq.get while !pq.is_empty
|
||||
56
Task/Priority-queue/Swift/priority-queue.swift
Normal file
56
Task/Priority-queue/Swift/priority-queue.swift
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
class Task : Comparable, CustomStringConvertible {
|
||||
var priority : Int
|
||||
var name: String
|
||||
init(priority: Int, name: String) {
|
||||
self.priority = priority
|
||||
self.name = name
|
||||
}
|
||||
var description: String {
|
||||
return "\(priority), \(name)"
|
||||
}
|
||||
}
|
||||
func ==(t1: Task, t2: Task) -> Bool {
|
||||
return t1.priority == t2.priority
|
||||
}
|
||||
func <(t1: Task, t2: Task) -> Bool {
|
||||
return t1.priority < t2.priority
|
||||
}
|
||||
|
||||
struct TaskPriorityQueue {
|
||||
let heap : CFBinaryHeapRef = {
|
||||
var callBacks = CFBinaryHeapCallBacks(version: 0, retain: {
|
||||
UnsafePointer(Unmanaged<Task>.fromOpaque(COpaquePointer($1)).retain().toOpaque())
|
||||
}, release: {
|
||||
Unmanaged<Task>.fromOpaque(COpaquePointer($1)).release()
|
||||
}, copyDescription: nil, compare: { (ptr1, ptr2, _) in
|
||||
let t1 : Task = Unmanaged<Task>.fromOpaque(COpaquePointer(ptr1)).takeUnretainedValue()
|
||||
let t2 : Task = Unmanaged<Task>.fromOpaque(COpaquePointer(ptr2)).takeUnretainedValue()
|
||||
return t1 == t2 ? CFComparisonResult.CompareEqualTo : t1 < t2 ? CFComparisonResult.CompareLessThan : CFComparisonResult.CompareGreaterThan
|
||||
})
|
||||
return CFBinaryHeapCreate(nil, 0, &callBacks, nil)
|
||||
}()
|
||||
var count : Int { return CFBinaryHeapGetCount(heap) }
|
||||
mutating func push(t: Task) {
|
||||
CFBinaryHeapAddValue(heap, UnsafePointer(Unmanaged.passUnretained(t).toOpaque()))
|
||||
}
|
||||
func peek() -> Task {
|
||||
return Unmanaged<Task>.fromOpaque(COpaquePointer(CFBinaryHeapGetMinimum(heap))).takeUnretainedValue()
|
||||
}
|
||||
mutating func pop() -> Task {
|
||||
let result = Unmanaged<Task>.fromOpaque(COpaquePointer(CFBinaryHeapGetMinimum(heap))).takeUnretainedValue()
|
||||
CFBinaryHeapRemoveMinimumValue(heap)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
var pq = TaskPriorityQueue()
|
||||
|
||||
pq.push(Task(priority: 3, name: "Clear drains"))
|
||||
pq.push(Task(priority: 4, name: "Feed cat"))
|
||||
pq.push(Task(priority: 5, name: "Make tea"))
|
||||
pq.push(Task(priority: 1, name: "Solve RC tasks"))
|
||||
pq.push(Task(priority: 2, name: "Tax return"))
|
||||
|
||||
while pq.count != 0 {
|
||||
print(pq.pop())
|
||||
}
|
||||
49
Task/Priority-queue/jq/priority-queue-1.jq
Normal file
49
Task/Priority-queue/jq/priority-queue-1.jq
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# In the following, pq stands for "priority queue".
|
||||
|
||||
# Add an item with the given priority (an integer,
|
||||
# or a string representing an integer)
|
||||
# Input: a pq
|
||||
def pq_add(priority; item):
|
||||
(priority|tostring) as $p
|
||||
| if .priorities|index($p) then
|
||||
if (.[$p] | index(item)) then . else .[$p] += [item] end
|
||||
else .[$p] = [item] | .priorities = (.priorities + [$p] | sort)
|
||||
end ;
|
||||
|
||||
# emit [ item, pq ]
|
||||
# Input: a pq
|
||||
def pq_pop:
|
||||
.priorities as $keys
|
||||
| if ($keys|length) == 0 then [ null, . ]
|
||||
else
|
||||
if (.[$keys[0]] | length) == 1
|
||||
then .priorities = .priorities[1:]
|
||||
else .
|
||||
end
|
||||
| [ (.[$keys[0]])[0], (.[$keys[0]] = .[$keys[0]][1:]) ]
|
||||
end ;
|
||||
|
||||
# Emit the item that would be popped, or null if there is none
|
||||
# Input: a pq
|
||||
def pq_peep:
|
||||
.priorities as $keys
|
||||
| if ($keys|length) == 0 then null
|
||||
else (.[$keys[0]])[0]
|
||||
end ;
|
||||
|
||||
# Add a bunch of tasks, presented as an array of arrays
|
||||
# Input: a pq
|
||||
def pq_add_tasks(list):
|
||||
reduce list[] as $pair (.; . + pq_add( $pair[0]; $pair[1]) ) ;
|
||||
|
||||
# Pop all the tasks, producing a stream
|
||||
# Input: a pq
|
||||
def pq_pop_tasks:
|
||||
pq_pop as $pair
|
||||
| if $pair[0] == null then empty
|
||||
else $pair[0], ( $pair[1] | pq_pop_tasks )
|
||||
end ;
|
||||
|
||||
# Input: a bunch of tasks, presented as an array of arrays
|
||||
def prioritize:
|
||||
. as $list | {} | pq_add_tasks($list) | pq_pop_tasks ;
|
||||
6
Task/Priority-queue/jq/priority-queue-2.jq
Normal file
6
Task/Priority-queue/jq/priority-queue-2.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[ [3, "Clear drains"],
|
||||
[4, "Feed cat"],
|
||||
[5, "Make tea"],
|
||||
[1, "Solve RC tasks"],
|
||||
[2, "Tax return"]
|
||||
] | prioritize
|
||||
Loading…
Add table
Add a link
Reference in a new issue