-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new is_odd(), is_even() and seq_surnames() functions
- Loading branch information
1 parent
b20c77f
commit 384235f
Showing
7 changed files
with
147 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#################################################################### | ||
#' Family Tree and Surnames Sequence | ||
#' | ||
#' @param n Integer. Number of generations. Notice this will generate sequences | ||
#' of 2^n integer values. | ||
#' @return Integer vector. | ||
#' @examples | ||
#' seq_surnames(1) # Expected: 1 | ||
#' seq_surnames(2) # Expected: 1, 2 | ||
#' seq_surnames(3) # Expected: 1, 5, 3, 7, 2, 6, 4, 8 | ||
#' seq_surnames(4) # Expected: 1, 9, 5, 13, 3, 11, 7, 15, 2, 10, 6, 14, 4, 12, 8, 16 | ||
#' @export | ||
seq_surnames <- function(n = 1) { | ||
stopifnot(!is.integer(n)) | ||
ni <- n - 1 # We calculate only one and then sum 1 to the other half | ||
for (k in seq(2^ni)) { | ||
# Always start with 1 | ||
if (k == 1) { | ||
vals <- NULL | ||
vals[1] <- 1 | ||
} else { | ||
# Handle the specific case for n = 2 | ||
if (n == 2) { | ||
vals[k] <- k | ||
} else { | ||
# Generate the first 4 values of seq for larger n | ||
if (k %in% seq(4)) { | ||
if (is_odd(k)) { | ||
vals[k] <- vals[k - 1] - 2^(ni - 1) | ||
} else { | ||
vals[k] <- vals[k - 1] + 2^(ni) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
# Now sum the rest of the sequence based on vals | ||
if (n >= 3) { | ||
for (i in (n - 2):1) { | ||
vals <- c(vals, vals + 2^(i - 1)) | ||
} | ||
} | ||
return(vals) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.