-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaddBorder.R
More file actions
executable file
·32 lines (30 loc) · 899 Bytes
/
addBorder.R
File metadata and controls
executable file
·32 lines (30 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Given a rectangular matrix of characters, add a border of asterisks(*) to it.
#
# Example
#
# For
#
# picture = ["abc",
# "ded"]
#
# the output should be
#
# addBorder(picture) = ["*****",
# "*abc*",
# "*ded*",
# "*****"]
# picture = list("abc",
# "ded")
addBorder <- function(picture) {
pictureLength <- nchar(picture[[1]])
pictureWidth <- length(picture)
#adding a border on each size would mean adding each of the dimensions by 2 extra pixels
OutputPicture <- list()
OutputPicture[[1]] <- paste(replicate(pictureLength + 2,"*"),collapse = "")
MidData <- lapply(picture, function(x) {
return(paste0("*",x,"*"))
})
OutputPicture <- c(OutputPicture,MidData)
OutputPicture[[pictureWidth + 2]] <- paste(replicate(pictureLength + 2,"*"),collapse = "")
return(OutputPicture)
}