Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

14
Task/Stack/Axe/stack.axe Normal file
View file

@ -0,0 +1,14 @@
0→S
Lbl PUSH
r₁→{L₁+S}ʳ
S+2→S
Return
Lbl POP
S-2→S
{L₁+S}ʳ
Return
Lbl EMPTY
S≤≤0
Return

View file

@ -0,0 +1,20 @@
; build stack [0 1 ... 9 (top)] from a list
(list->stack (iota 10) 'my-stack)
(stack-top 'my-stack) → 9
(pop 'my-stack) → 9
(stack-top 'my-stack) → 8
(push 'my-stack '🐸) ; any kind of lisp object in the stack
(stack-empty? 'my-stack) → #f
(stack->list 'my-stack) ; convert stack to list
→ (0 1 2 3 4 5 6 7 8 🐸)
(stack-swap 'my-stack) ; swaps two last items
→ 8 ; new top
(stack->list 'my-stack)
→ (0 1 2 3 4 5 6 7 🐸 8) ; swapped
(while (not (stack-empty? 'my-stack)) (pop 'my-stack)) ; pop until empty
(stack-empty? 'my-stack) → #t ; true
(push 'my-stack 7)
my-stack ; a stack is not a variable, nor a symbol - cannot be evaluated
⛔ error: #|user| : unbound variable : my-stack
(stack-top 'my-stack) → 7

View file

@ -0,0 +1,10 @@
local(a) = array
#a->push('a')
#a->push('b')
#a->push('c')
#a->pop // c
#a->pop // b
#a->pop // a
#a->pop // null

View file

@ -0,0 +1,23 @@
-- parent script "Stack"
property _tos
on push (me, data)
me._tos = [#data:data, #next:me._tos]
end
on pop (me)
if voidP(me._tos) then return VOID
data = me._tos.data
me._tos = me._tos.next
return data
end
on peek (me)
if voidP(me._tos) then return VOID
return me._tos.data
end
on empty (me)
return voidP(me.peek())
end

73
Task/Stack/Nim/stack.nim Normal file
View file

@ -0,0 +1,73 @@
import math
type
EStackEmpty = object of E_Base
TStack* [A] = object
data: seq[A]
count: int
proc initStack*[A](initialSize = 32): TStack[A] =
assert isPowerOfTwo(initialSize)
result.count = 0
newSeq(result.data,initialSize)
proc cap*[A] (s: TStack[A]): int =
result = s.data.len
proc len*[A](stack: TStack[A]): int =
result = stack.count
proc push*[A](s: var TStack[A], item: A) =
if s.count == s.data.len:
# not enough room, make container bigger
var d: Seq[A]
newSeq(d,s.len * 2)
for i in 0 .. s.data.len - 1:
shallowCopy(d[i],s.data[i])
shallowCopy(s.data,d)
s.data[s.count] = item
inc(s.count)
proc pop*[A](s: var TStack[A]): A {.raises: [EStackEmpty].}=
if s.count == 0:
raise newException(EStackEmpty,"the stack is empty")
dec(s.count)
result = s.data[s.count]
proc top*[A](s: TStack[A]): A =
result = s.data[s.count - 1]
proc isEmpty*[A](s: var TStack[A]): bool =
return s.count == 0
#Tests
when isMainModule:
var stk: TStack[char] = initStack[char](4)
stk.push('a')
stk.push('b')
stk.push('c')
stk.push('d')
assert(stk.count == 4)
assert(stk.data.len == 4)
stk.push('e')
assert(stk.cap == 8)
assert(stk.top == 'e')
discard stk.pop
discard stk.pop
discard stk.pop
discard stk.pop
assert(stk.isEmpty == false)
discard stk.pop
assert(stk.isEmpty == true)
try:
discard stk.pop
except:
let
e = getCurrentException()
msg = getCurrentExceptionMsg()
echo "Exception: [[", repr(e), "]] msg: ", msg

View file

@ -0,0 +1,4 @@
ListBuffer Class new: Stack
Stack method: push self add ;
Stack method: pop self removeLast ;
Stack method: top self last ;

View file

@ -0,0 +1,11 @@
: testStack
| s |
Stack new ->s
s push(10)
s push(11)
s push(12)
s top println
s pop println
s pop println
s pop println
s isEmpty ifTrue: [ "Stack is empty" println ] ;

View file

@ -0,0 +1,23 @@
sequence stack = {}
procedure push(object what)
stack = append(stack,what)
end procedure
function pop()
object what = stack[$]
stack = stack[1..$-1]
return what
end function
function empty()
return length(stack)=0
end function
?empty() -- 1
push(5)
?empty() -- 0
push(6)
?pop() -- 6
?pop() -- 5
?empty() -- 1

View file

@ -0,0 +1,26 @@
function push(sequence stack, object what)
stack = append(stack,what)
return stack
end function
function pop(sequence stack)
object what = stack[$]
stack = stack[1..$-1]
return {stack,what}
end function
function empty(sequence stack)
return length(stack)=0
end function
sequence stack = {}
?empty(stack) -- 1
stack = push(stack,5)
?empty(stack) -- 0
stack = push(stack,6)
integer top
{stack,top} = pop(stack)
?top -- 6
{stack,top} = pop(stack)
?top -- 5
?empty(stack) -- 1

View file

@ -0,0 +1,43 @@
sequence stacks = {}
integer freelist = 0
function new_stack()
integer res = freelist
if res!=0 then
freelist = stacks[freelist]
stacks[res] = {}
else
stacks = append(stacks,{})
res = length(stacks)
end if
return res
end function
procedure free_stack(integer sid)
stacks[sid] = freelist
freelist = sid
end procedure
procedure push(integer sid, object what)
stacks[sid] = append(stacks[sid],what)
end procedure
function pop(integer sid)
object res = stacks[sid][$]
stacks[sid] = stacks[sid][1..$-1]
return res
end function
function empty(integer sid)
return length(stacks[sid])=0
end function
integer sid = new_stack()
?empty(sid) -- 1
push(sid,5)
?empty(sid) -- 0
push(sid,6)
?pop(sid) -- 6
?pop(sid) -- 5
?empty(sid) -- 1
free_stack(sid)

View file

@ -0,0 +1,4 @@
var stack = [];
stack.push(42); # pushing
say stack.pop; # popping
say stack.is_empty; # is_emtpy?

View file

@ -0,0 +1,10 @@
class Stack(stack=[]) {
method pop { stack.pop };
method push(item) { stack.push(item) };
method empty { stack.is_empty };
}
var stack = Stack();
stack.push(42);
say stack.pop; # => 42
say stack.empty; # => true

View file

@ -0,0 +1,26 @@
struct Stack<T> {
var items = [T]()
var empty:Bool {
return items.count == 0
}
func peek() -> T {
return items[items.count - 1]
}
mutating func pop() -> T {
return items.removeLast()
}
mutating func push(obj:T) {
items.append(obj)
}
}
var stack = Stack<Int>()
stack.push(1)
stack.push(2)
println(stack.pop())
println(stack.peek())
stack.pop()
println(stack.empty)

View file

@ -0,0 +1,11 @@
def (stack)
(tag 'stack nil)
mac (push! x s) :qcase `(isa stack ,s)
`(push! ,x (rep ,s))
mac (pop! s) :qcase `(isa stack ,s)
`(pop! (rep ,s))
def (empty? s) :case (isa stack s)
(empty? rep.s)

View file

@ -0,0 +1,17 @@
(define-class stack
(instance-variables vals))
(define-method (stack 'initialize)
(setq vals '())
self)
(define-method (stack 'push x)
(setq vals (cons x vals)))
(define-method (stack 'pop)
(define tos (car vals))
(setq vals (cdr vals))
tos)
(define-method (stack 'emptyp)
(null vals))

View file

@ -0,0 +1,23 @@
; Loading 'stack.lsp'
[1] (define st (stack 'new))
ST
[2] (st 'push 1)
(1)
[3] (st 'push 2)
(2 1)
[4] (st 'emptyp)
()
[5] (st 'pop)
2
[6] (st 'pop)
1
[7] (st 'emptyp)
#T
[8]