September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,7 @@
let myQueue = Queue()
myQueue.push 'foo'
myQueue.push 'bar'
myQueue.push 'baz'
print myQueue.pop() # => 'foo'
print myQueue.pop() # => 'bar'
print myQueue.pop() # => 'baz'

View file

@ -1,25 +1,24 @@
#import system.
#import system'collections.
#import extensions.
import system'collections.
import extensions.
#symbol program =
program =
[
// Create a queue and "push" items into it
#var queue := Queue new.
var queue := Queue new.
queue push:1.
queue push:3.
queue push:5.
// "Pop" items from the queue in FIFO order
console writeLine:(queue pop). // 1
console writeLine:(queue pop). // 3
console writeLine:(queue pop). // 5
console printLine(queue pop). // 1
console printLine(queue pop). // 3
console printLine(queue pop). // 5
// To tell if the queue is empty, we check the count
console writeLine:"queue is ":((queue length == 0) iif:"empty":"nonempty").
console printLine("queue is ",(queue length == 0) iif("empty","nonempty")).
// If we try to pop from an empty queue, an exception
// is thrown.
queue pop | if &Error: e [ console writeLine:"Queue empty.". ].
queue pop | if(:e)[ console writeLine:"Queue empty.". ].
].

View file

@ -0,0 +1,10 @@
defmodule Queue do
def empty?([]), do: true
def empty?(_), do: false
def pop([h|t]), do: {h,t}
def push(q,t), do: q ++ [t]
def front([h|_]), do: h
end

View file

@ -0,0 +1,14 @@
iex(2)> q = [1,2,3,4,5]
[1, 2, 3, 4, 5]
iex(3)> Queue.push(q,10)
[1, 2, 3, 4, 5, 10]
iex(4)> front=Queue.front(q)
1
iex(5)> Queue.empty?(q)
false
iex(6)> Queue.pop(q)
{1, [2, 3, 4, 5]}
iex(7)> l=[]
[]
iex(8)> Queue.empty?(l)
true

View file

@ -0,0 +1,28 @@
' FB 1.05.0 Win64
#Include "queue_rosetta.bi" '' include macro-based generic Queue type used in earlier task
Declare_Queue(String) '' expand Queue type for Strings
Dim stringQueue As Queue(String)
With stringQueue '' push some strings into the Queue
.push("first")
.push("second")
.push("third")
.push("fourth")
.push("fifth")
End With
Print "Number of Strings in the Queue :" ; stringQueue.count
Print "Capacity of string Queue :" ; stringQueue.capacity
Print
' now pop them
While Not stringQueue.empty
Print stringQueue.pop(); " popped"
Wend
Print
Print "Number of Strings in the Queue :" ; stringQueue.count
Print "Capacity of string Queue :" ; stringQueue.capacity '' capacity should be unchanged
Print "Is Queue empty now : "; stringQueue.empty
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,22 @@
// version 1.1.2
import java.util.*
fun main(args: Array<String>) {
val q: Queue<Int> = ArrayDeque<Int>()
(1..5).forEach { q.add(it) }
println(q)
println("Size of queue = ${q.size}")
print("Removing: ")
(1..3).forEach { print("${q.remove()} ") }
println("\nRemaining in queue: $q")
println("Head element is now ${q.element()}")
q.clear()
println("After clearing, queue is ${if(q.isEmpty()) "empty" else "not empty"}")
try {
q.remove()
}
catch (e: NoSuchElementException) {
println("Can't remove elements from an empty queue")
}
}

View file

@ -1,12 +0,0 @@
import queues
var deq: TQueue[int] = initQueue[int]()
deq.enqueue(26)
deq.add(99) # same as enqueue()
deq.enqueue(2)
echo("Dequeue size: ", deq.len())
echo("De-queue: ", deq.dequeue())
echo("De-queue: ", deq.dequeue())
echo("De-queue: ", deq.dequeue())
#echo("De-queue: ", deq.dequeue()) # dequeue an empty dequeue raises [EAssertionFailed]

View file

@ -0,0 +1,5 @@
q = .queue~new -- create an instance
q~queue(3) -- adds to the end, but this is at the front
q~push(1) -- push on the front
q~queue(2) -- add to the end
say q~pull q~pull q~pull q~isempty -- should display all and be empty