Course Outline
-
segmentLearnosity
-
segmentCKCode
-
ckcode-chapter-a1-welcome-and-r
-
list Items Test Book
Book
ckcode ⌲ chapter-a1-welcome-and-r
print("Hello world!")
print("Hello world!")
ex() %>% check_function("print") %>% check_arg("x") %>% check_equal()
CK Code: ch1-1
sum(1,5,10)
sum(1,5,10)
ex() %>%
check_function("sum") %>%
check_arg("...", arg_not_specified_msg = "Make sure you don't delete what's inside the parentheses.") %>%
check_equal(incorrect_msg = "Make sure you don't change what's inside the parentheses.")
CK Code: ch1-2
# a few basic arithmetic things
5 + 1
10 - 3
2 * 4
9 / 3
# a few basic arithmetic things
5 + 1
10 - 3
2 * 4
9 / 3
ex() %>% {
check_operator(., "+") %>% check_result() %>% check_equal()
check_operator(., "-") %>% check_result() %>% check_equal()
check_operator(., "*") %>% check_result() %>% check_equal()
check_operator(., "/") %>% check_result() %>% check_equal()
}
CK Code: ch1-3
require(coursekata)
# type whatever you want
# see... blah blah blah
# no solution, but need code to show submit button
ex() %>% check_code("#", fixed = TRUE)
CK Code: ch1-0
# Use the sum() function to add the numbers 5, 10, 15
# Use the print() function to print the word "hello"
# Use the sum() function to add the numbers 5, 10, 15
sum(5, 10, 15)
# Use the print() function to print the word "hello"
print("hello")
ex() %>% {
check_function(., 'sum') %>% {
check_arg(., "...") %>% check_equal()
check_result(.) %>% check_equal()
}
check_function(., 'print') %>% check_result() %>% check_equal()
}
CK Code: ch1-4
# Run the code below by pressing Run
# Now debug the code - fix the mistake and press Run
Sum(1,2)
# Try running the code below by pressing Run
# Now try debugging the code - fix the mistake press Run again
sum(1,2)
ex() %>% check_function('sum') %>% check_result() %>% check_equal()
CK Code: ch1-5
# Try running this code that has left off the parenthesis at the end
# Now fix the code (by adding the closing parenthesis) and Run again
sum(50, 100
sum(50,100)
ex() %>% check_function('sum') %>% check_result() %>% check_equal()
ex() %>% check_error()
CK Code: ch1-6
require(coursekata)
# Try running rflip() to see what it does.
rflip()
# Try running rflip() to see what it does.
rflip()
ex() %>% check_function("rflip")
CK Code: ch1-7
require(coursekata)
# Modify this code to simulate 10 coin flips.
rflip()
# Modify this code to simulate 10 coin flips.
rflip(n = 10)
ex() %>% check_function('rflip')%>% check_arg('n') %>% check_equal()
CK Code: ch1-8
# This code will assign the number 20 to the R object my_favorite_number
my_favorite_number <- 20
# This code prints out my_favorite_number. Notice that you don't need to use the print() function to print the contents of an R object; you can just type the name of the object
my_favorite_number
# now revise the code to use your actual favorite number.
# This is an example --- use your favorite number!
my_favorite_number <- 17
my_favorite_number
ex() %>% {
check_object(., "my_favorite_number")
check_output_expr(., "my_favorite_number")
}
CK Code: ch1-9
# Assign 5 to num and 10 to NUM
num <-
NUM <-
# Write the name of the object that contains 10 and then press the <Run> button
# Doing so prints out the contents of that object
num <- 5
NUM <- 10
NUM
msg_undefined <- "Make sure to define both variables: num and NUM."
msg_incorrect <- "Make sure you assign the correct value to each variable."
msg_not_print <- "Don't forget to print out the object that contains 10."
ex() %>% {
check_object(., 'num', msg_undefined) %>% check_equal(msg_incorrect)
check_object(., 'NUM', msg_undefined) %>% check_equal(msg_incorrect)
check_output_expr(., "NUM", missing_msg = msg_not_print)
}
CK Code: ch1-10
# Here is the code to create two vectors my_vector and my_vector_2. We just made up those names.
# Run the code and see what happens
my_vector <- c(1,2,3,4,5)
my_vector_2 <- c(10,10,10,10,10)
# Now write some code to print out these two vectors in the R console. Run the code and see what happens.
my_vector <- c(1,2,3,4,5)
my_vector_2 <- c(10,10,10,10,10)
my_vector # or print(my_vector)
my_vector_2 # or print(my_vector_2)
ex() %>% {
check_object(., 'my_vector')
check_object(., 'my_vector_2')
check_output_expr(., "my_vector")
check_output_expr(., "my_vector_2")
}
CK Code: ch1-11
my_vector <- c(1, 2, 3, 4, 5)
# write code to multiply each number in my_vector by 100
my_vector <- c(1, 2, 3, 4, 5)
my_vector * 100
ex() %>% {
check_object(., "my_vector") %>% check_equal()
check_operator(., "*") %>% check_result() %>% check_equal()
}
CK Code: ch1-12
# Run the code below to see what happens
my_vector <- c(1,2,3,4,5)
my_vector * 100
# This will print out my_vector
my_vector
my_vector <- c(1,2,3,4,5)
my_vector * 100
my_vector
ex() %>% {
check_object(., "my_vector") %>% check_equal(incorrect_msg = "Make sure not to change the contents of my_vector")
check_operator(., "*") %>% check_result() %>% check_equal(incorrect_msg = "Make sure to keep the line my_vector * 100")
check_output_expr(., "my_vector", missing_msg = "Did you print my_vector?")
}
CK Code: ch1-13
require(coursekata)
my_vector <- c(1,2,3,4,5)
# This creates `my_vector` and stores 1, 2, 3, 4, 5 in it
my_vector <- c(1,2,3,4,5)
# Now write code to save `my_vector * 100` back into `my_vector`
my_vector <-
my_vector <- c(1,2,3,4,5)
my_vector <- my_vector * 100
ex() %>% {
check_operator(., "*") %>% check_result() %>% check_equal()
check_object(., "my_vector") %>% check_equal()
}
CK Code: ch1-14
require(coursekata)
my_vector <- c(1,2,3,4,5)
my_vector <- my_vector * 100
# Write code to get the 4th value in my_vector
my_vector[4]
ex() %>% check_output_expr("my_vector[4]", missing_msg = "Have you used `[4]` to print out the 4th number in `my_vector`?")
CK Code: ch1-15
require(coursekata)
my_vector <- c(100,200,300,400,500)
# Use sum() to total up the values in my_vector
sum(my_vector)
ex() %>% {
check_object(., "my_vector")
check_function(., "sum", not_called_msg = "don't forget to use the sum() function") %>% check_result() %>% check_equal(incorrect_msg = "did you call sum() on my_vector?")
}
CK Code: ch1-16
# Here are two ways of creating a numeric vector with the numbers 1 to 10
my_num_1 <- c(1,2,3,4,5,6,7,8,9,10)
my_num_2 <- 1:10
# Write code to print out both of these numeric vectors
my_num_1 <- c(1,2,3,4,5,6,7,8,9,10)
my_num_2 <- 1:10
my_num_1
my_num_2
ex() %>% {
check_object(., "my_num_1") %>% check_equal()
check_object(., "my_num_2") %>% check_equal()
check_output_expr(., "my_num_1", times = 2, missing_msg = "Did you print out both my_num_1 and my_num_2?", append = FALSE)
}
CK Code: ch1-17
many_hellos <- c("hi", "hello", "hola", "bonjour", "ni hao", "merhaba")
# Write code to print out the 5th way of saying hello in this vector
many_hellos <- c("hi", "hello", "hola", "bonjour", "ni hao", "merhaba")
many_hellos[5]
ex() %>% {
check_object(., "many_hellos") %>% check_equal(incorrect_msg = "Make sure not to change the contents of many_hellos")
check_output_expr(., "many_hellos[5]", missing_msg = "You can use [] to select the 5th element in many_hellos")
}
CK Code: ch1-18
require(coursekata)
many_hellos <- c("hi", "hello", "hola", "bonjour", "ni hao", "merhaba")
# See what happens when you submit this code:
many_hellos[1]== "hi"
many_hellos[1]== "hi"
ex() %>% check_output_expr("many_hellos[1]=='hi'", missing_msg = "Make sure you don't change the code before pressing Submit")
CK Code: ch1-19
require(coursekata)
many_hellos <- c("hi", "hello", "hola", "bonjour", "ni hao", "merhaba")
# Write some code that will answer this question: Is the first element in the vector many_hellos "hi"?
# And store it in an R object called first_is_hi
first_is_hi <- many_hellos[1] == "hi"
ex() %>% {
check_operator(., "==") %>% check_result() %>% check_equal()
check_object(., "first_is_hi") %>% check_equal()
}
CK Code: ch1-20
# Read this code and predict what value will come out of the R console. Then run the code and see if you were right.
A <- 1
B <- 5
compare <- A > B
compare
A <- 1
B <- 5
compare <- A > B
compare
ex() %>% {
check_object(., "A") %>% check_equal(incorrect_msg = "Make sure not to change the contents of A")
check_object(., "B") %>% check_equal(incorrect_msg = "Make sure not to change the contents of B")
check_object(., "compare") %>% check_equal(incorrect_msg = "Make sure not to change the contents of compare")
check_output_expr(., "compare", missing_msg = "Make sure to print compare")
}
CK Code: ch1-21
# Here's the code to create the my_numbers vector:
my_numbers <- c(1:10)
# And here's the code to check whether each element of the vector my_numbers is greater than or equal to 5, storing the result in a new vector called my_booleans.
my_booleans <- my_numbers >= 5
# This code prints out both vectors
my_numbers
my_booleans
my_numbers <- c(1:10)
my_booleans <- my_numbers >= 5
my_numbers
my_booleans
ex() %>% {
check_object(., "my_numbers") %>% check_equal(incorrect_msg = "Make sure to keep the line that assigns c(1:10) to my_numbers")
check_object(., "my_booleans") %>% check_equal(incorrect_msg = "Make sure you assign my_numbers>= 5 to my_booleans")
check_output_expr(., "my_numbers", missing_msg = "Did you print my_numbers?")
check_output_expr(., "my_booleans", missing_msg = "Did you print my_booleans?")
}
CK Code: ch1-22
# What do you expect from this code? Run the code to see what happens. Then, fix the bug and run again.
A <- 5
B <- 5
compare <- A = B
compare
A <- 5
B <- 5
compare <- A == B
compare
ex() %>% {
check_object(., "A") %>% check_equal(incorrect_msg = "Make sure the object A is assigned the value 5")
check_object(., "B") %>% check_equal(incorrect_msg = "Make sure the object B is assigned the value 5")
check_operator(., "==") %>% check_result() %>% check_equal()
check_object(., "compare") %>% check_equal()
check_output_expr(., "compare", missing_msg = "Did you tell R to print compare?")
}
CK Code: ch1-23
require(coursekata)
# run your code here
CK Code: A1_Code_Review_01
require(coursekata)
# run your code here
CK Code: A1_Code_Review_02