tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
48
Task/Queue-Definition/R/queue-definition-1.r
Normal file
48
Task/Queue-Definition/R/queue-definition-1.r
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
empty <- function() length(l) == 0
|
||||
push <- function(x)
|
||||
{
|
||||
l <<- c(l, list(x))
|
||||
print(l)
|
||||
invisible()
|
||||
}
|
||||
pop <- function()
|
||||
{
|
||||
if(empty()) stop("can't pop from an empty list")
|
||||
l[[1]] <<- NULL
|
||||
print(l)
|
||||
invisible()
|
||||
}
|
||||
l <- list()
|
||||
empty()
|
||||
# [1] TRUE
|
||||
push(3)
|
||||
# [[1]]
|
||||
# [1] 3
|
||||
push("abc")
|
||||
# [[1]]
|
||||
# [1] 3
|
||||
# [[2]]
|
||||
# [1] "abc"
|
||||
push(matrix(1:6, nrow=2))
|
||||
# [[1]]
|
||||
# [1] 3
|
||||
# [[2]]
|
||||
# [1] "abc"
|
||||
# [[3]]
|
||||
# [,1] [,2] [,3]
|
||||
# [1,] 1 3 5
|
||||
# [2,] 2 4 6
|
||||
empty()
|
||||
# [1] FALSE
|
||||
pop()
|
||||
# [[1]]
|
||||
# [1] 3
|
||||
# [[2]]
|
||||
# [1] "abc"
|
||||
pop()
|
||||
# [[1]]
|
||||
# [1] 3
|
||||
pop()
|
||||
# list()
|
||||
pop()
|
||||
# Error in pop() : can't pop from an empty list
|
||||
39
Task/Queue-Definition/R/queue-definition-2.r
Normal file
39
Task/Queue-Definition/R/queue-definition-2.r
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# The usual Scheme way : build a function that takes commands as parameters (it's like message passing oriented programming)
|
||||
queue <- function() {
|
||||
v <- list()
|
||||
f <- function(cmd, val=NULL) {
|
||||
if(cmd == "push") {
|
||||
v <<- c(v, val)
|
||||
invisible()
|
||||
} else if(cmd == "pop") {
|
||||
if(length(v) == 0) {
|
||||
stop("empty queue")
|
||||
} else {
|
||||
x <- v[[1]]
|
||||
v[[1]] <<- NULL
|
||||
x
|
||||
}
|
||||
} else if(cmd == "length") {
|
||||
length(v)
|
||||
} else if(cmd == "empty") {
|
||||
length(v) == 0
|
||||
} else {
|
||||
stop("unknown command")
|
||||
}
|
||||
}
|
||||
f
|
||||
}
|
||||
|
||||
# Create two queues
|
||||
a <- queue()
|
||||
b <- queue()
|
||||
a("push", 1)
|
||||
a("push", 2)
|
||||
b("push", 3)
|
||||
a("push", 4)
|
||||
b("push", 5)
|
||||
|
||||
a("pop")
|
||||
# [1] 1
|
||||
b("pop")
|
||||
# [1] 3
|
||||
30
Task/Queue-Definition/R/queue-definition-3.r
Normal file
30
Task/Queue-Definition/R/queue-definition-3.r
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
library(proto)
|
||||
|
||||
fifo <- proto(expr = {
|
||||
l <- list()
|
||||
empty <- function(.) length(.$l) == 0
|
||||
push <- function(., x)
|
||||
{
|
||||
.$l <- c(.$l, list(x))
|
||||
print(.$l)
|
||||
invisible()
|
||||
}
|
||||
pop <- function(.)
|
||||
{
|
||||
if(.$empty()) stop("can't pop from an empty list")
|
||||
.$l[[1]] <- NULL
|
||||
print(.$l)
|
||||
invisible()
|
||||
}
|
||||
})
|
||||
|
||||
#The following code provides output that is the same as the previous example.
|
||||
fifo$empty()
|
||||
fifo$push(3)
|
||||
fifo$push("abc")
|
||||
fifo$push(matrix(1:6, nrow=2))
|
||||
fifo$empty()
|
||||
fifo$pop()
|
||||
fifo$pop()
|
||||
fifo$pop()
|
||||
fifo$pop()
|
||||
Loading…
Add table
Add a link
Reference in a new issue