September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

205
Task/Stack/ABAP/stack.abap Normal file
View file

@ -0,0 +1,205 @@
report z_stack.
interface stack.
methods:
push
importing
new_element type any
returning
value(new_stack) type ref to stack,
pop
exporting
top_element type any
returning
value(new_stack) type ref to stack,
empty
returning
value(is_empty) type abap_bool,
peek
exporting
top_element type any,
get_size
returning
value(size) type int4,
stringify
returning
value(stringified_stack) type string.
endinterface.
class character_stack definition.
public section.
interfaces:
stack.
methods:
constructor
importing
characters type string optional.
private section.
data:
characters type string.
endclass.
class character_stack implementation.
method stack~push.
characters = |{ new_element }{ characters }|.
new_stack = me.
endmethod.
method stack~pop.
if not me->stack~empty( ).
top_element = me->characters(1).
me->characters = me->characters+1.
endif.
new_stack = me.
endmethod.
method stack~empty.
is_empty = xsdbool( strlen( me->characters ) eq 0 ).
endmethod.
method stack~peek.
check not me->stack~empty( ).
top_element = me->characters(1).
endmethod.
method stack~get_size.
size = strlen( me->characters ).
endmethod.
method stack~stringify.
stringified_stack = cond string(
when me->stack~empty( )
then `empty`
else me->characters ).
endmethod.
method constructor.
check characters is not initial.
me->characters = characters.
endmethod.
endclass.
class integer_stack definition.
public section.
interfaces:
stack.
methods:
constructor
importing
integers type int4_table optional.
private section.
data:
integers type int4_table.
endclass.
class integer_stack implementation.
method stack~push.
append new_element to me->integers.
new_stack = me.
endmethod.
method stack~pop.
if not me->stack~empty( ).
top_element = me->integers[ me->stack~get_size( ) ].
delete me->integers index me->stack~get_size( ).
endif.
new_stack = me.
endmethod.
method stack~empty.
is_empty = xsdbool( lines( me->integers ) eq 0 ).
endmethod.
method stack~peek.
check not me->stack~empty( ).
top_element = me->integers[ lines( me->integers ) ].
endmethod.
method stack~get_size.
size = lines( me->integers ).
endmethod.
method stack~stringify.
stringified_stack = cond string(
when me->stack~empty( )
then `empty`
else reduce string(
init stack = ``
for integer in me->integers
next stack = |{ integer }{ stack }| ) ).
endmethod.
method constructor.
check integers is not initial.
me->integers = integers.
endmethod.
endclass.
start-of-selection.
data:
stack1 type ref to stack,
stack2 type ref to stack,
stack3 type ref to stack,
top_character type char1,
top_integer type int4.
stack1 = new character_stack( ).
stack2 = new integer_stack( ).
stack3 = new integer_stack( ).
write: |Stack1 = { stack1->stringify( ) }|, /.
stack1->push( 'a' )->push( 'b' )->push( 'c' )->push( 'd' ).
write: |push a, push b, push c, push d -> Stack1 = { stack1->stringify( ) }|, /.
stack1->pop( )->pop( importing top_element = top_character ).
write: |pop, pop and return element -> { top_character }, Stack1 = { stack1->stringify( ) }|, /, /.
write: |Stack2 = { stack2->stringify( ) }|, /.
stack2->push( 1 )->push( 2 )->push( 3 )->push( 4 ).
write: |push 1, push 2, push 3, push 4 -> Stack2 = { stack2->stringify( ) }|, /.
stack2->pop( )->pop( importing top_element = top_integer ).
write: |pop, pop and return element -> { top_integer }, Stack2 = { stack2->stringify( ) }|, /, /.
write: |Stack3 = { stack3->stringify( ) }|, /.
stack3->pop( ).
write: |pop -> Stack3 = { stack3->stringify( ) }|, /, /.

View file

@ -1,9 +1,12 @@
var stack := system'collections'Stack new.
public program()
{
var stack := new system'collections'Stack();
stack push:2.
stack.push:2;
var isEmpty := stack length == 0.
var isEmpty := stack.Length == 0;
var item := stack peek. // Peek without Popping.
var item := stack.peek(); // Peek without Popping.
item := stack pop.
item := stack.pop()
}

View file

@ -0,0 +1,23 @@
program Stack;
{$IFDEF FPC}{$MODE DELPHI}{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}{$ENDIF}
{$ASSERTIONS ON}
uses Generics.Collections;
var
lStack: TStack<Integer>;
begin
lStack := TStack<Integer>.Create;
try
lStack.Push(1);
lStack.Push(2);
lStack.Push(3);
Assert(lStack.Peek = 3); // 3 should be at the top of the stack
Write(lStack.Pop:2); // 3
Write(lStack.Pop:2); // 2
Writeln(lStack.Pop:2); // 1
Assert(lStack.Count = 0, 'Stack is not empty'); // should be empty
finally
lStack.Free;
end;
end.

View file

@ -5,7 +5,7 @@ class ['a] stack =
val mutable lst : 'a list = []
method push x =
lst <- x::lst
lst <- x::lst
method pop =
match lst with

View file

@ -1,5 +1,5 @@
#lang racket
(define (stack) '())
(define stack '())
(define (push x stack) (cons x stack))
(define (pop stack) (values (car stack) (cdr stack)))
(define (empty? stack) (null? stack))

View file

@ -0,0 +1,11 @@
require 'forwardable'
class Stack
extend Forwardable
def initialize
@stack = []
end
def_delegators :@stack, :push, :pop, :empty?
end

View file

@ -0,0 +1,39 @@
' Stack Definition - VBScript
Option Explicit
Dim stack, i, x
Set stack = CreateObject("System.Collections.ArrayList")
If Not empty_(stack) Then Wscript.Echo stack.Count
push stack, "Banana"
push stack, "Apple"
push stack, "Pear"
push stack, "Strawberry"
Wscript.Echo "Count=" & stack.Count ' --> Count=4
Wscript.Echo pop(stack) & " - Count=" & stack.Count ' --> Strawberry - Count=3
Wscript.Echo "Tail=" & stack.Item(0) ' --> Tail=Banana
Wscript.Echo "Head=" & stack.Item(stack.Count-1) ' --> Head=Pear
Wscript.Echo stack.IndexOf("Apple", 0) ' --> 1
For i=1 To stack.Count
Wscript.Echo join(stack.ToArray(), ", ")
x = pop(stack)
Next 'i
Sub push(s, what)
s.Add what
End Sub 'push
Function pop(s)
Dim what
If s.Count > 0 Then
what = s(s.Count-1)
s.RemoveAt s.Count-1
Else
what = ""
End If
pop = what
End Function 'pop
Function empty_(s)
empty_ = s.Count = 0
End Function 'empty_