Skip to content

Instantly share code, notes, and snippets.

@kennedymwavu
Created November 16, 2023 10:02
Show Gist options
  • Save kennedymwavu/498fab89815e0d82a5f7fdd4c6062c95 to your computer and use it in GitHub Desktop.
Save kennedymwavu/498fab89815e0d82a5f7fdd4c6062c95 to your computer and use it in GitHub Desktop.
Create a diamond pattern
n <- 10
num_of_stars_per_row_upper_sequence <- seq(from = 1, by = 2, length.out = n)
row_length <- num_of_stars_per_row_upper_sequence[n]
num_of_stars_per_row_lower_sequence <- rev(num_of_stars_per_row_upper_sequence)[-1]
upper_and_lower_sequence <- c(num_of_stars_per_row_upper_sequence, num_of_stars_per_row_lower_sequence)
for (i in upper_and_lower_sequence) {
num_of_spaces <- (row_length - i) / 2
spaces <- rep(" ", times = num_of_spaces)
stars <- rep("*", times = i)
cat(spaces, stars, "\n", sep = "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment