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
# Please write a function that takes an array as an argument, and returns an array where any consecutive duplicated values are removed. | |
# | |
# E.g., [a,a,a,b,b,c,d,d,a,b,c,c] as input would return [a,b,c,d,a,b,c] | |
my_array <- c("a","a","a","b","b","c","d","d","a","b","c","c") | |
dedup_actions <- function(my_array){ | |
# Takes an array of values (e.g., actions), deduplicates list where consecutive duplicated values are removed. | |
if(any(!is.character(my_array))) stop("Your input array is not all 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
# Give primes | |
give_primes <- function(number) { | |
# Function that gives all the prime factors of an integer | |
if(!is.integer(number)) stop("Input is not an integer. Did you forget to concat 'L' to the number?") | |
# Find all the factors | |
find_factors <- function(number){ | |
factors <- c() | |
for(i in 1:number) { | |
if(number%%i==0) factors <- c(factors, i) |
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
# Fizzbuzz | |
numbers <- 1:100 | |
for(i in seq_along(numbers)) { | |
cat(numbers[i], "\n") | |
if(numbers[i]%%2 == 0) cat("fizz") | |
if(numbers[i]%%3 == 0) cat("buzz") | |
cat("\n") | |
} |
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
# Break date into components | |
anytime::addFormats(c("%Y年%m月%e日","%Y年%m月%d日","%y.%m.%d")) | |
date_components <- function(date) { | |
if(!is.null(date)) { | |
# If it's less than 4 characters, then it's garbage | |
if(str_length(date) < 4) { | |
out <- list(date = "garbage", | |
year = NA, | |
month = NA, |
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
# (Problem) Suppose you have two vectors of numbers in sorted order. You need to | |
# combine them to form a single vector that is sorted from lowest to highest value. | |
# You cannot use an existing sort() routine. Write a program to interleave the | |
# vectors and preserve sorted order. | |
# Test vectors | |
vec1<- c(3,4,1,6,3,2,8) | |
vec2 <- c(1, 2,3,6,7,21,67,9) | |
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
library(shiny) | |
library(progress) | |
n <- 1000 | |
foo <- function() { | |
pb <- progress_bar$new(total = n, clear = FALSE) | |
for(i in 1:n) { | |
pb$tick() | |
Sys.sleep(n/10000) | |
} |
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
word1 | |
---|---|
KAIFENG PINGMEI NEW CARBON MATERIALS TECHNOLOGY CO., LTD. |
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
library(elastic) | |
# Establish a connection | |
conn <- connect(host = "<URL>", | |
path = "", | |
user = "<USERNAME>", | |
pwd = "<PASSWORD>", | |
port = 1234, | |
transport_schema = "https") | |
# conn_cloud$ping() |
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
# Examples using progress bars inside of for and while loops | |
library(progress) | |
pb <- progress_bar$new( | |
format = " [:bar] :current/:total (:percent) eta: :eta", | |
total = length(LETTERS), clear = FALSE) | |
# Saving your results in a for loop and while loop incrementally is a good practice as eventually the code will error, but you can review your results up to the point of the error | |
my_letters <- list() |
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
## Error catching with both timeout and error catches | |
# Create a simple function | |
funct1 <- function(x, sleep_time) { | |
log(x) | |
Sys.sleep(sleep_time) | |
print("All done!") | |
} | |
funct1(x = 10, sleep_time = 1) # Works as expected | |
funct1(x = "foo", sleep_time = 1) # Errors as expected |
NewerOlder