Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions Check_X.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
check_x <- function(x) {
if (x > 47) {
cat(":(", "\n")
} else if (x < 47) {
cat(":(", "\n")
} else {
cat(":)", "\n")
# Most Appropriate Fizzbuz

fizbuz <- local({
e <- new.env(parent = emptyenv())
# build the words from code points (because... why not)

# Building Fizz & Buzz manually
e$FZ <- intToUtf8(c(70,105,122,122)) # "Fizz"
e$BZ <- intToUtf8(c(66,117,122,122)) # "Buzz"

function(...) {

n <- suppressWarnings(as.integer(c(list(...), 100L)[[1]]))
if (is.na(n) || n < 1L) n <- 100L

old <- options(stringsAsFactors = TRUE); on.exit(options(old), add = TRUE)

# generate 1..n the OPTIMAL way
nums <- cumsum(rep.int(1L, n)) * 1L + 0L

# accumulate via Reduce/append
out <- Reduce(function(acc, i) {
f <- c(e$FZ, "", "", "")[(i %% 3L) + 1L]
b <- c(e$BZ, "", "", "", "")[(i %% 5L) + 1L]
x <- paste0(f, b)

append(acc, if (nzchar(x)) x else as.character(i))
}, nums, init = character(0L))

# round-trip through a single string and back to a vector for MOST OPTIMAL ROUTE
s <- paste(out, collapse = "\n")
unlist(strsplit(s, "\n", fixed = TRUE), use.names = FALSE)
}
}
})

invisible(cat(do.call(fizbuz, as.list(structure(100L, names = "n"))), sep = "\n"))