Skip to content

Instantly share code, notes, and snippets.

@BroVic
Last active July 13, 2019 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BroVic/edb9bf1af375fb995b3084d181f0884e to your computer and use it in GitHub Desktop.
Save BroVic/edb9bf1af375fb995b3084d181f0884e to your computer and use it in GitHub Desktop.
R implementation of CS50x's Problem Set 6 - mario, more comfortable
# mario.R
printCharInSeq <- function(char, num) {
for (n in seq_len(num)) {
cat(char)
}
}
prompt <- "Height: "
repeat {
if (interactive()) {
height <- readline(prompt)
}
else {
cat(prompt)
height <- readLines("stdin", n = 1)
}
height <- suppressWarnings(as.numeric(height[1]))
if ((height > 0 && height < 9) && !is.na(height))
break
}
for (numblocks in seq_len(height)) {
spaces <- height - numblocks
printCharInSeq(" ", spaces)
printCharInSeq("#", numblocks)
printCharInSeq(" ", 2)
printCharInSeq("#", numblocks)
cat("\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment